The Google Fit APIs, including the Google Fit REST API, will be deprecated in 2026. As of May 1, 2024, developers cannot sign up to use these APIs.

For instructions on which API or platform to migrate to, visit the Health Connect migration guide. For a comparison of Health Connect with the Google Fit APIs and the Fitbit Web APIs, visit the Health Connect comparison guide.

Learn more about Health Connect and how to integrate with the API.

Create a Fitness API client

  • Creating a Fitness API client involves defining required data types and access levels using FitnessOptions.

  • The authorization flow is initiated if the user hasn't previously granted the necessary data access.

  • Handling the user's response after the authorization flow is crucial for proceeding with API access.

  • After authorization, a specific fitness client like HistoryClient is created to read or write data based on app requirements.

This example shows how to create a Fitness API client.

Create the API client as follows:

  1. Create a FitnessOptions instance, declaring the data types and access type (read and/or write) your app needs:

    valfitnessOptions=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:

    valaccount=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:

    overridefunonActivityResult(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:

    privatefunaccessGoogleFit(){
    valend=LocalDateTime.now()
    valstart=end.minusYears(1)
    valendSeconds=end.atZone(ZoneId.systemDefault()).toEpochSecond()
    valstartSeconds=start.atZone(ZoneId.systemDefault()).toEpochSecond()
    valreadRequest=DataReadRequest.Builder()
    .aggregate(DataType.AGGREGATE_STEP_COUNT_DELTA)
    .setTimeRange(startSeconds,endSeconds,TimeUnit.SECONDS)
    .bucketByTime(1,TimeUnit.DAYS)
    .build()
    valaccount=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)})
    }
    

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年08月28日 UTC.