HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

Evaluate REST APIs

Python code examples demonstrating the Agent REST APIs' capabilities.

📘

Note

The REST API documentation is defined via an OpenAPI (Swagger) spec and can be viewed here.

Start an HTTP session

Each request made into Optimizely Agent is in the context of an Optimizely SDK Key. SDK Keys map API requests to a specific Optimizely Feature Experimentation Project and Environment.

📘

Note

You can find your SDK key in app.optimizely.com under Settings > Environments > SDK Key. Remember you have a different SDK key for each environment.

We can set up a global request header by using the requests.Session object:

import requests

s = requests.Session()
s.headers.update({'X-Optimizely-SDK-Key': 'YOUR-SDK-KEY'})

🚧

️Important

You must create an HTTP session. The following examples will assume this session is being maintained.

Get current environment configuration

The /vi/config` endpoint returns a manifest of the current working environment.

resp = s.get('http://localhost:8080/v1/config')
env = resp.json()

for key in env['featuresMap']:
    print(key)

Run a feature flag rule

The Decide endpoint buckets a user into a feature flag variation (choosing between multiple enabled variations or one disabled variation) as part of a flag rule. Flag rules let you:

  • experiment using A/B tests
  • roll out feature flags progressively to a selected audience using targeted feature flag deliveries.

To run a flag rule, use

# decide 1 flag. 
params = { "keys": "my-feature-flag" }
payload = {
    "userId": "test-user",
    "userAttributes": {
        "attr1": "sample-attribute-1",
        "attr2": "sample-attribute-2"
    }
}

resp = s.post(url = 'http://localhost:8080/v1/decide', params=params, json=payload)

print(resp.json())


# multiple (bulk) feature flag decisions for specified flags.
# To decide ALL flags, simply omit keys params
payload = { "userId": "test-user" }
params = {"keys":"flag_1", "keys":"flag_2"}
resp2 = s.post(url = 'http://localhost:8080/v1/decide', params=params, json=payload)
print(json.dumps(resp.json(), indent=4, sort_keys=True))

The decide API is a POST to signal to the caller that there are side effects. Namely, the decision results in a "decision" event sent to Optimizely analytics for the purpose of analyzing A/B test results.

A decision event will NOT be sent by default if the flag is simply part of a targeted delivery and not an experiment.