Fastly
Run Statsig feature gate and experiment evaluations at the edge with Fastly Compute@Edge for low-latency rules evaluation in your CDN layer.
Statsig offers a suite of integration tools for use with Fastly:
- Statsig automatically pushes project changes to Fastly KV/Config Store, providing low-latency SDK startup.
- Statsig provides a Fastly helper that handles client initialization and event flushing, so you can focus on your business logic.
Configure integration
First, enable the Fastly integration in the Statsig Console.
Navigate to Project Settings -> Integrations, and then select FastlyInput the following:
- Fastly API Key - Can be found in Fastly portal under Account -> API Tokens.
- Create an Automation Token with:
- global and global:read scope
- Create an Automation Token with:
- Store Type - Select "Config Store" or "KV Store" depending on your storage type.
- Config Store ID OR KV Store ID- Your Store ID
There is also an option to filter the configs synced into your KV namespace by a Target App. Enable this as the size of your config payload grows. For now, you can leave this unchecked.
After filling this out, select Enable.
Within a minute, the Statsig backend generates a config payload from your Statsig project and pushes it into your store. Under your Config or KV Store, look for a key starting with the prefix
statsig-. This is thekeyassociated with your Statsig config specs in your store. Note this key, as it is required later.- Fastly API Key - Can be found in Fastly portal under Account -> API Tokens.
Install the Statsig SDK
Install the Statsig serverless SDK:
bashnpm install @statsig/serverless-clientImport the statsig SDK
Import the Statsig Helper:
javascriptimport { handleWithStatsig} from "@statsig/serverless-client/fastly";Use the SDK
javascripthandleWithStatsig(handler, params)The helper method takes two arguments:
handlerThis is your Fastly Compute code.params : StatsigFastlyHandlerParamsFor best practice: storeParameter Optional Type Description statsigSdkKeyNo stringYour Statsig client API key fastlyStoreTypeNo stringEither kvorconfig, signifying your Fastly store typestoreIdNo stringYour KV or Config store id keyIdNo stringThe key storing your Statsig config specs in your Fastly store apiTokenNo stringYour Fastly API token used to authenticate requests to the Fastly API statsigOptionsYes StatsigOptionsSee StatsigOptions here statsigSdkKeyandapiTokenin a Fastly Secret Store
Example usage
javascriptimport { handleWithStatsig} from "@statsig/serverless-client/fastly"; async function myHandler(event, client) { const user = { userID: Math.random().toString().substring(2, 5) }; const res = client.checkGate("pass_gate", user); client.logEvent("pass_gate", user); return new Response( JSON.stringify({ res, user }), { status: 200, headers: { 'Content-Type': 'application/json' } } ); } const handleRequest = handleWithStatsig(myHandler,{ statsigSdkKey: "client-LhxVWHSeZt2uor***********", fastlyStoreType: "kv", storeId:"7b12fn*********", keyId:"statsig-3htllY8XxFsJ*****", apiToken:"7NaRxS6R**********" }) addEventListener('fetch', (event) => event.respondWith(handleRequest(event)));
The helper automatically:
- Initializes the Statsig Client with config specs from your KV or Config store
- Executes your handler code (your business logic and Statsig usage)
- Flushes all events after your handler completes execution
- Cleans up resources
Advanced usage
To check evaluations, go to the gate you created for this example and view the evaluations in the Diagnostics tab. To check logged events, in the Statsig Console, go to Data -> Events.
Size limits
Fastly Config Store has maximum size limits that may prevent Statsig from pushing configs into Fastly. Go to the Fastly documentation for the latest Config Store limits. If your payload continues to grow, you'll need to set the option to filter the payload by a Target App in the integration settings.Unsupported features
Statsig doesn't currently sync ID Lists into Fastly KVs or Config Stores. If you rely on large (>1000) ID lists, you can't check them in your Fastly compute services.
Was this helpful?