Evaluate Agent REST APIs
Python code examples demonstrating the Agent REST APIs' capabilities.
Note
The Agent REST API documentation is defined through an OpenAPI (Swagger) spec file and is viewable in the 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.
You can configure 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. This endpoint results in a decision event sent to Optimizely to analyze the A/B test results on the Optimizely Experiment Results page. By default, a decision is not sent if the feature flag is part of a targeted delivery.
Updated 13 days ago