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.
Install the Statsig SDK
bashnpm install @statsig/serverless-clientImport the Statsig SDK
bashimport {StatsigServerlessClient} from '@statsig/serverless-client'Create a StatsigServerlessClient instance
javascriptconst client = new StatsigServerlessClient(process.env.STATSIG_KEY)The client instantiation takes two arguments:
sdkKey : stringYour Statsig client API key. Available from the Project Settings page in the Statsig Console. Statsig uses this to authenticate requests.options : StatsigOptionsRefer to StatsigOptions for more options.
Client Initialization
This line initializes the client by loading feature gate and experiment configurations over the network.
javascriptconst init = await client.initializeAsync();Checking a Gate
javascriptconst GateResult = client.checkGate('pass_gate', user);The
checkGatemethod takes two arguments:name : stringThe name of the Statsig gate that you are checking.user : StatsigUserThe Statsig user object for whom the gate is checked. For more information on the user object, refer to StatsigUser.
Logging an Event
javascriptclient.logEvent('gate_check', { userID: randomUserId });The
logEventmethod takes two parameters:eventOrName : string | StatsigEventThe name and details of the event you are logging.user : StatsigUserThe Statsig user object for whom the event is logged.
- 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
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.
// 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
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.
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?