Star us on GitHub
Star
Menu

Cloudflare Worker SDK API Reference

Cloudflare Worker SDK

Highlight's Cloudflare Worker SDK lets you track your errors and responses in Cloudflare Workers with no impact on performance..

Just getting started?

Check out our getting started guide to get up and running quickly.

H.init

H.init() configures the highlight SDK and records console log methods. The session is inferred based on the incoming network request headers.

Method Parameters
import { H } from "@highlight-run/cloudflare"; export default { async fetch(request: Request, env: {}, ctx: ExecutionContext) { H.init(request, { HIGHLIGHT_PROJECT_ID: '<YOUR_PROJECT_ID>' }, ctx) // do something... console.log('hi!', {hello: 'world'}) return new Response('hello!') }, }
Copy

H.consumeError

H.consumeError() reports an error and its corresponding stack trace to Highlight. The session is inferred based on the incoming network request headers.

Method Parameters
import { H } from "@highlight-run/cloudflare"; export default { async fetch(request: Request, env: {}, ctx: ExecutionContext) { H.init(request, { HIGHLIGHT_PROJECT_ID: '<YOUR_PROJECT_ID>' }, ctx) try { // do something... return new Response('hello!') } catch (e: any) { H.consumeError(request, { HIGHLIGHT_PROJECT_ID: '<YOUR_PROJECT_ID>' }, ctx, e) throw e } }, }
Copy

H.sendResponse

H.sendResponse() traces a response from your backend. This allows tracking incoming and outgoing headers and bodies.

Method Parameters
import { H } from "@highlight-run/cloudflare"; export default { async fetch(request: Request, env: {}, ctx: ExecutionContext) { H.init(request, { HIGHLIGHT_PROJECT_ID: '<YOUR_PROJECT_ID>' }, ctx) const response = return new Response('hello!') H.sendResponse(request, { HIGHLIGHT_PROJECT_ID: '<YOUR_PROJECT_ID>' }, ctx, response) return response }, }
Copy

H.setAttributes

H.setAttributes() attached structured log attributes to all subsequent console methods. Repeat calls with the same key update the value.

Method Parameters
import { H } from "@highlight-run/cloudflare"; export default { async fetch(request: Request, env: {}, ctx: ExecutionContext) { H.init(request, { HIGHLIGHT_PROJECT_ID: '<YOUR_PROJECT_ID>' }, ctx) // do something... console.log('hi!', {hello: 'world'}) H.setAttributes({my: 'attribute', is: Math.random()}) console.warn('whoa') return new Response('hello!') }, }
Copy