On this page

Serverless

Use Statsig integrations with serverless platforms like AWS Lambda, Vercel, and Cloudflare Workers to evaluate flags and log events at the edge.

The serverless SDK is a lightweight JavaScript SDK optimized for serverless environments and compatible with any platform. The serverless SDK provides the same functionality as all other Statsig server SDKs.

  1. Install the Statsig SDK

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

    bash
    import {StatsigServerlessClient} from '@statsig/serverless-client'
    
  3. Create a StatsigServerlessClient instance

    javascript
    const client = new StatsigServerlessClient(process.env.STATSIG_KEY)
    

    The client instantiation takes two arguments:

    • sdkKey : string Your Statsig client API key. Available from the Project Settings page in the Statsig Console. Statsig uses this to authenticate requests.
    • options : StatsigOptions Refer to StatsigOptions for more options.
  4. Client Initialization

    This line initializes the client by loading feature gate and experiment configurations over the network.

    javascript
    const init = await client.initializeAsync();
    
  5. Checking a Gate

    javascript
    const GateResult = client.checkGate('pass_gate', user);
    

    The checkGate method takes two arguments:

    • name : string The name of the Statsig gate that you are checking.
    • user : StatsigUser The Statsig user object for whom the gate is checked. For more information on the user object, refer to StatsigUser.
    Refer to the JavaScript on-device evaluation SDK documentation for how to check other entities like experiments and dynamic configs.
  6. Logging an Event

    javascript
    client.logEvent('gate_check', { userID: randomUserId });
    

    The logEvent method takes two parameters:

    • eventOrName : string | StatsigEvent The name and details of the event you are logging.
    • user : StatsigUser The Statsig user object for whom the event is logged.
    For more information on event logging, refer to Logging an Event.
  7. javascript
    client.flush();
    

    This flushes all events from the SDK to Statsig. Without this, diagnostic information and logged event data won't appear in the Statsig Console.

Putting it all together

javascript
import { StatsigServerlessClient } from '@statsig/serverless-client';

export default async function handler(request) {
    const client = new StatsigServerlessClient(process.env.STATSIG_KEY);
    
    let init = await client.initializeAsync();
    
    const user = { userID: Math.random().toString().substring(2, 5) };
    const passed = client.checkGate('pass_gate', user);
    
    client.logEvent('serverless_event', user, passed.toString());
    
    client.flush();

    return new Response(
      JSON.stringify({ passed, user }),
    );
}

Bootstrapping

Use the serverless SDK to bootstrap your client SDK.

javascript
// Get client initialize response for a user
const values = statsig.getClientInitializeResponse(user, options);

// Pass values to a client SDK to initialize without a network request
For more information on bootstrapping, including options and a full code example, refer to Client SDK Bootstrapping.

Bring your own CDN

The Statsig Serverless SDK also supports a bring-your-own-CDN model. Store your Statsig config specs anywhere and initialize your client directly from that source. This model has been tested and optimized for AWS Lambda/Lambda@Edge and S3, but is compatible with any other data source. The URL you provide must return the Statsig config specs for your project.

javascript
let init = await client.initializeViaURL("https://my-statsig-config.s3.us-east-1.amazonaws.com/my_dcs.json")

After initialization, use the SDK as usual.

Was this helpful?