Experiments over the API
Create a metric and an A/B experiment with the REST API, with copy-paste bodies.
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_catalogscope (ordata_catalog_approval, which includes it). The broad scopes*:writeand*:readdo not grant it, because metrics are governed catalog objects. With only*:writethe call is refused.Create an experiment: a key that can write experiments (
experiment:write, or*:write). Reading results needsexperiment:read.The flag. Creating an experiment creates its feature flag. Without
flag_keythe flag is named from the experiment name (lower-case, dashes); if a flag of that key already exists the request is rejected. Withflag_key: a flag that does not exist is created with that key, and an existing flag is reused only if it ismultivariate, has exactly the experiment's variant keys and is not already in a live experiment, otherwise409names the reason. The key used comes back in thex-lucida-flag-keyresponse header.The exposure event. Results count a person from their first
$exposureevent ($feature_flag_calledalso 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 lucidapreviews the changes,lucida apply --dir lucidamakes them (matched byname, so a re-run changes nothing). A minimal set:
lucida/metrics/signup_conversion.yamlname: 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 fromlucida 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 aflags/new-pricing-page.yaml: the key would be taken. A stand-alone flag islucida/flags/<key>.yamlwithkey,kind,filtersandenabled.
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_pctsumming to 100; the first is the control. Or givefactorswithkind: "multivariate".metrics.primaryandsecondaryare metric ids;guardrailentries are{"metric": "<id>"}.audiencelimits who is enrolled (property filters);traffic_pctis the share of eligible visitors in the experiment.Read it with
GET /v1/projects/{project_id}/experiments/{id}and change it withPATCH.
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.