Your app can record blood glucose data by writing to the
[`com.google.blood_glucose`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/HealthDataTypes#TYPE_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.

> [!NOTE]
> **Note:** Because health data is potentially sensitive, Google Fit [restricts read/write
> access to health data types](https://developers.google.com/fit/datatypes/restricted).

- The blood glucose concentration is measured in *mmol/L* (1 mmol/L is equivalent to 18 mg/dL).
- If specified, temporal relation to meal must have one of the values listed in [`FIELD_TEMPORAL_RELATION_TO_MEAL`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/HealthFields#FIELD_TEMPORAL_RELATION_TO_MEAL).
- Meal type must have one of the values listed in [`FIELD_MEAL_TYPE`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Field#FIELD_MEAL_TYPE). If the meal type is not known, use [`MEAL_TYPE_UNKNOWN`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Field#MEAL_TYPE_UNKNOWN).
- If specified, temporal relation to sleep must have one of the values listed in [`FIELD_TEMPORAL_RELATION_TO_SLEEP`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/HealthFields#FIELD_TEMPORAL_RELATION_TO_SLEEP).
- If specified, blood glucose specimen source must have one of the values listed in [`FIELD_BLOOD_GLUCOSE_SPECIMEN_SOURCE`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/HealthFields#FIELD_BLOOD_GLUCOSE_SPECIMEN_SOURCE).

## Create a data source

### Android

To write a blood glucose data point, create a new [`DataSource`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/DataSource)
of [`TYPE_BLOOD_GLUCOSE`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/HealthDataTypes#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**

```html
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](https://developers.google.com/fit/scenarios/write-blood-glucose-data#create_a_data_source).

**HTTP method**

    PATCH

**Request URL**

```html
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.

```restructuredtext
{
  "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
```

> [!NOTE]
> **Note:** Use --request PATCH (not --request POST) in your CURL command when adding data.