---
title: "Create metric"
method: POST
path: "/api/v2/metrics/{projectKey}"
tags: ["Metrics"]
---

# Create metric

`POST /api/v2/metrics/{projectKey}`

Create a new metric in the specified project. The expected `POST` body differs depending on the specified `kind` property.

## Path parameters

- `projectKey` string, string, required — The project key

## Request body

- MetricPost
  - `key` string, required — A unique key to reference the metric
  - `name` string — A human-friendly name for the metric
  - `description` string — Description of the metric
  - `kind` 'pageview' | 'click' | 'custom', required — The kind of event your metric will track
  - `selector` string — One or more CSS selectors. Required for click metrics only.
  - `urls` UrlPost[] — One or more target URLs. Required for click and pageview metrics only.
    - `kind` 'exact' | 'canonical' | 'substring' | 'regex'
    - `url` string
    - `substring` string
    - `pattern` string
  - `isNumeric` boolean — Whether to track numeric changes in value against a baseline (<code>true</code>) or to track a conversion when an end user takes an action (<code>false</code>). Required for custom metrics only.
  - `unit` string — The unit of measure. Applicable for numeric custom metrics only.
  - `eventKey` string — The event key to use in your code. Required for custom conversion/binary and custom numeric metrics only.
  - `successCriteria` 'HigherThanBaseline' | 'LowerThanBaseline' — Success criteria. Required for custom numeric metrics, optional for custom conversion metrics.
  - `tags` string[] — Tags for the metric
  - `randomizationUnits` string[] — An array of randomization units allowed for this metric
  - `maintainerId` string — The ID of the member who maintains this metric
  - `unitAggregationType` 'average' | 'sum' — The method by which multiple unit event values are aggregated
  - `analysisType` string — The method for analyzing metric events
  - `percentileValue` integer — The percentile for the analysis method. An integer denoting the target percentile between 0 and 100. Required when <code>analysisType</code> is <code>percentile</code>.
  - `eventDefault` MetricEventDefaultRep
    - `disabled` boolean — Whether to disable defaulting missing unit events when calculating results. Defaults to false
    - `value` number — The default value applied to missing unit events. Set to 0 when <code>disabled</code> is false. No other values are currently supported.
  - `dataSource` MetricDataSourceRefRep
    - `key` string, required
    - `environmentKey` string
    - `_name` string
    - `_integrationKey` string
  - `filters` EventFilter
    - `type` string, required — Filter type. One of [contextAttribute, eventProperty, group]
    - `attribute` string — If not a group node, the context attribute name or event property name to filter on
    - `op` string, required
    - `values` unknown[], required — The context attribute / event property values or group member nodes. Numeric values must not exceed 14 decimal places.
      - unknown
    - `contextKind` string — For context attribute filters, the context kind.
    - `negate` boolean, required — If set, then take the inverse of the operator. 'in' becomes 'not in'.

## Response `201`

Metric response

