We're making some changes to the Google Fit APIs. Learn about how these changes might affect your app. Read our new policy.

Add Nutrition Data

You can add nutrition data to Google Fit by creating a data source and using the com.google.nutrition data type. Each data point represents the value of all nutrients consumed in a meal or snack. This example shows you how to add nutrition data for someone who's eaten a banana.

Creating a data source

Android

Use DataSource.Builder to create a new data source. For example, nutritionSource.

DataSource nutritionSource = new DataSource.Builder()
        .setDataType(DataType.TYPE_NUTRITION)
        ...
        .build();

REST

Call the REST API to create a new data source. For example, NutritionSource.

HTTP method

POST

Request URL

https://www.googleapis.com/fitness/v1/users/me/dataSources

Request body

{
  "dataStreamName": "NutritionSource",
  "type": "raw",
  "application": {
    "detailsUrl": "http://example.com",
    "name": "My Example App",
    "version": "1"
  },
  "dataType": {
    "name": "com.google.nutrition",
   }
}

Response

If your data source was created successfully, you'll get a 200 OK HTTP response status code. The response body contains a JSON representation of the data source, including a datasource.dataStreamId property. Use this ID as the dataSourceId to add data.

CURL command

$ curl --header "Authorization: Bearer ya29.yourtokenvalue --request POST \
--header "Content-Type: application/json;encoding=utf-8" --data @nutrition-ds.json \
https://www.googleapis.com/fitness/v1/users/me/dataSources

Adding nutrition data

Android

This example shows you how to create a new data point, and add nutrition data for a banana, using the nutritionSource data source.

Map<String, Float> nutrients = new HashMap<>();
nutrients.put(Field.NUTRIENT_TOTAL_FAT, 0.4f);
nutrients.put(Field.NUTRIENT_SODIUM, 1f);
nutrients.put(Field.NUTRIENT_SATURATED_FAT, 0.1f);
nutrients.put(Field.NUTRIENT_PROTEIN, 1.3f);
nutrients.put(Field.NUTRIENT_TOTAL_CARBS, 27.0f);
nutrients.put(Field.NUTRIENT_CHOLESTEROL, 0.0f);
nutrients.put(Field.NUTRIENT_CALORIES, 105.0f);
nutrients.put(Field.NUTRIENT_SUGAR, 14.0f);
nutrients.put(Field.NUTRIENT_DIETARY_FIBER, 3.1f);
nutrients.put(Field.NUTRIENT_POTASSIUM, 422f);
DataPoint banana =
    DataPoint.builder(nutritionSource)
        .setTimestamp(now.getMillis(), TimeUnit.MILLISECONDS)
        .setField(Field.FIELD_FOOD_ITEM, "banana")
        .setField(Field.FIELD_MEAL_TYPE, Field.MEAL_TYPE_SNACK)
        .setField(Field.FIELD_NUTRIENTS, nutrients)
        .build();

REST

This example shows you how to add a set of nutrition data using the NutritionSource data source. The values for the nutrition data type are nutrients (a map), meal type (4 = 'snack'), and the actual food item (a string).

HTTP method

PATCH

Request URL

  https://www.googleapis.com/fitness/v1/users/me/dataSources/datasource.dataStreamId/datasets/1574159699023000000-1574159699023999000
 

Request body

    {
      "minStartTimeNs": 1574159699023000000,
      "maxEndTimeNs": 1574159699023999000,
      "dataSourceId": "datasource.dataStreamId",
      "point": [
        {
          "startTimeNanos": 1574159699023000000,
          "endTimeNanos": 1574159699023999000,
          "dataTypeName": "com.google.nutrition",
          "value": [
            {
              "mapVal": [
              {
                "key": "fat.total",
                "value": {
                  "fpVal": 0.4
                }
              },
              {
                "key": "sodium",
                "value": {
                  "fpVal": 1.0
                }
              },
              {
                "key": "fat.saturated",
                "value": {
                  "fpVal": 0.1
                }
              },
              {
                "key": "protein",
                "value": {
                  "fpVal": 1.3
                }
              },
              {
                "key": "carbs.total",
                "value": {
                  "fpVal": 27.0
                }
              },
              {
                "key": "cholesterol",
                "value": {
                  "fpVal": 0.0
                }
              },
              {
                "key": "calories",
                "value": {
                  "fpVal": 105.0
                }
              },
              {
                "key": "sugar",
                "value": {
                  "fpVal": 14.0
                }
              },
              {
                "key": "dietary_fiber",
                "value": {
                  "fpVal": 3.1
                }
              },
              {
                "key": "potassium",
                "value": {
                  "fpVal": 422.0
                }
              }
             ]
            },
            {
              "intVal": 4
            },
            {
              "strVal": "banana"
            }
          ]
        }
      ]
    }

Response

If your data point was created successfully, you'll get a 200 OK HTTP response status code. The response body contains a JSON representation of the data set.

CURL command

$ curl --header "Authorization: Bearer ya29.yourtokenvalue --request PATCH \
--header "Content-Type: application/json;encoding=utf-8" --data @nutrition-data.json \
https://www.googleapis.com/fitness/v1/users/me/dataSources/datasource.dataStreamId/datasets/1574159699023000000-1574159699023999000