# Create a metric

Returns a newly created metric.

[endpoint]
base_url = "https://telemetry.betterstack.com"
path = "/api/v2/sources/{source_id}/metrics"
method = "POST"

[[path_param]]
name = "source_id"
description = "Source for which to create a metric"
required = true
type = "integer"

[[body_param]]
name = "team_name"
description = "Required if using [global API token](https://betterstack.com/docs/logs/api/getting-started/#get-a-global-api-token) to specify the team which should own the resource"
required = false
type = "string"

[[body_param]]
name = "name"
description = "Metric name"
required = true
type = "string"

[[body_param]]
name = "sql_expression"
description = "SQL expression to use for the metric"
required = true
type = "string"

[[body_param]]
name = "aggregations"
description = "Aggregations to apply to the metric. <br> Valid aggregations are avg, count, uniq, max, min, anyLast, sum, p50, p90, p95, p99"
required = false
type = "array"

[[body_param]]
name = "type"
description = "Metric type. <br> Valid types are `string_low_cardinality`, `int64_delta`, `float64_delta`, `datetime64_delta`, and `boolean`"
required = true
type = "string"

[[header]]
name = "Authorization"
description = "Bearer `$TOKEN`"
required = true
type = "string"
[/endpoint]

[responses]
[[response]]
status = 201
description = ''''''
body = '''
{
"data":
{
"id": "g-123",
"type": "metric",
"attributes": {
"name": "level",
"sql_expression": "JSONExtract(raw, 'level', 'Nullable(String)')",
"aggregations": [],
"type": "string_low_cardinality"
}}}
'''
[/responses]

#### Example cURL 

[code-tabs]
```shell
[label Non-agreggated metric]
curl --request POST \
  --url https://telemetry.betterstack.com/api/v2/sources/123/metrics \
  --header "Authorization: Bearer $TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "name": "log_type",
    "sql_expression": "JSONExtractString(raw, '\''message_json'\'', '\''log_type'\'')",
    "type": "string_low_cardinality"
  }'
```
```shell
[label Agreggated metric]
curl --request POST \
  --url https://telemetry.betterstack.com/api/v2/sources/123/metrics \
  --header "Authorization: Bearer $TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "name": "duration_ms",
    "sql_expression": "JSONExtract(raw, '\''message_json'\'', '\''duration_ms'\'', '\''Nullable(Float64)'\'')",
    "aggregations": ["avg", "p95", "min", "max"],
    "type": "float64_delta"
  }'
```
[/code-tabs]
