---
title: Serverless
description: "Use Statsig integrations with serverless platforms like AWS Lambda, Vercel, and Cloudflare Workers to evaluate flags and log events at the edge."
product: general
token_estimate: 1078
---
# Serverless

> For AI agents: a documentation index is available at [/llms.txt](/llms.txt). Append `.md` to any page URL for markdown, or send `Accept: text/markdown`.

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](https://console.statsig.com/api_keys) page in the Statsig Console. Statsig uses the key to authenticate requests.
   - `options : StatsigOptions` Refer to [StatsigOptions](https://docs.statsig.com/client/javascript-sdk#statsig-options) 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're checking.
   - `user : StatsigUser` The Statsig user object for whom you check the gate. For more information on the user object, refer to [StatsigUser](https://docs.statsig.com/sdks/user#introduction-to-the-statsiguser-object).

   Refer to the [JavaScript on-device evaluation SDK documentation](https://docs.statsig.com/client/jsOnDeviceEvaluationSDK) 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're logging.
   - `user : StatsigUser` The Statsig user object for whom you log the event.

   For more information on event logging, refer to [Logging an Event](https://docs.statsig.com/client/jsOnDeviceEvaluationSDK#logging-an-event).
7. ****

   ```javascript
   client.flush();
   ```

   This flushes all events from the SDK to Statsig. **Without the flush call, 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](https://docs.statsig.com/server-core/node-core#client-sdk-bootstrapping-%7C-ssr).

### 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. Statsig has tested and optimized this model for AWS Lambda/Lambda@Edge and S3, but it's 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.

