# Create a Fitness API client

This example shows how to create a Fitness API client.

> [!NOTE]
> **Note:** In this example, only aggregate steps are read, so no Android permissions are required.

Create the API client as follows:

1. Create a `FitnessOptions` instance, declaring the
   [data types](https://developers.google.com/fit/datatypes) and access type (read and/or write) your app
   needs:

   > [!IMPORTANT]
   > **Tip:** Choose data types responsibly. Don't request every data type in case your app might need it. The types specified determine which scopes the user is prompted to grant permission for. Only ask for the data types your app needs so users are more likely to grant access. Users more readily grant access to limited, clearly described scopes.

       val fitnessOptions = FitnessOptions.builder()
           .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
           .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
           .build()

2. Get an instance of the `Account` object to use with the API:

       val account = GoogleSignIn.getAccountForExtension(this, fitnessOptions)

3. Check if the user has previously granted the necessary data access, and if
   not, initiate the authorization flow:

       if (!GoogleSignIn.hasPermissions(account, fitnessOptions)) {
           GoogleSignIn.requestPermissions(
               this, // your activity
               GOOGLE_FIT_PERMISSIONS_REQUEST_CODE, // e.g. 1
               account,
               fitnessOptions)
       } else {
           accessGoogleFit()
       }

4. If the authorization flow is required, handle the user's response:

       override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
           super.onActivityResult(requestCode, resultCode, data)
           when (resultCode) {
               Activity.RESULT_OK -> when (requestCode) {
                   GOOGLE_FIT_PERMISSIONS_REQUEST_CODE -> accessGoogleFit()
                   else -> {
                       // Result wasn't from Google Fit
                   }
               }
               else -> {
                   // Permission not granted
               }
           }
       }

5. After the user has authorized access to the data requested, create a fitness
   client (for example, a `HistoryClient` to read and/or write historic fitness
   data) based on your app's purpose and needs:

       private fun accessGoogleFit() {
           val end = LocalDateTime.now()
           val start = end.minusYears(1)
           val endSeconds = end.atZone(ZoneId.systemDefault()).toEpochSecond()
           val startSeconds = start.atZone(ZoneId.systemDefault()).toEpochSecond()

           val readRequest = DataReadRequest.Builder()
               .aggregate(DataType.AGGREGATE_STEP_COUNT_DELTA)
               .setTimeRange(startSeconds, endSeconds, TimeUnit.SECONDS)
               .bucketByTime(1, TimeUnit.DAYS)
               .build()
           val account = GoogleSignIn.getAccountForExtension(this, fitnessOptions)
           Fitness.getHistoryClient(this, account)
               .readData(readRequest)
               .addOnSuccessListener({ response ->
                   // Use response data here
                   Log.i(TAG, "OnSuccess()")
               })
               .addOnFailureListener({ e -> Log.d(TAG, "OnFailure()", e) })
       }