Skip to content

API Key Management

API keys give programs access to the platform: SDK clients, server-side applications and scripts. Unlike a user's login token, which expires (after 12 hours by default), an API key lasts until you delete it or it reaches its own expires_at. It is meant for service-to-service calls.

The commands on this page run as written against the stack from the Quick Start, in one terminal, top to bottom. Each one uses the shell variables set by the ones before it.


What API Keys Are Used For

  • SDK integration: the SDKs authenticate with an API key to evaluate feature flags, assign users to experiments and track events.
  • Server-to-server calls: backend services that track conversions or evaluate flags.
  • Event ingestion: the tracking endpoints (/api/v1/tracking/*) take an API key.

An API key authenticates as the user who created it, on the endpoints an SDK calls: tracking, flag evaluation and the flag ruleset. A key that carries the mcp:read scope also reads flags, experiments and results on four read operations (see Scopes); a key without it is refused there with 403. Creating or changing flags and experiments, and managing users, take a user login; an API key there is refused with 401 and {"detail":"Not authenticated"}, the answer to a request with no login at all. API keys are not for signing people in: user-facing applications use the login described in Authentication.


Creating an API Key

Creating a key takes a user login. Log in first; this saves the token in $TOKEN:

TOKEN=$(curl -s -X POST localhost:8000/api/v1/auth/login \
  -H 'content-type: application/json' \
  -d '{"email":"admin@demo.com","password":"Demo1234!"}' | jq -r .access_token)

curl -s localhost:8000/api/v1/auth/me -H "Authorization: Bearer $TOKEN" | jq .role

It prints "ADMIN". Any role can create keys for itself.

Then create the key. The collection URL has no trailing slash: /api/v1/api-keys/ answers 307 Temporary Redirect, which curl doesn't follow.

CREATED=$(curl -s -X POST localhost:8000/api/v1/api-keys \
  -H "Authorization: Bearer $TOKEN" \
  -H 'content-type: application/json' \
  -d '{
    "name": "Production Checkout Service",
    "description": "Used by the checkout microservice to evaluate feature flags"
  }')
KEY=$(jq -r .key <<<"$CREATED")
KEY_ID=$(jq -r .id <<<"$CREATED")

jq '{name, prefix}' <<<"$CREATED"

The API answers 201 Created. The response carries the key's id, name, key, prefix (the key's first nine characters, eptk_ and four more), created_at, expires_at and environment. This saves the key in $KEY and its id in $KEY_ID, and prints:

{
  "name": "Production Checkout Service",
  "prefix": "eptk_af52"
}

The key value is shown only once. Only a hash of it is stored, so nobody can show it to you again. Store it immediately in a secure location (a secrets manager, not a code repository). If you lose it, create a new key.

The request takes: - name (required, up to 100 characters); - description; - scopes, a list of names, at most 255 characters in all when joined with commas; a longer list answers 422 (see Scopes); - environment, the flag environment the key works in: production (the default), staging or development. It can't be changed later, and mcp:read is for production keys; - expires_at, an ISO 8601 time after which the key stops working. Leave it out for a key that doesn't expire.

Scopes

Scopes are stored with the key and returned when you list keys. Two scopes are enforced: sdk:ruleset and mcp:read. Every endpoint that accepts an API key, except the ones listed below for each and the two a web page calls (Refused in a web page), accepts any active key, whatever its scopes, except that the routes where experiments and event tracking run refuse a staging or development key (Flag environments). Names such as read, write or admin on existing keys are labels only: no route requires one, though the two routes a web page calls refuse a key that carries any scope.

Scope names are matched exactly and case-sensitively: the stored list is split on commas and each entry is trimmed, so "read, sdk:ruleset " carries sdk:ruleset, while SDK:RULESET, xsdk:ruleset and sdk:ruleset-ro do not. In the same way MCP:READ, xmcp:read and mcp:read-only do not carry mcp:read.

sdk:ruleset

sdk:ruleset is for a server: one that evaluates flags locally, one that assigns lists of users to an experiment, or one that links an anonymous id to a known id. These routes answer 403 to a key that does not carry it:

  • GET /api/v1/sdk/ruleset (beta), which returns every feature flag's targeting rules, those of the key's environment, in the form a server-side SDK evaluates locally;
  • POST /api/v1/tracking/evaluations (beta), where a server-side SDK reports how many times it evaluated each flag locally, so safety monitoring can still compute each flag's error rate;
  • POST /api/v1/tracking/assign/batch (beta), which assigns up to 1,000 users to an experiment in one request (see Assign a customer list to an experiment);
  • POST /api/v1/tracking/alias (beta), which your backend calls to link an anonymous id to the known id of the same person (see API endpoints).

The 403 says This API key does not have the 'sdk:ruleset' scope. Create a key with the 'sdk:ruleset' scope for server-side SDK use.

A key with sdk:ruleset can therefore download every feature flag's targeting rules, including the values in them, and the values string, number and JSON flags serve, so keep such a key on a server and never ship it to a browser or a mobile app.

  • In the dashboard (Admin → API Keys → Create API Key), tick Server-side local evaluation (sdk:ruleset); the same checkbox is the one for assigning lists of users and for linking ids. Leave it unticked for any other key; the key is then created with no scopes. The dashboard's API Keys page needs a superuser account; any other user creates keys through the API, or on Your API keys.
  • Through the API, send "scopes": ["sdk:ruleset"].

Only users who can change feature flags can create a key with the sdk:ruleset scope: the ADMIN and DEVELOPER roles, superusers, and in the full profile anyone whose custom role or direct grant gives feature_flag (update). For anyone else the request is refused with 403 Forbidden, whose message says who may create such a key, and no key is created. Keys without that scope can be created by every role, as before. Using such a key follows the same rule: the four routes above accept a key with sdk:ruleset only while its owner can change feature flags, checked on every request, so a key whose owner loses that (moved to ANALYST or VIEWER, or a grant revoked or expired) is refused with 403 from then on. POST /api/v1/tracking/assign/batch also needs the owner to be able to change experiments; otherwise it answers 403 and assigns nobody. POST /api/v1/tracking/alias does not: an owner who can change feature flags may link ids, whether or not they can change experiments.

mcp:read

mcp:read lets a key read flags, experiments and results. These four operations accept a key that carries it, as well as a user login:

  • GET /api/v1/feature-flags/, which lists the feature flags;
  • GET /api/v1/experiments/, which lists the experiments;
  • GET /api/v1/experiments/{experiment_id}, which reads one experiment;
  • GET /api/v1/experiments/{experiment_id}/results, which reads its results.

The request reads as the user who created the key, and the operation checks that user's role as it does for a login. Every role may read all four, so anyone holding the key reads everything its owner can there: every flag, with its targeting rules and the values it serves, every experiment, and every experiment's results. Keep such a key out of browsers, mobile apps and code repositories. A key without the scope is refused on these four with 403:

This API key does not have the 'mcp:read' scope. Create a key with the 'mcp:read' scope to read flags, experiments and results with it.

The scope opens nothing else. The routes that create, change or delete flags or experiments, and the routes that manage users, still take a user login, and a request that carries a bearer token is judged on the token alone.

Every role can create a production key with mcp:read: through the API, send "scopes": ["mcp:read"]. A staging or development key cannot carry it, and the request answers 422. Keys already labelled mcp:read gain that access when the instance is upgraded to a version that enforces the scope. An ADMIN lists every user's keys, with their scopes, with GET /api/v1/api-keys?all=true.

Refused in a web page

A key in a web page can be read by anyone who opens the page. So the two routes the visual editor's browser script calls answer 403 to a key that carries any scope, whatever its name:

  • POST /api/v1/tracking/visual (beta), which answers the visual changes and goals a visitor gets on a page;
  • POST /api/v1/tracking/visual/preview (beta), which answers the variant a preview link shows.

The 403 says This API key has scopes, so it must stay on a server: a key in a web page can be read by anyone who opens the page. Use a key with no scopes on your website.

Use a key with no scopes on a website: through the API, send "scopes": [] or leave scopes out. A key with a scope keeps working on every other route that takes a key.


Using an API Key

Pass the key in the X-API-Key header on every request. This evaluates the demo data's beta_features flag for one user:

curl -s -G localhost:8000/api/v1/feature-flags/evaluate/beta_features \
  -H "X-API-Key: $KEY" \
  --data-urlencode "user_id=user-123" \
  --data-urlencode 'context={"plan":"pro"}' | jq '{key, enabled}'

It prints "enabled": true: the flag is on for everyone.

This tracks a purchase by the same user in the demo data's checkout_button_color experiment. A tracked event names the experiment (experiment_key) or the flag (feature_flag_key) it belongs to:

curl -s -X POST localhost:8000/api/v1/tracking/track \
  -H "X-API-Key: $KEY" \
  -H 'content-type: application/json' \
  -d '{
    "user_id": "user-123",
    "experiment_key": "checkout_button_color",
    "event_type": "purchase",
    "value": 49.99
  }' | jq '{event_type, value}'

It prints the stored event's event_type and value.

In an SDK, the key is a setting of the client. Load it from the environment rather than writing it into the code:

const client = new ExperimentationClient({
  apiUrl: 'https://your-platform.example.com',
  apiKey: process.env.EXPERIMENTATION_API_KEY,
});
client = ExperimentationClient(
    api_url="https://your-platform.example.com",
    api_key=os.environ["EXPERIMENTATION_API_KEY"],
)

Listing Keys

List your keys. The secret values are never shown:

curl -s localhost:8000/api/v1/api-keys \
  -H "Authorization: Bearer $TOKEN" | jq '.[] | {name, scopes, is_active}'

It prints each of your keys, including the one created above:

{
  "name": "Production Checkout Service",
  "scopes": [],
  "is_active": true
}

Each key also carries its id, description, user_id, created_at, expires_at, last_used_at and environment. The list holds only your own keys. An ADMIN, or any superuser, can add ?all=true to list every user's keys. The list leaves out inactive keys unless you add ?include_inactive=true.

last_used_at is when the key last authenticated a request, and null for a key that was never used. The API writes it at most once a minute per key, so it can trail the last request by up to a minute. Use it to find keys that are no longer used.


Deleting a Key

Delete a key when it is no longer needed, or if you suspect it has leaked. Only its owner or an ADMIN can delete it:

curl -s -o /dev/null -w '%{http_code}\n' -X DELETE localhost:8000/api/v1/api-keys/$KEY_ID \
  -H "Authorization: Bearer $TOKEN"

It prints 204: the key is deleted permanently, and can't be restored.

From then on, requests with the key are refused:

curl -s -X POST localhost:8000/api/v1/tracking/track \
  -H "X-API-Key: $KEY" \
  -H 'content-type: application/json' \
  -d '{"user_id": "user-123", "experiment_key": "checkout_button_color", "event_type": "purchase"}'

It prints {"detail":"Invalid API Key"}, with status 401 Unauthorized. A service still using the key starts failing at once, so delete a key only after its services use a new one.


Key Rotation Best Practices

Rotate API keys on a regular schedule to limit the window of exposure if a key leaks.

Environment Recommended Rotation
Production Every 90 days
Staging / Development Every 180 days
CI/CD pipelines On every major deployment

Setting expires_at when you create a key makes the schedule hard to forget: the key stops working on that date.

Zero-Downtime Rotation Procedure

  1. Create a new API key with the same name, scopes and environment as the old key.
  2. Update your service's secrets (Secrets Manager, environment variables, and so on) to the new key.
  3. Deploy or restart the service so it uses the new key.
  4. Check that the service works with the new key.
  5. Delete the old key.

The service always has a valid key during the rotation.


Security Best Practices

Never commit keys to source control

API keys in source code can be exposed in logs, error messages or git history. Load keys from environment variables or a secrets manager:

apiKey: process.env.EXPERIMENTATION_API_KEY

and never write the key itself into the code.

Use a secrets manager

Store production API keys in a dedicated secrets manager:

  • AWS Secrets Manager: integrates with ECS task definitions and Lambda environment variables
  • HashiCorp Vault: for multi-cloud or on-premise setups
  • Kubernetes Secrets: for Kubernetes-based deployments

Separate keys per service

Use one API key per service or application, and name it after the service. That limits the blast radius of a leaked key: you can delete that service's key without affecting the others.

checkout-service-prod    → eptk_aaaa…
recommendations-prod     → eptk_bbbb…
analytics-pipeline       → eptk_cccc…

Grant sdk:ruleset only where it is needed

Give the sdk:ruleset scope only to a server that will evaluate flags locally, assign lists of users or link ids. Every other key, including any key used in a browser or a mobile app, should be created without it. Because no other scope limits a key, a separate key per service is what lets you revoke one integration without touching the others.

Remember that a browser key answers for any user id

A key used in a browser or a mobile app is visible to the people who use the app, and it answers for whichever user id it is sent: what that id is served, whether it is in a segment, and which experiments it is assigned to, which for an id linked to another as one person are that person's assignments. Where that matters, use user ids that do not identify a person (not email addresses), in ID lists too, and read assignments from your server.

Remember that a key acts as its owner

A key authenticates as the user who created it. Create keys from an account whose role is no broader than the services need. When a user leaves, first switch every application that uses one of their keys to a key created by an account that is staying, then deactivate the user or delete their keys. Removing a user from the identity provider does not revoke their API keys: deactivate the Controlvar user or delete the keys.