This project demonstrates how to integrate Flagship feature flags with Vercel Edge Functions, enabling feature flagging and A/B testing at the edge.
This example shows how to:
- Initialize the Flagship SDK in a Vercel Edge Function
- Use Edge Config 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 Function performs the following operations:
- Retrieves Flagship credentials from environment variables
- Loads cached bucketing data from Vercel Edge Config
- Initializes the Flagship SDK in edge bucketing 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 Vercel account
- A Flagship account with API credentials
- Clone this repository:
git clone https://github.com/flagship-io/flagship-vercel-edge-function-example.git
cd flagship-vercel-edge-function-example
- Install dependencies:
yarn install
- Configure your Flagship credentials as environment variables:
Create or update .env.local file:
FLAGSHIP_ENV_ID=your_env_id
FLAGSHIP_API_KEY=your_api_key
- Create an Edge Config in the Vercel dashboard and add it to your project
npx vercel link
npx vercel env pull .env.local
Bucketing data contains information about your Flagship campaigns and variations, allowing the Edge Function 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
- Upload the bucketing data to your Vercel Edge Config:
Adding the bucketing to Edge config can be done using the Vercel dashboard or via the Vercel API.
Here’s how to do it via the API:
# Using Vercel API to upload the bucketing data
curl -X PATCH "https://api.vercel.com/v1/edge-config/ecfg_YOUR_EDGE_CONFIG_ID/items" \
-H "Authorization: Bearer YOUR_VERCEL_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"items": [
{
"operation": "create",
"key": "initialBucketing",
"value": '"$(cat bucketing-data.json | jq -c .)"'
}
]
}'
- Access this data in your code (already implemented):
import { get } from "@vercel/edge-config";
const initialBucketing = await get("initialBucketing");
For direct integration, you can:
- Fetch the bucketing data during your build process
- Save it as a JSON file in your project
- Import it directly in your Edge Function
# During build/deployment:
curl -s https://cdn.flagship.io/YOUR_ENV_ID/bucketing.json > src/bucketing-data.json
Then modify your code to import the file directly:
import bucketingData from './bucketing-data.json';
async function initFlagship(request: Request) {
// ...existing code...
// Use the imported bucketing data instead of Edge Config
await Flagship.start(FLAGSHIP_ENV_ID as string, FLAGSHIP_API_KEY as string, {
decisionMode: DecisionMode.BUCKETING_EDGE,
initialBucketing: bucketingData as BucketingDTO,
fetchNow: false,
logLevel: LogLevel.DEBUG,
});
// ...existing code...
}
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.
Start a local development server:
yarn dev
Test your Edge Function:
curl http://localhost:3000/api/hello
With a visitor ID:
curl http://localhost:3000/api/hello?visitorId=user123
Deploy to Vercel:
yarn build
vercel deploy --prod
The key file in this example is route.ts, which implements the Edge Function. It:
- Specifies
edge
runtime for Next.js - Initializes Flagship SDK with credentials and cached bucketing data
- Creates a visitor with context from request headers
- Fetches and returns feature flags