This project demonstrates how to integrate Flagship feature flags with Fastly Compute@Edge, enabling feature flagging and A/B testing at the edge.
This example shows how to:
- Initialize the Flagship SDK in a Fastly Compute@Edge environment
- Use KV storage for caching bucketing data to improve performance
- Extract visitor context from request headers
- Fetch and apply feature flags for each visitor
- Send analytics data back to Flagship
The edge application performs the following operations:
- Retrieves Flagship credentials from the Fastly Secret Store
- Loads cached bucketing data from Fastly KV storage
- Initializes the Flagship SDK in decision API mode
- Creates a visitor with context data from request headers
- Fetches feature flags for the visitor
- Flushes analytics data back to Flagship
- Returns flag values in a JSON response
- Node.js (v18 or later)
- Yarn (v4 or later)
- A Fastly account with Compute@Edge access
- A Flagship account with API credentials
- Clone this repository
- Install dependencies:
yarn install- Create a FLAGSHIP_CONFIG.json file in your project root with your credentials:
{
"envId": "your-env-id",
"apiKey": "your-api-key"
}- For production deployment, create a secret store with your Flagship configuration:
# Create a secret store
fastly compute secret-store create --name=MY_APP_SECRET
# Add your configuration as a secret
fastly compute secret create --store=MY_APP_SECRET --name=FLAGSHIP_CONFIG --file=./FLAGSHIP_CONFIG.json- Update the fastly.toml file with your service configuration
This project uses Fastly's Secret Store to securely manage Flagship credentials. The implementation:
- Retrieves a JSON configuration containing both environment ID and API key as a single secret
- Uses this approach to minimize secret reads (Fastly limits to 5 secret reads per request)
- Configures a local secret store for development in fastly.toml
For local development, the configuration is read from your local FLAGSHIP_CONFIG.json file:
[local_server.secret_stores]
[[local_server.secret_stores.MY_APP_SECRET]]
key = "FLAGSHIP_CONFIG"
file = "./FLAGSHIP_CONFIG.json"Bucketing data contains information about your Flagship campaigns and variations, allowing the edge application to make flag decisions without calling the Flagship API for every request.
- Fetch bucketing data directly from the Flagship CDN:
# Replace YOUR_ENV_ID with your Flagship Environment ID
curl -s https://cdn.flagship.io/YOUR_ENV_ID/bucketing.json > bucketing-data.json- Configure your local development environment to use this data by adding the following to your fastly.toml file:
[local_server.kv_stores]
[[local_server.kv_stores.MY_APP_KV]]
key = "initialBucketing"
file = "./bucketing-data.json"For direct integration, you'll need to:
- Fetch the bucketing data during your build process
- Save it as a JSON file in your project
- Import it directly in your edge application code
# During build/deployment:
curl -s https://cdn.flagship.io/YOUR_ENV_ID/bucketing.json > src/bucketing-data.jsonThen modify your code to import the data directly:
import bucketingData from './bucketing-data.json';
// Then in your initialization code:
await Flagship.start(FLAGSHIP_ENV_ID, FLAGSHIP_API_KEY, {
decisionMode: DecisionMode.BUCKETING_EDGE,
initialBucketing: bucketingData,
fetchNow: false,
logLevel: LogLevel.DEBUG,
});For production environments, there are two recommended approaches. Both require setting up webhooks in the Flagship platform that trigger your CI/CD pipeline when campaigns are updated. Find more details in the Flagship documentation.
For more details on managing KV stores, refer to the Fastly KV Store documentation.
Start a local development server:
yarn startBuild the project:
yarn buildDeploy to Fastly:
yarn deploy