- Statsig automatically pushes project changes to Fastly KV/Config Store, providing low-latency SDK startup
- Statsig offers 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 FastlyYou will need to 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
- Create an Automation Token with:
- Store Type - Select “Config Store” or “KV Store” depending on your storage type.
- Config Store ID OR KV Store ID- Your Store ID
statsig-. This is the key associated with your Statsig config specs in your Store. Note this key down as it will be required later.2
3
4
Use the SDK
-
handlerThis is your Fastly Compute code. -
params : StatsigFastlyHandlerParamsFor best practice: storeParameter Optional Type Description statsigSdkKeyNo stringYour Statsig client API key fastlyStoreTypeNo stringEither kvorconfig, signifying your Fastly store typestoreIdNo stringYour KV or Config store id keyIdNo stringThey key storing your Statsig config specs in your Fastly store apiTokenNo stringYour Fastly API token used to authenticate requests to the Fastly API statsigOptionsYes StatsigOptionsSee StatsigOptions here statsigSdkKeyandapiTokenin a Fastly Secret Store
Example Usage
index.js
- Initializes the Statsig Client with config specs from your KV or Config store
- Executes your handler code (Your business logic + Statsig usage)
- Flushes all events after your handler completes execution
- Cleans up resources
Advanced Usage
Advanced/manual usage
Advanced/manual usage
Use the advanced/manual setup if:Creating a
The client instantiation takes two arguments:The client initialization takes four arguments:This is a gate check in code.The This is an event log in code.The This flushes all events from the sdk to Statsig. Without this, you wont be able to get diagnostic information in the Statsig Console, nor any event data you logged.
- You need fine-grained control over initialization timing
- You need fine-grained control over event flushing timing
- You need to customize error handling behavior
Prerequisites
- Completed the Statsig Fastly integration setup
1
2
3
Creating a StatsigFastlyClient instance
sdkKey : stringThis is your Statsig client API key. It is available from the Project Settings page in the Statsig Console. This is used to authenticate your requests.options : StatsigOptionsSee here, for more options.
sdkKey in a Fastly Secret Store4
Client initialization
The following line initializes the client by loading feature gate and experiment configurations directly from your Fastly KV or Config store.fastlyStoreType : stringThis is the Fastly store type you are using represented bykvorconfigstoreId : stringThe id of your Fastly storekeyId : stringThis is they key storing the Statsig config specs in your storeapiToken : stringYour Fastly API Token
apiToken in a Fastly Secret Store5
Checking a Gate
checkGate method takes two arguments:name : stringThe name of the Statsig gate that you are checking.user : StatsigUserThe Statsig user object for whom the gate is being checked. For more information on the user object, see here.
6
Logging an event
logEvent method takes two parameters:eventOrName : string | StatsigEventThis is the name and details of the event you are logging.user : StatsigUserThe Statsig user object for whom the event is being logged.value : stringA value you would like to associate with this event
7
Flushing Events
Putting it all together
Other Considerations
Polling for updates v5.13.0+
The SDK cannot poll for updates across requests since Fastly does not allow for timers.To optimize for edge use cases, we do not provide an api to recognize updates to your config specs. However, when a change is made to your project definition on the Statsig console, the changes will be propagates to the KV/Config store and will be reflected the next time you initialize the StatsigFastly client.Flushing events v4.16.0+
The SDK enqueues logged events and flushes them in batches. In order to ensure events are properly flushed, we recommend calling flush usingevent.waitUntil(). This will keep the request handler alive until events are flushed without blocking the response.