Cloudflare KV
Run Statsig feature gate and experiment evaluations at the edge with Cloudflare Workers for low- latency rules evaluation in your CDN layer.
Statsig offers a set of integrations that make usage with Cloudflare easy:
- Automatically pushing changes to Cloudflare's KV store, for low-latency SDK startup
- A helper pattern that handles Statsig SDK overhead, so you can focus on worker logic
Configure Integration
Navigate to Project Settings > Integrations in the Statsig Console, then select Cloudflare and enter:
- Cloudflare Account ID: Can be found in Cloudflare portal on the Compute (Workers) page, under Account Details
- KV Namespace ID: Create a new namespace, then go to Account Home -> Storage and Databases -> Workers KV, and copy the ID from the table view.
- Cloudflare API Key: In Cloudflare portal under Account Home -> Profile -> API Tokens. Use a token with Account.Workers KV Storage Edit Permissions.
Enable Integration
Click Enable. The Statsig backend pushes a config to your KV namespace in under 60 seconds. In your KV namespace, navigate to KV Pairs. You'll see an entry starting with
statsig-. This is thekeyassociated with your KV storage. Copy or record this key for later.Add the Statsig SDK to your Worker
Set up a worker to read experiments and gates from your KV namespace. If you haven't created a worker before, follow the instructions here.After creating your worker, you need to connect your KV store to your worker through a binding. Navigate to Compute (Workers) -> Select Your Worker -> Bindings -> Add binding -> KV namespace. Name your binding under Variable name. Under KV namespace, select your KV store name. For more information on connecting your worker to your KV store, you can follow the instructions here.Install the Statsig SDK
Install the Statsig serverless SDK:
bashnpm install @statsig/serverless-clientUse the SDK
The helper method takes two arguments, a handler function, and a ParamsObject. Put all of your worker logic in the handler function, along with your Statsig usage.
javascriptimport { handleWithStatsig } from '@statsig/serverless-client/cloudflare'; export default handleWithStatsig( async (request, env, ctx, client) => { // Your business, and Statsig logic here }, { kvKey: 'kv_key', envStatsigKey: 'statsig_key', envKvBindingName: 'STATSIG_KV' } );Store the required ParamsObject params (kvKey, envStatsigKey, envKvBindingName) as env variables, either in your wrangler.toml or as Cloudflare secrets.
Example usage
This is an example of an end-to-end worker function that uses the Statsig SDK and returns a flag value. This is all you need: it compiles as the index.js file in your worker.
import { handleWithStatsig } from '@statsig/serverless-client/cloudflare'; export default handleWithStatsig( async (request, env, ctx, client) => { const randomUserId = Math.floor(Math.random() * 100).toString(); const gate = client.getFeatureGate("test_cloudflare_sync", { userID: randomUserId }); const value = gate.value; client.logEvent('new_event', { userID: randomUserId }); return new Response(`Gate check result: ${value}`); }, { kvKey: 'kv_key', envStatsigKey: 'statsig_key', envKvBindingName: 'STATSIG_KV' } );
The helper automatically:
- Initializes the Statsig Client with config specs from your KV store
- Executes your handler code (Your business logic + Statsig usage)
- Flushes all events after your handler completes execution
- Cleans up resources
Was this helpful?