On this page

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.
  1. Configure integration

    First, enable the Fastly integration in the Statsig Console.

    Navigate to Project Settings -> Integrations, and then select Fastly

    Input 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
    • 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 the key associated with your Statsig config specs in your store. Note this key, as it is required later.

  2. Install the Statsig SDK

    Install the Statsig serverless SDK:

    bash
    npm install @statsig/serverless-client
    
  3. Import the statsig SDK

    Import the Statsig Helper:

    javascript
    import { handleWithStatsig} from "@statsig/serverless-client/fastly";
    
  4. Use the SDK

    javascript
    handleWithStatsig(handler, params)
    

    The helper method takes two arguments:

    • handler This is your Fastly Compute code.

    • params : StatsigFastlyHandlerParams

      For best practice: store statsigSdkKey and apiToken in a Fastly Secret Store

    Example usage

    javascript
    import { 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?