> ## Documentation Index
> Fetch the complete documentation index at: https://docs.appsignal.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cloudflare Workers

A Cloudflare Workers application can report errors to AppSignal using the [AppSignal for JavaScript](https://docs.appsignal.com/front-end.md) integration. It reports errors only, with no performance data, traces, or metrics.

## Module workers

A module worker receives its secrets and environment variables on the `env` argument of the `fetch` handler. Create the `Appsignal` instance inside the handler, where `env` is available.

## Set up

Add the package to your worker project:

```bash Bash theme={null}
npm install @appsignal/javascript
```

Store the Front-end API key for the app you want to report to, listed under the ["Push & deploy" section](https://appsignal.com/redirect-to/app?to=info) of that app's settings, as a worker secret:

```bash Bash theme={null}
wrangler secret put APPSIGNAL_API_KEY
```

> [!WARNING]
>
> A wrong key fails silently: the Push API responds with a `401`, `sendError` discards it, and you get no log line and no incident. Trigger an error after your first deploy and confirm it appears in AppSignal.

Create the instance in the `fetch` handler, and pass caught errors to `sendError`:

```javascript JavaScript theme={null}
import Appsignal from "@appsignal/javascript";

let appsignal;

export default {
  async fetch(request, env) {
    if (!appsignal) {
      appsignal = new Appsignal({ key: env.APPSIGNAL_API_KEY });
    }

    try {
      // Your worker logic
      return await handleRequest(request);
    } catch (error) {
      await appsignal.sendError(error);
      return new Response("Internal server error", { status: 500 });
    }
  },
};
```

Uncaught exceptions are [not reported automatically](https://docs.appsignal.com/front-end/error-handling.md#uncaught-exceptions), so catch errors in your worker and pass them to `sendError` yourself. For the options you can set on the instance, see the [configuration section](https://docs.appsignal.com/front-end/configuration.md). For the metadata you can add to an error, see the [error handling guide](https://docs.appsignal.com/front-end/error-handling.md).

## Further reading

Our blog post ["How to debug Cloudflare Workers with AppSignal"](https://blog.appsignal.com/2021/09/15/how-to-debug-cloudflare-workers-with-appsignal.html) walks through a worker project from scratch.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.