Experiments over the API

Create a metric and an A/B experiment with the REST API, with copy-paste bodies.

Embed
Preview of the card
<a href="https://lucidace.com/help/experiments-api"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/bff84f43-2ec3-446d-b1f0-11203b861084/social-card.svg?v=3e7ae33eec8b" alt="Experiments over the API" width="600" height="315"></a>

Authenticate as in Queries and insights over the API. The schemas are in GET /v1/openapi.json.

What you need

  • Create a metric: a key with the data_catalog scope (or data_catalog_approval, which includes it). The broad scopes *:write and *:read do not grant it, because metrics are governed catalog objects. With only *:write the call is refused.

  • Create an experiment: a key that can write experiments (experiment:write, or *:write). Reading results needs experiment:read.

  • The flag. Creating an experiment creates its feature flag. Without flag_key the flag is named from the experiment name (lower-case, dashes); if a flag of that key already exists the request is rejected. With flag_key: a flag that does not exist is created with that key, and an existing flag is reused only if it is multivariate, has exactly the experiment's variant keys and is not already in a live experiment, otherwise 409 names the reason. The key used comes back in the x-lucida-flag-key response header.

  • The exposure event. Results count a person from their first $exposure event ($feature_flag_called also works) carrying $feature_flag = the flag key and $feature_flag_response = the variant key. The SDKs send it when you evaluate the flag; over HTTP see Record exposure ("record_exposure": true).

Do this as code

Instead of calling the API, keep the metric and the experiment in files and let the CLI converge them (Project setup as code). lucida plan --dir lucida previews the changes, lucida apply --dir lucida makes them (matched by name, so a re-run changes nothing). A minimal set:

lucida/metrics/signup_conversion.yaml

name: signup_conversion
kind: proportion
definition:
  event: {source: {type: event, name: signed_up}}
  window_days: 7

lucida/experiments/new-pricing-page.yaml (use the metric's id from lucida pull; an experiment file does not accept metric names)

name: New pricing page
kind: ab
hypothesis: A shorter page lifts signups
traffic_pct: 100
variants:
  - {key: control, allocation_pct: 50}
  - {key: test, allocation_pct: 50}
metrics: {primary: ["<signup_conversion metric id>"], secondary: [], guardrail: []}

The experiment creates its own flag, named from the experiment name (new-pricing-page), so do not also add a flags/new-pricing-page.yaml: the key would be taken. A stand-alone flag is lucida/flags/<key>.yaml with key, kind, filters and enabled.

1. Create a metric

An experiment's metrics refer to saved metrics by id. A conversion metric (kind: "proportion") is 1 when the event happens after exposure:

curl -X POST "$LUCIDA/v1/projects/$PROJECT/metrics" -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"name": "signup_conversion", "kind": "proportion",
       "definition": {"event": {"source": {"type": "event", "name": "signed_up"}}, "window_days": 7}}'

Other kinds usable in experiments: mean ({"value": {"source": {...}, "property": "amount"}}), ratio (numerator, denominator), funnel, retention, quantile, threshold. Names are lower-case identifiers (^[a-z][a-z0-9_]{0,62}$). The answer carries the metric id.

2. Create the experiment

curl -X POST "$LUCIDA/v1/projects/$PROJECT/experiments" -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{
    "name": "New pricing page",
    "hypothesis": "A shorter page lifts signups",
    "flag_key": "new-pricing-page",
    "variants": [
      {"key": "control", "allocation_pct": 50},
      {"key": "test", "allocation_pct": 50}
    ],
    "traffic_pct": 100,
    "metrics": {"primary": ["<signup_conversion metric id>"], "secondary": [], "guardrail": []}
  }'
  • variants: at least two, unique keys, allocation_pct summing to 100; the first is the control. Or give factors with kind: "multivariate".

  • metrics.primary and secondary are metric ids; guardrail entries are {"metric": "<id>"}.

  • audience limits who is enrolled (property filters); traffic_pct is the share of eligible visitors in the experiment.

  • Read it with GET /v1/projects/{project_id}/experiments/{id} and change it with PATCH.

Evaluate the flag from your app as in Flag governance; the SDKs record the exposure. A caller that uses POST /v1/flags directly must record it itself, see Record exposure.

Updated