- MetricRep
  - `experimentCount` integer — The number of experiments using this metric
  - `metricGroupCount` integer — The number of metric groups using this metric
  - `activeExperimentCount` integer — The number of active experiments using this metric
  - `activeGuardedRolloutCount` integer — The number of active guarded rollouts using this metric
  - `_id` string, required — The ID of this metric
  - `_versionId` string, required — The version ID of the metric
  - `_version` integer — Version of the metric
  - `key` string, required — A unique key to reference the metric
  - `name` string, required — A human-friendly name for the metric
  - `kind` 'pageview' | 'click' | 'custom', required — The kind of event the metric tracks
  - `_attachedFlagCount` integer — The number of feature flags currently attached to this metric
  - `_links` object, required — The location and content type of related resources
  - `_site` Link
    - `href` string
    - `type` string
  - `_access` Access
    - `denied` AccessDenied[], required
      - `action` string, required
      - `reason` AccessDeniedReason, required
        - `resources` string[] — Resource specifier strings
        - `notResources` string[] — Targeted resources are the resources NOT in this list. The <code>resources</code> and <code>notActions</code> fields must be empty to use this field.
        - `actions` ActionSpecifier[] — Actions to perform on a resource
        - `notActions` ActionSpecifier[] — Targeted actions are the actions NOT in this list. The <code>actions</code> and <code>notResources</code> fields must be empty to use this field.
        - `effect` 'allow' | 'deny', required — Whether this statement should allow or deny actions on the resources.
        - `role_name` string
    - `allowed` AccessAllowedRep[], required
      - `action` string, required
      - `reason` AccessAllowedReason, required
        - `resources` string[] — Resource specifier strings
        - `notResources` string[] — Targeted resources are the resources NOT in this list. The <code>resources</code> and <code>notActions</code> fields must be empty to use this field.
        - `actions` ActionSpecifier[] — Actions to perform on a resource
        - `notActions` ActionSpecifier[] — Targeted actions are the actions NOT in this list. The <code>actions</code> and <code>notResources</code> fields must be empty to use this field.
        - `effect` 'allow' | 'deny', required — Whether this statement should allow or deny actions on the resources.
        - `role_name` string
  - `tags` string[], required — Tags for the metric
  - `_creationDate` integer, required
  - `lastModified` Modification
    - `date` string, date-time
  - `maintainerId` string — The ID of the member who maintains this metric
  - `_maintainer` MemberSummary
    - `_links` object, required — The location and content type of related resources
    - `_id` string, required — The member's ID
    - `firstName` string — The member's first name
    - `lastName` string — The member's last name
    - `role` string, required — The member's base role. If the member has no additional roles, this role will be in effect.
    - `email` string, required — The member's email address
  - `description` string — Description of the metric
  - `category` string — The category of the metric
  - `isNumeric` boolean — For custom metrics, whether to track numeric changes in value against a baseline (<code>true</code>) or to track a conversion when an end user takes an action (<code>false</code>).
  - `successCriteria` 'HigherThanBaseline' | 'LowerThanBaseline' — For custom metrics, the success criteria
  - `unit` string — For numeric custom metrics, the unit of measure
  - `eventKey` string — For custom metrics, the event key to use in your code
  - `randomizationUnits` string[] — An array of randomization units allowed for this metric
  - `filters` Filter
    - `type` 'group' | 'contextAttribute' | 'eventProperty', required — Filter type. One of [contextAttribute, eventProperty, group]
    - `attribute` string — If not a group node, the context attribute name or event property name to filter on
    - `op` string, required
    - `values` unknown[], required — The context attribute / event property values or group member nodes
      - unknown
    - `contextKind` string — For context attribute filters, the context kind.
    - `negate` boolean, required — If set, then take the inverse of the operator. 'in' becomes 'not in'.
  - `unitAggregationType` 'average' | 'sum' — The method by which multiple unit event values are aggregated
  - `analysisType` 'mean' | 'percentile' — The method for analyzing metric events
  - `percentileValue` integer — The percentile for the analysis method. An integer denoting the target percentile between 0 and 100. Required when <code>analysisType</code> is <code>percentile</code>.
  - `eventDefault` MetricEventDefaultRep
    - `disabled` boolean — Whether to disable defaulting missing unit events when calculating results. Defaults to false
    - `value` number — The default value applied to missing unit events. Set to 0 when <code>disabled</code> is false. No other values are currently supported.
  - `dataSource` MetricDataSourceRefRep, required
    - `key` string, required
    - `environmentKey` string
    - `_name` string
    - `_integrationKey` string
  - `lastSeen` integer
  - `archived` boolean — Whether the metric version is archived
  - `archivedAt` integer
  - `selector` string — For click metrics, the CSS selectors
  - `urls` UrlMatcher[]
  - `experiments` DependentExperimentRep[]
    - `key` string, required — The experiment key
    - `name` string, required — The experiment name
    - `environmentId` string, required — The environment ID
    - `environmentKey` string, required — The environment key
    - `creationDate` integer, required
    - `archivedDate` integer
    - `_links` object, required — The location and content type of related resources
  - `metricGroups` DependentMetricGroupRep[] — Metric groups that use this metric
    - `key` string, required — A unique key to reference the metric group
    - `name` string, required — A human-friendly name for the metric group
    - `kind` 'funnel' | 'standard' | 'guardrail', required — The type of the metric group
    - `_links` object, required — The location and content type of related resources
  - `lastUsedInExperiment` DependentExperimentRep
    - `key` string, required — The experiment key
    - `name` string, required — The experiment name
    - `environmentId` string, required — The environment ID
    - `environmentKey` string, required — The environment key
    - `creationDate` integer, required
    - `archivedDate` integer
    - `_links` object, required — The location and content type of related resources
  - `lastUsedInGuardedRollout` DependentMeasuredRolloutRep
    - `_id` string, required — The guarded rollout measured rollout Id
    - `flagKey` string, required — The guarded rollout flag key
    - `flagName` string, required — The guarded rollout flag name
    - `flagPurpose` string — The guarded rollout flag purpose
    - `environmentKey` string, required — The guarded rollout environment key
    - `environmentName` string, required — The guarded rollout environment name
    - `status` string, required — The guarded rollout status
    - `creationDate` integer, required
    - `_links` object, required — The location and content type of related resources
  - `isActive` boolean — Whether the metric is active
  - `_attachedFeatures` FlagListingRep[] — Details on the flags attached to this metric
    - `name` string, required — The flag name
    - `key` string, required — The flag key
    - `_links` object
    - `_site` Link
      - `href` string
      - `type` string

## Other responses

- `400` — Invalid request
- `401` — Invalid access token
- `403` — Forbidden
- `404` — Invalid resource identifier
- `409` — Status conflict
- `429` — Rate limited

## Changes

- **2026-02-28** `69c5c9aafe78` — 10 info
  - added the new optional request property `dataSource`
  - added the new optional request property `filters`
  - added the optional property `activeExperimentCount` to the response with the `201` status
  - added the optional property `activeGuardedRolloutCount` to the response with the `201` status
  - …6 more
- **2025-07-08** `b2724c5174d1` — 1 warning
  - added the new `guardrail` enum value to the `metricGroups/items/kind` response property for the response status `201`

[Change history](https://skmtc.dev/launchdarkly/apis/launchdarkly-rest-api/changes/api/v2/metrics/:projectKey/post.md)

---

[API](https://skmtc.dev/launchdarkly/apis/launchdarkly-rest-api.md) · [All operations](https://skmtc.dev/launchdarkly/apis/launchdarkly-rest-api/llms.txt) · [OpenAPI document](https://skmtc-service-production.skmtc.workers.dev/v1/apis/launchdarkly/launchdarkly-rest-api/revisions/69c5c9aafe78/schema)
