Skip to Content

Simulators

Experiments

Experiments let you test code that moves an A/B test through its lifecycle and acts on the flag that splits users between variants. The posthog_flag_experiment scenario seeds three experiments in project 3103, one in each starting state:

KEY=phx_rysticexamplepersonalkey000000000000000
TOKEN=phc_45f8a0bcb7d18d1ec767bab3d1dd386fb5c692b93c1
curl -s -X POST "$RYSTIC_URL/_rystic/scenario" -o /dev/null -w '%{http_code}\n' -d '{"name":"posthog_flag_experiment"}'
# -> 204
P="$RYSTIC_URL/api/projects/3103"
api() { curl -s -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' "$@"; }
variant() { curl -s -X POST "$RYSTIC_URL/flags/?v=2" -d "{\"token\":\"$TOKEN\",\"distinct_id\":\"$1\"}" | jq -c ".flags[\"$2\"] | {enabled, variant}"; }
api "$P/experiments/" | jq -c '.results[] | {id, name, status, feature_flag_key}'
# -> {"id":5201,"name":"Checkout redesign","status":"running","feature_flag_key":"checkout-redesign"}
# -> {"id":5202,"name":"Pricing page copy","status":"draft","feature_flag_key":"pricing-page-copy"}
# -> {"id":5203,"name":"Onboarding checklist","status":"stopped","feature_flag_key":"onboarding-checklist"}

The api helper sends your personal API key, as Auth describes. The variant helper evaluates one flag for one user with the scenario’s project API key. This scenario’s project is 3103, not the 3104 of the default scenario. Later blocks build on the state each one leaves behind.

Launch a draft experiment

Launching sets start_date to the simulator’s clock. A second launch returns 400:

api -X POST "$P/experiments/5202/launch/" | jq -c '{status, start_date}'
# -> {"status":"running","start_date":"2026-03-01T00:00:00...Z"}
api -X POST "$P/experiments/5202/launch/" -w '%{http_code}\n'
# -> {"attr":null,"code":"invalid_input","detail":"Experiment has already been launched.","type":"validation_error"}
# -> 400

Pause and resume a running experiment

Pausing turns the linked flag off, so every user evaluates it as disabled until you resume:

api -X POST "$P/experiments/5201/pause/" | jq -c '{status, flag_active: .feature_flag.active}'
# -> {"status":"paused","flag_active":false}
variant user_1001 checkout-redesign
# -> {"enabled":false,"variant":null}
api -X POST "$P/experiments/5201/resume/" | jq -c '{status, flag_active: .feature_flag.active}'
# -> {"status":"running","flag_active":true}
variant user_1001 checkout-redesign
# -> {"enabled":true,"variant":"control"}

End an experiment and read its result

To record a result, send conclusion and conclusion_comment with the end request. The experiment moves to stopped, and its flag stays on with the same split:

api -X POST "$P/experiments/5201/end/" -d '{"conclusion":"won","conclusion_comment":"Test raised conversion"}' -o /dev/null -w '%{http_code}\n'
# -> 200
api "$P/experiments/5201/" | jq -c '{status, end_date, conclusion, conclusion_comment}'
# -> {"status":"stopped","end_date":"2026-03-01T00:00:00...Z","conclusion":"won","conclusion_comment":"Test raised conversion"}
variant user_1002 checkout-redesign
# -> {"enabled":true,"variant":"test"}
api -X POST "$P/experiments/5201/end/" | jq -r .detail
# -> Experiment is not running.

Ship the winning variant

Shipping a variant rolls it out to every user of the flag. Name it in variant_key; a key the flag does not have returns 400:

api -X POST "$P/experiments/5201/ship_variant/" -d '{"variant_key":"test"}' -o /dev/null -w '%{http_code}\n'
# -> 200
api "$P/feature_flags/5101/" | jq -c '[.filters.multivariate.variants[] | {key, rollout_percentage}]'
# -> [{"key":"control","rollout_percentage":0},{"key":"test","rollout_percentage":100}]
variant user_1001 checkout-redesign
# -> {"enabled":true,"variant":"test"}
api -X POST "$P/experiments/5201/ship_variant/" -d '{"variant_key":"blue"}' -w '%{http_code}\n'
# -> {"attr":null,"code":"invalid_input","detail":"The variant_key was not found on the experiment's linked feature flag.","type":"validation_error"}
# -> 400

The feature flags page covers the flag routes and evaluation in detail.

Reset, archive and duplicate an experiment

Resetting returns an experiment to draft and clears its dates. Only a stopped experiment can be archived, and the list leaves archived experiments out unless you ask for them:

api -X POST "$P/experiments/5202/reset/" | jq -c '{status, start_date, end_date}'
# -> {"status":"draft","start_date":null,"end_date":null}
api -X POST "$P/experiments/5202/archive/" | jq -r .detail
# -> Experiment must be ended before it can be archived.
api -X POST "$P/experiments/5203/archive/" | jq -c '{status, archived}'
# -> {"status":"stopped","archived":true}
api "$P/experiments/" | jq -c '[.results[].id]'
# -> [5201,5202]
api "$P/experiments/?archived=true" | jq -c '[.results[].id]'
# -> [5203]
api -X POST "$P/experiments/5203/duplicate/" | jq -c '{id, name, status, feature_flag_key}'
# -> {"id":5204,"name":"Onboarding checklist (Copy)","status":"draft","feature_flag_key":"onboarding-checklist"}

The copy links the same flag as the original.

Create an experiment with a holdout and a shared metric

Creating an experiment also creates its flag, switched off until launch. Pass holdout_id to keep a holdout’s users out, and saved_metrics_ids to attach shared metrics:

api -X POST "$P/experiments/" -d '{"name":"Search ranking","feature_flag_key":"search-ranking","holdout_id":5251,"saved_metrics_ids":[{"id":5301,"metadata":{"type":"primary"}}]}' | jq -c '{id, status, holdout_id, metrics: [.saved_metrics[].name], flag: (.feature_flag | {id, key, active, holdout: .filters.holdout})}'
# -> {"id":5205,"status":"draft","holdout_id":5251,"metrics":["Revenue per user"],"flag":{"id":5105,"key":"search-ranking","active":false,"holdout":{"exclusion_percentage":10,"id":5251}}}
api "$P/experiments/5205/" | jq -c '[.parameters.feature_flag_variants[] | {key, rollout_percentage}]'
# -> [{"key":"control","rollout_percentage":50},{"key":"test","rollout_percentage":50}]

Without parameters.feature_flag_variants, the flag gets a control and a test variant split evenly.

Tag experiments in bulk

POST .../experiments/bulk_update_tags/ changes the tags of several experiments in one request. action is add, remove or set:

api -X POST "$P/experiments/bulk_update_tags/" -d '{"ids":[5201,5202],"action":"add","tags":["q1","checkout"]}' | jq -c .
# -> {"skipped":[],"updated":[{"id":5201,"tags":["q1","checkout"]},{"id":5202,"tags":["q1","checkout"]}]}
api -X POST "$P/experiments/bulk_update_tags/" -d '{"ids":[5202],"action":"set","tags":["pricing"]}' | jq -c .updated
# -> [{"id":5202,"tags":["pricing"]}]

Read an experiment’s history

The activity route lists the changes recorded for an experiment, newest first. It includes the creation of the flag the experiment uses:

api "$P/experiments/5201/activity/" | jq -c '.results[] | {activity, scope, fields: [.detail.changes[]?.field]}'
# -> {"activity":"updated","scope":"Experiment","fields":["tags"]}
# -> {"activity":"updated","scope":"Experiment","fields":["start_date"]}
# -> {"activity":"created","scope":"Experiment","fields":[]}
# -> {"activity":"created","scope":"FeatureFlag","fields":[]}

Manage holdouts and shared metrics

A holdout’s filters give the share of users it keeps out, and the simulator names each group’s variant holdout-<id>. A shared metric’s query must be an ExperimentMetric, and linked_experiments lists the experiments that use it:

api -X POST "$P/experiment_holdouts/" -d '{"name":"Pricing holdout","filters":[{"properties":[],"rollout_percentage":5}]}' | jq -c '{id, filters}'
# -> {"id":5252,"filters":[{"properties":[],"rollout_percentage":5,"variant":"holdout-5252"}]}
api -X POST "$P/experiment_saved_metrics/" -d '{"name":"Checkout conversion","query":{"kind":"ExperimentMetric","metric_type":"funnel","series":[{"kind":"EventsNode","event":"order_completed"}]}}' | jq -c '{id, name, linked_experiments}'
# -> {"id":5302,"name":"Checkout conversion","linked_experiments":[]}
api -X POST "$P/experiment_saved_metrics/" -d '{"name":"Weekly users","query":{"kind":"TrendsQuery"}}' | jq -c '{attr, detail}'
# -> {"attr":"query","detail":"Only an ExperimentMetric query is supported for a shared metric."}
api "$P/experiment_saved_metrics/5301/" | jq -c '.linked_experiments'
# -> [{"id":5201,"is_running":false,"name":"Checkout redesign"},{"id":5205,"is_running":false,"name":"Search ranking"}]

Lifecycle reference

Each action changes status only from the states below. Any other starting state returns 400 with code invalid_input and the detail shown:

ActionAllowed fromResultdetail otherwise
launchdraftrunning, flag onExperiment has already been launched.
pauserunningpaused, flag offExperiment is not running
resumepausedrunning, flag onExperiment is not paused.
endrunningstopped, flag unchangedExperiment is not running.
ship_variantrunning or stoppedstopped, variant at 100%The experiment is in draft state and cannot be shipped.
resetrunning or stoppeddraft, dates clearedThe experiment is already in draft state.
archivestoppedarchived set to trueExperiment must be ended before it can be archived.
freeze_exposurerunning, with can_freeze_exposure set to trueexposure_frozenExposure can only be frozen on a running experiment.

The refusals below come from the draft experiment 5202 and from 5205 once launched:

for action in pause resume reset ship_variant freeze_exposure; do api -X POST "$P/experiments/5202/$action/" | jq -r .detail; done
# -> Experiment is not running
# -> Experiment is not paused.
# -> The experiment is already in draft state.
# -> The experiment is in draft state and cannot be shipped.
# -> Exposure can only be frozen on a running experiment.
api "$P/experiments/5205/" | jq -c '{status, flag_active: .feature_flag.active}'
# -> {"status":"draft","flag_active":false}
api -X POST "$P/experiments/5205/launch/" | jq -c '{status, flag_active: .feature_flag.active, can_freeze_exposure}'
# -> {"status":"running","flag_active":true,"can_freeze_exposure":false}
api -X POST "$P/experiments/5205/freeze_exposure/" -o /dev/null -w '%{http_code}\n'
# -> 400
api -X POST "$P/experiments/5205/freeze_exposure/" | jq -r .detail
# -> This experiment's exposure cannot be frozen.

An experiment created over the API starts with its flag off, and launching it turns the flag on. Its can_freeze_exposure stays false, so freezing its exposure returns 400.

Last updated on