| title | Quickstart for Agent | ||
|---|---|---|---|
| excerpt | |||
| slug | quickstart-for-agent | ||
| hidden | false | ||
| metadata |
|
||
| createdAt | 2020-05-21T20:35:58.387Z | ||
| updatedAt | 2021-03-15T23:02:34.056Z |
This brief quickstart describes how to run Agent, using two examples:
-
To get started using Docker, see the following section.
-
To get started using example Node microservices, see the following video link.
| Resource | Description |
|---|---|
| Implementing feature flags across microservices with Optimizely Agent | 4-minute video on implementing Optimizely Agent with example microservices |
Follow these steps to deploy Optimizely Agent locally via Docker and access some of the common API endpoints. If Docker is not installed then you can download it here.
First pull the Docker image with:
docker pull optimizely/agentThen start the service in the foreground with the following command:
docker run -p 8080:8080 --env OPTIMIZELY_LOG_PRETTY=true optimizely/agentNote that we're enabling "pretty" logs which provide colorized and human readable formatting. The default log output format is structured JSON.
The rest of the getting started guide will demonstrate the APIs capabilities. For brevity, we've chosen to illustrate the API usage with Python. Note that the APIs are also defined via OpenAPI (Swagger) and can be found on localhost here.
Each request made into Optimizely Agent is in the context of an Optimizely SDK Key. SDK Keys map API requests to a specific Optimizely Project and 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>>'})To get your SDK key, navigate to the project settings of your Optimizely account.
Future examples will assume this session is being maintained.
The /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)The /decide?keys={keys} endpoint decides whether to enable a feature flag or flags for a given user. You can decide multiple flags with this syntax: /v1/decide?keys=flagA&keys=flagB. We'll provide a userId via the request body. The API evaluates the userId to determine which flag rule and flag variation the user buckets into. Rule types include A/B tests, in which flag variations are measured against one another, or a flag delivery, which progressively make the flag available to the selected audience.
This endpoint returns an array of OptimizelyDecision objects, which contains information about the flag and rule the user bucketed into.
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())The decide API is a POST to signal to the caller that there are side-effects. Namely, this endpoint results in a "decision" event sent to Optimizely analytics for the purpose of analyzing A/B test results. By default a "decision" is not sent if the feature flag is simply part of a delivery.