Add Nutrition Data

This section demonstrates adding a set of nutrition data. In the com.google.nutrition data type, each data point represents the value of all nutrients consumed as part of a meal or a food item. In this example the user has just eaten a banana.

Create a data source

Android

This example demonstrates using DataSource.Builder() to create a new data source named nutritionSource.

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

REST

This example demonstrates calling the REST API to create a new data source named 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",
    "field": [
     {
      "name": "nutrients",
      "format": "map"
     },
     {
      "name": "meal_type",
      "format": "integer",
      "optional": true
     },
     {
      "name": "food_item",
      "format": "string",
      "optional": true
     }
    ]
   }
}

Response

If the data source is created successfully, the response is a 200 OK status code. The response body contains a JSON representation of the data source, including a datasource.dataStreamId property that you can use as the data source ID for subsequent requests.

CURL command

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

Add nutrition data

Android

This example demonstrates creating a new data point, and adding nutritional data for a banana, using the nutritionSource data source.

DataPoint banana = DataPoint.create(nutritionSource);
banana.setTimestamp(now.getMillis(), TimeUnit.MILLISECONDS);
banana.getValue(Field.FIELD_FOOD_ITEM).setString("banana");
banana.getValue(Field.FIELD_MEAL_TYPE).setInt(Field.MEAL_TYPE_SNACK);
banana.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_TOTAL_FAT, 0.4f);
banana.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_SODIUM, 1f);
banana.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_SATURATED_FAT, 0.1f);
banana.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_PROTEIN, 1.3f);
banana.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_TOTAL_CARBS, 27.0f);
banana.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_CHOLESTEROL, 0.0f);
banana.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_CALORIES, 105.0f);
banana.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_SUGAR, 14.0f);
banana.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_DIETARY_FIBER, 3.1f);
banana.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_POTASSIUM, 422f);

REST

This example demonstrates adding 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/raw:com.google.nutrition:407408718192:NutritionSource/datasets/1471462988000000000-1471462988001000000

Request body

{
  "minStartTimeNs": 1471244400000,
  "maxEndTimeNs": 1471330800000,
  "dataSourceId":
    "raw:com.google.nutrition:407408718192:NutritionSource,
  "point": [
    {
      "startTimeNanos": 1471244400000,
      "endTimeNanos": 1471330800000,
      "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 the data source is created successfully, the response is a 200 OK status code. The response body contains a JSON representation of the data source, including a datasource.dataStreamId property that you can use as the data source ID for subsequent requests.

CURL command

$ curl --header "Authorization: Bearer ya29.yourtokenvalue --request PATCH \
  --header "Content-Type: application/json;encoding=utf-8" -d @nutrition-data.json \
  https://www.googleapis.com/fitness/v1/users/me/dataSources/raw:com.google.nutrition:407408718192:NutritionSource/datasets/1471462988000000000-1471462988001000000

Note the use of --request PATCH instead of -X POST.

Send feedback about...