API Routes

Full request lifecycle logging with correlation ID propagation in Next.js API Routes.

NNode.jsServer
GET Request
Fetches mock user data with full request tracing
POST Request with PII
Sends sensitive data to the API (watch how it's sanitized in logs)
! This request includes: email, password, and creditCard fields. Check the log panel to see them sanitized
Request Body (sent to API)
{
  "name": "Test User",
  "email": "test@example.com",
  "password": "secret123",
  "creditCard": "4111111111111111"
}
Code Example
How to use vestig in API Routes
typescript
import { serverLogger } from '@/lib/logger'
import { withContext, createCorrelationContext } from 'vestig'

const log = serverLogger.child('api:users')

export async function GET(request: Request) {
  const ctx = createCorrelationContext()

  return withContext(ctx, async () => {
    log.info('API request received', {
      method: 'GET',
      requestId: ctx.requestId,
    })

    const data = await fetchData()

    log.info('API response sent', {
      status: 200,
      itemCount: data.length,
    })

    return Response.json(data, {
      headers: {
        'X-Request-Id': ctx.requestId!,
      },
    })
  })
}

Key Features

  • Request Lifecycle — Full tracing from request to response
  • Correlation IDs — Request ID and Trace ID propagation
  • Response Headers — IDs returned for client-side tracing
  • Duration Tracking — Performance metrics logged automatically
  • Error Handling — Errors logged with context preserved