If your app needs to capture information that isn't already covered by one of the
existing data types on the Google Fit platform, you can create a custom data type.

> [!NOTE]
> **Note:** Custom data types are only available to the apps that created them. Other apps can't see or use your custom data types.

## Creating custom data types

Create, or specify, a custom data type to capture custom data. When you create
custom data types, make sure:

- The data type name accurately represents the underlying data.
- The prefix of the data type name matches your app's package name.

### Android

To create a custom data type for the first time, use the
[`ConfigClient.createCustomDataType`](https://developers.google.com/android/gms/fitness/ConfigClient#create%0ACustomDataType(com.google.android.gms.fitness.request.DataTypeCreateRequest))
method:

    val request = DataTypeCreateRequest.Builder()
        // The prefix of your data type name must match your app's package name
        .setName("com.packagename.appname.custom_data_type") // Add some custom fields, both int and float
        .addField("field1", Field.FORMAT_INT32)
        .addField("field2", Field.FORMAT_FLOAT)
        // Add some common fields
        .addField(Field.FIELD_ACTIVITY)
        .build()

    Fitness.getConfigClient(this, account)
        .createCustomDataType(request)
        .addOnSuccessListener { dataType ->
            // Use this custom data type to insert data into your app.
            Log.d(TAG, "Created data type: ${dataType.name}")
        }

### REST

Data types are a property of data sources, in the REST API. To capture
custom data, you need to create a data source and then specify the data type:

1. Call the REST API to create a *new data source* . For example, `FlexibilityMeasure`.
2. Give the data type a unique name, that closely represents the data it's capturing.

   > [!NOTE]
   > **Note:** This name can be anything, as long as it's unique and doesn't have the same name as another data type. We recommend this convention: com.packagename.appname.custom_data_type

3. Specify the fields of the data type, and their formats.

**HTTP method**

    POST

**Request URL**

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

**Request body**

    {
      "dataStreamName": "FlexibilityMeasure",
      "type": "raw",
      "application": {
        "detailsUrl": "http://recoveryapps.com",
        "name": "Stretch Flex",
        "version": "1"
      },
      "dataType": {
        "name": "com.recoveryapps.stretchflex.flexibility",
        "field": [
         {
          "name": "ankle_range_degrees",
          "format": "integer"
         },
         {
          "name": "wrist_range_degrees",
          "format": "integer",
          "optional": true
         }
        ]
       }
    }

**Response**

If your data source was created successfully, you'll get a [`200 OK`](https://httpstatuses.com/200) 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.

## Using custom data types

### Android

Convert your custom data type's name from a string
(com.packagename.appname.custom_data_type) into a `DataType` object using the
[`ConfigClient.readDataType`](https://developers.google.com/android/reference/com/google/android/gms/fitness/ConfigClient#readDataType(java.lang.String)) method. Use the returned object to [insert](https://developers.google.com/android/reference/com/google/android/gms/fitness/HistoryClient#public-taskvoid-insertdata-dataset-dataset)
and [read](https://developers.google.com/android/reference/com/google/android/gms/fitness/ConfigClient#public-taskdatatype-readdatatype-string-datatypename) custom data.

    Fitness.getConfigClient(this, account)
        .readDataType("com.packagename.appname.custom_data_type")
        .addOnSuccessListener { dataType ->
            // Use this custom data type to insert data into your app.
            Log.d(TAG, "Retrieved data type: ${dataType.name}")
        }

### REST

To add or read custom data using your custom data types, you need their data
sources. To check the data sources of a custom data type, [send a `GET`
request to the REST API](https://developers.google.com/fit/rest/v1/reference/users/dataSources/list).

### Adding custom data

To insert custom data, create a dataset with new data points. Specify the
data source you created for your custom data type. The data points need to
have all the correct fields and formats specified in your custom data type.

### Reading custom data

To read custom data, specify the data source you created for your custom
data type when you retrieve data points from the Google Fit platform.