Skip to main content

Direct API Access

Flagsmith is built around a client/server architecture. The REST API server is accessible from SDK clients as well as the administration front end. This decoupling means that you can programmatically access the entire API if you wish.

We have a Postman Collection that you can use to play around with the API and get a feel for how it works.

Run in Postman

You can also access the API directly with tools like curl or httpie, or with clients for languages that we do not currently have SDKs for.

API Explorer

You can view the API via Swagger at https://api.flagsmith.com/api/v1/docs/ or get OpenAPI as JSON or YAML.

Environment Key

Publicly accessible API calls need to have an environment key supplied with each request. This is provided as an HTTP header, with the name X-Environment-Key and the value of the Environment Key that you can find within the Flagsmith administrative area.

Curl Examples

These are the two main endpoints that you need to consume the SDK aspect of the API.

Get Environment Flags

curl 'https://edge.api.flagsmith.com/api/v1/flags/' -H 'X-Environment-Key: <Your Env Key>'

Send Identity with Traits and receive Flags

This command will perform the entire SDK Identity workflow in a single call:

  1. Lazily create an Identity
  2. Setting Traits for the Identity
  3. Receiving the Flags for that Identity
curl --request POST 'https://edge.api.flagsmith.com/api/v1/identities/' \
--header 'X-Environment-Key: <Your Env Key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"identifier":"identifier_5",
"traits": [
{
"trait_key": "my_trait_key",
"trait_value": 123.5
},
{
"trait_key": "my_other_key",
"trait_value": true
}
]
}'

Private Endpoints

You can also do things like create new flags, environments, toggle flags or indeed anything that is possible from the administrative front end via the API.

To authenticate, you can use the token associated with your account. This can be found in the "Account" page from the top right navigation panel. Use this token for API calls. For example, to create a new evironment:

curl 'https://api.flagsmith.com/api/v1/environments/' \
-H 'content-type: application/json' \
-H 'authorization: Token <TOKEN FROM PREVIOUS STEP>' \
--data-binary '{"name":"New Environment","project":"<Project ID>"}'

You can find a complete list of endpoints via the Swagger REST API at https://api.flagsmith.com/api/v1/docs/.

Code Examples

Update the value / state of a feature in an environment

Here is a simple python example for updating the value of a feature state in a particular environment.

import json
import os

import requests

TOKEN = os.environ.get("API_TOKEN") # ontained from Account section in dashboard
ENV_KEY = os.environ.get("ENV_KEY") # obtained from environment settings in dashboard
BASE_URL = "https://api.flagsmith.com/api/v1" # update this if self hosting
FEATURE_STATES_URL = f"{BASE_URL}/environments/{ENV_KEY}/featurestates"
FEATURE_NAME = os.environ.get("FEATURE_NAME")

session = requests.Session()
session.headers.update(
{"Authorization": f"Token {TOKEN}", "Content-Type": "application/json"}
)

# get the existing feature state id based on the feature name
get_feature_states_response = session.get(
f"{FEATURE_STATES_URL}/?feature_name={FEATURE_NAME}"
)
feature_state_id = get_feature_states_response.json()["results"][0]["id"]

# update the feature state
data = {"enabled": True, "feature_state_value": "new value"} # `feature_state_value` can be str, int, bool or float
update_feature_state_response = session.patch(
f"{FEATURE_STATES_URL}/{feature_state_id}/", data=json.dumps(data)
)