Star us on GitHub
Star
Menu

Cloudflare Workers

Learn how to set up highlight.io in Cloudflare Workers.
1
Set up your frontend 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', 'example.myapp.com/backend'], networkRecording: { enabled: true, recordHeadersAndBody: true, }, });
Copy
2
Install the Highlight JS SDK.

Install the @highlight-run/cloudflare package 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
Copy
3
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 } }, }
Copy
4
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!')) }, }
Copy
5
Verify your backend logs are being recorded.

With the JS SDKs, your logs are reported automatically from console methods. Visit the highlight logs portal and check that backend logs are coming in.