Star us on GitHub

Cloudflare Workers

Learn how to set up in Cloudflare Workers.
Add `tracingOrigins` to your client Highlight snippet.

This backend SDK requires one of the Highlight frontend SDKs to be installed, so please make sure you've followed the fullstack mapping guide first.

H.init("<YOUR_PROJECT_ID>", { tracingOrigins: ['localhost', ''], networkRecording: { enabled: true, recordHeadersAndBody: true, }, });
Install the Highlight JS SDK.

Install @highlight-run/cloudflare with your package manager.

# with yarn yarn add @highlight-run/cloudflare # with pnpm pnpm add @highlight-run/cloudflare # with npm npm install @highlight-run/cloudflare
Add the Cloudflare Worker Highlight integration.

Use the Cloudflare Worker SDK in your response handler. The sendResponse method traces successful requests while consumeError reports exceptions. All Highlight data submission uses waitUntil to make sure that we have no impact on request handling performance.

import { H } from '@highlight-run/cloudflare' async function doRequest() { return new Response('hello!') } export default { async fetch(request: Request, env: {}, ctx: ExecutionContext) { const hEnv = { HIGHLIGHT_PROJECT_ID: 'YOUR_PROJECT_ID' } try { const response = await doRequest() H.sendResponse(request, hEnv, ctx, response) return response } catch (e: any) { H.consumeError(request, hEnv, ctx, e) throw e } }, }
Verify that your SDK is reporting errors.

You'll want to throw an exception in one of your cloudflare handlers. Access the API handler and make sure the error shows up in Highlight.

export default { async fetch(request: Request, env: {}, ctx: ExecutionContext) { H.consumeError(request, { HIGHLIGHT_PROJECT_ID: 'YOUR_PROJECT_ID' }, ctx, new Error('example error!')) }, }
Set up logging.

With the JS SDKs, your logs are reported automatically from console methods. See the JS logging setup guide for more details.