---
title: "Create metric SQL model"
method: POST
path: "/api/v2/experiments/metric-sql-models"
tags: ["Experiments"]
---

# Create metric SQL model

`POST /api/v2/experiments/metric-sql-models`

## Request

- Base URL: `https://api.datadoghq.com`
- URL: `https://api.datadoghq.com/api/v2/experiments/metric-sql-models`
- Auth: one of:
  - API key in header `DD-API-KEY` and API key in header `DD-APPLICATION-KEY`
  - OAuth 2
    - Authorization code: `/oauth2/v1/authorize`
    - Token URL: `/oauth2/v1/token` scopes: `product_analytics_warehouse_model_write`
- TypeScript: <https://skmtc.dev/datadog/apis/datadog-api-v2-collection/docs/api/v2/experiments/metric-sql-models/post.ts> (a function with typed inputs; its response is checked against the schema)

Create a metric SQL model. Requires at least one subject type, whose subject_type_id must already exist for the organization (list them with GET /api/v2/experiments/subject-types). The model is created against the organization's warehouse connection, which is resolved server-side. Only customer-defined measures belong in measures. The response provides unique_subject_count_measure_id for each subject type and event_count_measure_id for use in metric aggregations. column_type is required for every measure and property. Certification is read-only and cannot be changed through this endpoint.

## Request body

- ExperimentsCreateMetricSQLModelV2Request: Request to create a SQL model that supplies metric data.
  - `data` ExperimentsCreateMetricSQLModelV2RequestData, required: Metric SQL model resource to create.
    - `attributes` ExperimentsUpdateMetricSQLModelV2RequestDataAttributes, required: Complete column mappings and query used to replace the metric SQL model.
      - `date_partition_column` string, nullable: SQL column used to partition the source data by date.
      - `description` string, nullable: Text that explains the metric SQL model.
      - `measures` ExperimentsCreateMetricSQLModelV2RequestDataAttributesMeasuresItems[]: Measures available from the SQL model's result columns.
        - `column_name` string, required: SQL result column that contains the measure values.
        - `column_type` 'STRING' | 'INTEGER' | 'FLOAT' | 'BOOLEAN' | 'DATE' | 'TIMESTAMP', required: Data type of a column in the SQL model.
        - `description` string, nullable: Description of the measure.
        - `migration_metadata` unknown
        - `name` string, nullable: Display name of the measure.
      - `migration_metadata` unknown
      - `name` string, required: Display name of the metric SQL model.
      - `properties` ExperimentsMetricSQLModelPropertyInput[]: Property columns exposed by the SQL model.
        - `column_name` string, required: Name of the SQL result column that supplies this property.
        - `column_type` 'STRING' | 'INTEGER' | 'FLOAT' | 'BOOLEAN' | 'DATE' | 'TIMESTAMP', required: Data type of a column in the SQL model.
        - `description` string, nullable: Optional text that explains what this property represents.
        - `migration_metadata` unknown
        - `name` string, required: Name used to identify the property in the model.
      - `sql` string, required: SQL query that produces the model's source data.
      - `subject_types` ExperimentsCreateExposureSQLModelV2RequestDataAttributesSubjectTypesItems[], required: Subject types mapped to columns in the SQL model.
        - `column_name` string, required: SQL result column that contains the subject identifier.
        - `subject_type_id` string, required: Identifier of the subject type mapped to this column.
      - `timestamp_column` string, required: SQL column that supplies the event timestamp.
    - `id` string: Optional JSON:API resource identifier field.
    - `type` 'metric-sql-models', required: Metric SQL models resource type.

## Response `201`

Created

- ExperimentsMetricSQLModelV2DTO: Response containing the metric SQL model.
  - `data` ExperimentsMetricSQLModelV2DTOData, required: JSON:API resource containing the metric SQL model identity and fields.
    - `attributes` ExperimentsMetricSQLModelV2DTODataAttributes: Details of the metric SQL model.
      - `certified_at` string, date-time, nullable: Time when this resource was certified.
      - `created_at` string, date-time: Time when this resource was created.
      - `date_partition_column` string, nullable: SQL column used to partition the source data by date.
      - `description` string, nullable: Text that explains the metric SQL model.
      - `event_count_measure_id` string: Read-only measure ID. Pass it as warehouse_metric_measure.id when the metric operation is count.
      - `experiment_count` integer, nullable: Number of experiments that reference this resource.
      - `is_certified` boolean: Whether this resource has been certified.
      - `measures` ExperimentsMetricSQLModelV2DTODataAttributesMeasuresItems[]: Measures available from the SQL model's result columns.
        - `column_name` string: Name of the SQL result column that supplies this measure.
        - `column_type` 'STRING' | 'INTEGER' | 'FLOAT' | 'BOOLEAN' | 'DATE' | 'TIMESTAMP': Data type of a column in the SQL model.
        - `description` string: Text that explains the measure.
        - `id` string: ID of the measure.
        - `migration_metadata` unknown
        - `name` string: Display name of the measure.
      - `metric_count` integer, nullable: Number of metrics that use this SQL model.
      - `migration_metadata` unknown
      - `name` string: Display name of the metric SQL model.
      - `properties` ExperimentsMetricSQLModelV2DTODataAttributesMeasuresItems[]: Property columns exposed by the SQL model.
        - `column_name` string: Name of the SQL result column that supplies this measure.
        - `column_type` 'STRING' | 'INTEGER' | 'FLOAT' | 'BOOLEAN' | 'DATE' | 'TIMESTAMP': Data type of a column in the SQL model.
        - `description` string: Text that explains the measure.
        - `id` string: ID of the measure.
        - `migration_metadata` unknown
        - `name` string: Display name of the measure.
      - `sql` string: SQL query that produces the model's source data.
      - `subject_types` ExperimentsMetricSQLModelV2DTODataAttributesSubjectTypesItems[]: Subject types mapped to columns in the SQL model.
        - `column_name` string: Name of the SQL result column that identifies subjects of this type.
        - `subject_type_id` string: ID of the subject type used by this configuration.
        - `unique_subject_count_measure_id` string: Read-only measure ID. Pass it as warehouse_metric_measure.id when the metric operation is `uniqueSubjects`.
      - `timestamp_column` string: SQL column that supplies the event timestamp.
      - `updated_at` string, date-time: Time when this resource was last updated.
    - `id` string, uuid, required: ID of the metric SQL model.
    - `type` 'metric-sql-models', required: Metric SQL models resource type.

## Other responses

- `400`: Malformed body, validation failure, the SQL was rejected by the warehouse validator, or the organization has no warehouse connection.
- `401`: Missing or invalid authentication.
- `403`: Authenticated caller lacks the product_analytics_warehouse_model_write permission.
- `409`: Another metric SQL model in this organization already uses one of these values.
- `429`: Too many requests

---

[API](https://skmtc.dev/datadog/apis/datadog-api-v2-collection.md) - [All operations](https://skmtc.dev/datadog/apis/datadog-api-v2-collection/llms.txt) - [OpenAPI document](https://skmtc.dev/datadog/apis/datadog-api-v2-collection/revisions/3f70db624329?raw)
