Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In

Evaluate REST APIs

Python code examples demonstrating the Agent REST APIs' capabilities.

๐Ÿ“˜

Note

The Agent REST API documentation is defined via an OpenAPI (Swagger) spec file and is viewable in our Agent API reference documentation.

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 there are different SDK keys 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:

  • Deliver your flag to visitors that match a specific audience using targeted deliveries.
  • Test multiple variations of your flag to find the best one using A/B tests.
  • Use machine learning to allocate traffic to the best-performing variation dynamically using Multi-armed bandit optimizations.

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.