Write Blood Glucose Data

Your app can record blood glucose data by writing to the com.google.blood_glucose data type. In this data type, each data point represents a single instantaneous blood glucose reading. The data point contains fields for the blood glucose concentration, temporal relationships to meals and sleep, and the source of the specimen which was measured. All fields except for blood glucose concentration are optional.

Create a data source

Android

To write a blood glucose data point, create a new DataSource of TYPE_BLOOD_GLUCOSE, as shown in the following example:

val bloodGlucoseSource = DataSource.Builder()
    .setDataType(TYPE_BLOOD_GLUCOSE)
    // ...
    .build()

REST

To write a blood glucose data point, create a new data source.

HTTP method

POST

Request URL

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

Request body

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

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 POST \
  --header "Content-Type: application/json;encoding=utf-8" --data @blood-glucose-ds.json \
  https://www.googleapis.com/fitness/v1/users/me/dataSources

Adding data

Android

To add data to the source created above, create a data point for this data source, which can be inserted using the History API:

val bloodGlucose = DataPoint.builder(bloodGlucoseSource)
    .setTimestamp(timestamp, TimeUnit.MILLISECONDS)
    .setField(FIELD_BLOOD_GLUCOSE_LEVEL, 5.0f) // 90 mg/dL
    .setField(FIELD_TEMPORAL_RELATION_TO_MEAL, FIELD_TEMPORAL_RELATION_TO_MEAL_BEFORE_MEAL)
    .setField(FIELD_MEAL_TYPE, MEAL_TYPE_BREAKFAST)
    .setField(FIELD_TEMPORAL_RELATION_TO_SLEEP, TEMPORAL_RELATION_TO_SLEEP_ON_WAKING)
    .setField(FIELD_BLOOD_GLUCOSE_SPECIMEN_SOURCE, BLOOD_GLUCOSE_SPECIMEN_SOURCE_CAPILLARY_BLOOD)
    .build()

REST

This example demonstrates adding blood glucose data using the data source created above.

HTTP method

PATCH

Request URL

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

Request body

For clarity the JSON body shown below is annotated with comments, to show the use of health field constants. Although the Fit API will currently drop comments, it is highly recommended you remove these from your code, as JSON does not officially support comments.

{
  "minStartTimeNs": 1574159699023000000,
  "maxEndTimeNs": 1574159699023000000,
  "dataSourceId": "datasource.dataStreamId",
  "point": [
    {
      "startTimeNanos": 1574159699023000000,
      "endTimeNanos": 1574159699023000000,
      "dataTypeName": "com.google.blood_glucose",
      "value": [
        {
          // Blood glucose level, 90 mg/dL
          "fpVal": 5.0
        },
        {
          // FIELD_TEMPORAL_RELATION_TO_MEAL_BEFORE_MEAL
          "intVal": 3
        },
        {
          // MEAL_TYPE_BREAKFAST
          "intVal": 1
        },
        {
          // TEMPORAL_RELATION_TO_SLEEP_ON_WAKING
          "intVal": 3
        },
        {
          // BLOOD_GLUCOSE_SPECIMEN_SOURCE_CAPILLARY_BLOOD
          "intVal": 2
        }
      ]
    }
  ]
}

Response

If the blood glucose data is added successfully, the response is a 200 OK status code. The response body contains a JSON representation of the blood glucosedata that has been added.

CURL command

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