Next.js ships with API routes which provides an easy solution to build your own API. This example shows how to create multiple API endpoints with serverless functions, which can execute independently.
This project is configured to use Vercel Trace Drains with OpenTelemetry. To enable tracing:
- Go to Vercel Dashboard > Team Settings > Drains
- Click Create Drain
- Select Traces as the data type
- Choose your project(s)
- Configure the destination endpoint (your OpenTelemetry collector or observability platform)
- Set the sampling rate if needed
In your Vercel project settings, add the following environment variable:
OTEL_TRACES_EXPORTER=otlp(Required - tells OpenTelemetry to use the OTLP exporter)
The OTEL_EXPORTER_OTLP_ENDPOINT is automatically set by Vercel when a Trace Drain is configured, so you don't need to set it manually.
The instrumentation.ts file is already configured with @vercel/otel. After setting the environment variable and deploying, traces should automatically be sent to your configured Trace Drain.
If you've set OTEL_TRACES_EXPORTER=otlp but still don't see traces, check the following:
-
Verify Trace Drain is Active:
- Go to Vercel Dashboard > Team Settings > Drains
- Ensure your Trace Drain shows as "Active"
- Verify it's assigned to the correct project
-
Check Environment Variables:
- In Vercel Dashboard > Project Settings > Environment Variables
- Verify
OTEL_TRACES_EXPORTER=otlpis set for all environments (Production, Preview, Development) - Important: After adding/changing environment variables, you MUST redeploy
-
Redeploy After Changes:
- Any changes to environment variables or
instrumentation.tsrequire a new deployment - Local development won't send traces to Trace Drains (only deployed environments)
- Any changes to environment variables or
-
Check Function Logs:
- In Vercel Dashboard, go to your project > Functions tab
- Look for any OpenTelemetry-related errors
- Check for connection errors to your Trace Drain endpoint
-
Verify Next.js Configuration:
- Ensure
experimental.instrumentationHook: trueis innext.config.js(already configured) - The
instrumentation.tsfile must be in the project root
- Ensure
-
Test with API Routes:
- Make requests to your API routes (e.g.,
/api/people,/api/divide) - Traces are generated automatically for API routes when using
@vercel/otel - Check your Trace Drain destination to see if traces arrive
- Make requests to your API routes (e.g.,
-
Sampling Rate:
- Check your Trace Drain settings for sampling rules
- During testing, set sampling to 100% to ensure all traces are captured
-
Destination Endpoint:
- Verify your Trace Drain destination endpoint is accessible
- Test the endpoint connectivity from Vercel's infrastructure
Deploy the example using Vercel or preview live with StackBlitz
Execute create-next-app with npm, Yarn, or pnpm to bootstrap the example:
npx create-next-app --example api-routes api-routes-appyarn create next-app --example api-routes api-routes-apppnpm create next-app --example api-routes api-routes-appDeploy it to the cloud with Vercel (Documentation).
Run:
npm run build
OTEL_TRACES_EXPORTER=otlp,console OTEL_LOG_LEVEL=debug OTEL_METRICS_EXPORTER=none OTEL_EXPORTER_OTLP_HEADERS=`Authorization=5xrocjh0p5ir233mvi34dvl5bepnyqri3rqb1` OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf OTEL_RESOURCE_ATTRIBUTES='mw.account_key=5xrocjh0p5ir233mvi34dvl5bepnyqri3rqb1' MW_API_KEY=5xrocjh0p5ir233mvi34dvl5bepnyqri3rqb1 OTEL_LOGS_EXPORTER=otlp npm run dev