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.

val nutritionSource = 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.

val nutrients = mapOf(
    Field.NUTRIENT_TOTAL_FAT to 0.4f,
    Field.NUTRIENT_SODIUM to 1f,
    Field.NUTRIENT_SATURATED_FAT to 0.1f,
    Field.NUTRIENT_PROTEIN to 1.3f,
    Field.NUTRIENT_TOTAL_CARBS to 27.0f,
    Field.NUTRIENT_CHOLESTEROL to 0.0f,
    Field.NUTRIENT_CALORIES to 105.0f,
    Field.NUTRIENT_SUGAR to 14.0f,
    Field.NUTRIENT_DIETARY_FIBER to 3.1f,
    Field.NUTRIENT_POTASSIUM to 422f
)
val banana = DataPoint.builder(nutritionSource)
    .setTimestamp(timestamp, 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