Overview
Statsig’s Cloudflare integration pushes Statsig Configs to Cloudflare KV, providing low latency for gate and experiment evaluations in Cloudflare Workers.Configure Integration
First, enable the Cloudflare integration in the Statsig Console. Navigate to Project Settings -> Integrations, and then select Cloudflare You will need to input the following:- Cloudflare Account ID: Can be found in Cloudflare portal under Account Home -> Workers, on the right hand side
- KV Namespace ID: We recommend creating a new KV namespace for your Statsig integration. You can create a new namespace, and get the ID from Account Home -> Storage and Databases -> KV, and copy it from the table view.
- Cloudflare API Key: Can be found in Cloudflare portal under Account Home -> Profile -> API Tokens. We need a token with Account.Workers KV Storage Edit Permissions.
statsig-
. The next part of that is your project ID. Copy that to the clipboard - you’ll need it later.
Add the Statsig SDK to your Worker
Now lets hook up the SDK to read that config payload and use it for gate and experiment checks in your worker. If you’ve never created a worker before, you can follow the instructions here. The remainder of this document assumes you are using nodejs. If you are using another language, see the sdk language specific documentation for how to apply the implementation to that language. You may need to implement a custom data adapter for your language - we currently only offer thestatsig-node-cloudflare-kv
package in node.
First up, you’ll need to install the statsig sdk and the cloudflare kv data adapter.
- Creating a new
CloudflareKVDataAdapter
with the namespace you set up previously and your statsig project id - Initializing the statsig sdk with the DataAdapter
- Checking a Gate
- Flushing events to statsig
1. The CloudflareKVDataAdapter
- The KV namespace you set up previously. This must be accessible and configured as a binding for the worker
- Your statsig project id. This is the string that comes after
statsig-
in the KV namespace prefix. You can also find it in any url when you are on the Statsig Console. For example,https://console.statsig.com/7LhuarZImmfNdtl9IsDJeX/gates/test_cloudflare_sync/diagnostics
has a project ID of7LhuarZImmfNdtl9IsDJeX
2. SDK Initialization
- Your statsig secret key. This is available from the Project Settings page in the Statsig Console. This is used to authenticate your requests to the statsig backend. In this example, we’ve configured it as an environment variable (set this in the cloudflare dashboard under Worker > Settings > Variables and Secrets)
- An options object. We are using the
dataAdapter
property to hook up the Cloudflare KV store to the SDK. We’re also disabling the ID list sync to speed up initialization
3. Checking a Gate
StatsigUser
object you are checking, and the second is the gate name. Refer to the node sdk documentation for how to check other entities like experiments and dynamic configs. Here, we have created a user with a random userID for every evaluation to illustrate a gate with a partial rollout working.