Exposes the growthbook client as an HTTP API, to allow us to interact with it where we cannot run a client
This uses the async version of the GrowthBook client, and FastAPI for the API.
This project uses uv to manage dependancies. After installing uv, you can run a development server using the following command:
uv run fastapi dev src/growthbook_api/main.pyThis project uses ruff for linting:
uv run ruff checkruff for formatting:
uv run ruff formatmypy for type checking:
uv run mypy .and pytest for the test runner:
uv run pytestThe API is documented and available at the /docs/ endpoint of the service. It just passes through the request details to the client. For more information on those fields, please see the growthbook python client documentation and the growthbook documentation. The currently implemented endpoints are:
evaluate_feature. This corresponds to theeval_featuremethod of the GrowthBook client. It takes in thefeature_keyanduser_attributesparameters, and returns thevalue,source,on/off,ruleIdandexperiment. For example, the following request body:
{
"feature_key": "my_feature",
"user_attributes": {
"email": "user@example.com"
}
}translates to the following Python code:
context = UserContext(attributes={"email": "user@example.com"})
return await gb_client.eval_feature("my_feature", context)and can give a response body like:
{
"experiment": {
"key": "test-experiment",
},
"off": false,
"on": true,
"ruleId": "test-ruleid",
"source": "experiment",
"value": "B",
}This relies on the client to manage authentication keys and send the key to the service on every request. The key is sent as a header named Client-Token.
This service is able to serve multiple clients, each with their own client token. Just pass a different token in the header, and it will fetch the configuration for that client. It will keep all the clients in memory and in SSE mode so that they will get updates to the configuration and respond immediately. Only the first request for each token will have a delay while it fetches the configuration from growthbook.