LeveredDocs
API Reference

Metrics

Endpoints for defining reward metrics, assignment sources, and previewing metric data.

A metric is a SQL query that Levered runs against your warehouse. A reward metric returns the events that count as a reward. An assignment source returns the exposure rows. All metric endpoints are under /api/v2/warehouse and require an Authorization: Bearer <token> header.

Create metric

POST /api/v2/warehouse/metrics

Auth: Required

Creates a reward metric backed by a SQL query against your warehouse. If the organization already has a metric with the same name, the request replaces its SQL and column mapping.

Request body:

{
  "name": "Signup Conversion",
  "sql": "SELECT user_id AS anonymous_id, converted_at AS timestamp FROM conversions",
  "column_mapping": {
    "anonymous_id": "anonymous_id",
    "timestamp": "timestamp",
    "reward_type": "bool"
  }
}
FieldTypeRequiredDescription
namestringYesHuman-readable metric name
sqlstringYesSQL query that produces the metric data. Must return at least the columns specified in column_mapping.
column_mappingobjectYesMaps query output columns to Levered's expected fields
column_mapping.anonymous_idstringYesColumn containing the user identifier
column_mapping.timestampstringYesColumn containing the event timestamp
column_mapping.reward_typestringNoType of reward: bool (binary conversion) or numeric (continuous value)
column_mapping.valuestringNoColumn with the numeric reward value. Without it, each returned row counts as a reward of 1

Response (201):

{
  "id": "metric-uuid",
  "name": "Signup Conversion",
  "sql": "SELECT user_id AS anonymous_id, converted_at AS timestamp FROM conversions",
  "column_mapping": {
    "anonymous_id": "anonymous_id",
    "timestamp": "timestamp",
    "reward_type": "bool"
  },
  "created_at": "2026-03-28T14:00:00Z",
  "updated_at": null,
  "meta": {
    "request_id": "req_abc123",
    "organization_id": "org_xyz"
  }
}

List metrics

GET /api/v2/warehouse/metrics

Auth: Required

Returns all reward metrics in the organization. Assignment sources are not in the list.

Response:

{
  "metrics": [
    {
      "id": "metric-uuid",
      "name": "Signup Conversion",
      "sql": "SELECT user_id AS anonymous_id, converted_at AS timestamp FROM conversions",
      "column_mapping": {
        "anonymous_id": "anonymous_id",
        "timestamp": "timestamp",
        "reward_type": "bool"
      },
      "created_at": "2026-03-28T14:00:00Z",
      "updated_at": null
    }
  ],
  "meta": {
    "request_id": "req_abc123",
    "organization_id": "org_xyz"
  }
}

Get metric

GET /api/v2/warehouse/metrics/:id

Auth: Required

Path parameters:

ParameterTypeDescription
idstring (UUID)Metric ID

Response: Returns the full metric object including sql and column_mapping.

Preview metric

POST /api/v2/warehouse/metrics/:id/preview

Auth: Required

Executes the metric's SQL query and returns a sample of the results. Use it to check that the query returns the expected data before linking the metric to an optimization.

Path parameters:

ParameterTypeDescription
idstring (UUID)Metric ID

Query parameters:

ParameterTypeDefaultDescription
limitinteger10Number of rows to return (1-1000)

Response:

{
  "rows": [
    { "anonymous_id": "user-abc", "timestamp": "2026-03-28T14:00:00Z" },
    { "anonymous_id": "user-def", "timestamp": "2026-03-28T14:05:00Z" }
  ],
  "meta": {
    "request_id": "req_abc123",
    "organization_id": "org_xyz"
  }
}

Update metric

PUT /api/v2/warehouse/metrics/:id

Auth: Required

Replaces a metric's name, SQL and column mapping. Takes the same body as Create metric. The new SQL is checked against your warehouse in the same way before it is saved. Works for reward metrics and for an optimization's exposure source, for example to filter test users out of both.

Response: the updated metric, as in Create metric.

Delete metric

DELETE /api/v2/warehouse/metrics/:id

Auth: Required

Deletes a metric. If the metric is referenced by guardrails, the request fails with 409 Conflict.

Path parameters:

ParameterTypeDescription
idstring (UUID)Metric ID

Response: 204 No Content

Assignment source

Assignment sources define where Levered reads exposure (variant assignment) data from your warehouse. Each optimization is linked to one through assignment_source_id at creation. If assignment_source_id is omitted, Levered creates one for the optimization.

Create assignment source

Creates a new assignment source, or replaces the one with the same name.

POST /api/v2/warehouse/assignment-source

Auth: Required

Request body:

{
  "name": "Exposure Events",
  "sql": "SELECT anonymous_id, timestamp, headline, cta_text FROM exposures",
  "column_mapping": {
    "anonymous_id": "anonymous_id",
    "timestamp": "timestamp"
  }
}
FieldTypeRequiredDescription
namestringYesHuman-readable name
sqlstringYesSQL query that returns exposure rows. Must include the design factor columns.
column_mapping.anonymous_idstringYesColumn containing the user identifier
column_mapping.timestampstringYesColumn containing the event timestamp
column_mapping.variantstringNoColumn containing a JSON-serialized variant object (e.g., {"headline": "A", "cta": "B"})
column_mapping.contextstringNoColumn containing JSON context attributes
column_mapping.factorsobjectNoMaps design factor names to warehouse column names (e.g., {"headline": "headline_col"})

Response (201):

{
  "id": "source-uuid",
  "name": "Exposure Events",
  "sql": "SELECT anonymous_id, timestamp, headline, cta_text FROM exposures",
  "column_mapping": {
    "anonymous_id": "anonymous_id",
    "timestamp": "timestamp"
  },
  "created_at": "2026-03-28T14:00:00Z",
  "updated_at": null,
  "meta": {
    "request_id": "req_abc123",
    "organization_id": "org_xyz"
  }
}

Get assignment source

GET /api/v2/warehouse/assignment-source

Auth: Required

Returns an assignment source configuration. If the organization has several, it returns one of them.

Preview assignment source

POST /api/v2/warehouse/assignment-source/preview

Auth: Required

Executes the assignment source SQL and returns a sample of rows.

Response:

{
  "rows": [
    { "anonymous_id": "user-abc", "timestamp": "2026-03-28T14:00:00Z", "headline": "Ship faster", "cta_text": "Start free" },
    { "anonymous_id": "user-def", "timestamp": "2026-03-28T14:01:00Z", "headline": "Build better", "cta_text": "Try it now" }
  ],
  "meta": {
    "request_id": "req_abc123",
    "organization_id": "org_xyz"
  }
}