
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.

{% steps %}
{% step title="Install the Statsig SDK" %}
```bash
npm install @statsig/serverless-client
```
{% /step %}

{% step title="Import the Statsig SDK" %}
```bash
import {StatsigServerlessClient} from '@statsig/serverless-client'
```
{% /step %}

{% step title="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 this to authenticate requests.
* `options : StatsigOptions` Refer to [StatsigOptions](/client/javascript-sdk#statsig-options) for more options.
{% /step %}

{% step title="Client Initialization" %}
This line initializes the client by loading feature gate and experiment configurations over the network.

```javascript
const init = await client.initializeAsync();
```
{% /step %}

{% step title="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](/sdks/user#introduction-to-the-statsiguser-object).

Refer to the [JavaScript on-device evaluation SDK documentation](/client/jsOnDeviceEvaluationSDK) for how to check other entities like experiments and dynamic configs.
{% /step %}

{% step title="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](/client/jsOnDeviceEvaluationSDK#logging-an-event).
{% /step %}

{% step %}
```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**.
{% /step %}
{% /steps %}

### Putting it all together

```javascript index.js
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](/server-core/node-core#client-sdk-bootstrapping-|-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. 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.
