> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/web-application-and-api-protection-waap/data-collection/gateways-load-balancers/cloudflare-workers.md).

# Cloudflare workers

Cloudflare Workers is a serverless computing platform that enables developers to deploy and execute JavaScript code at the edge of Cloudflare's global network. Cloudflare Workers allow developers to write serverless functions that run directly on Cloudflare's edge servers. Cloudflare Workers can build various applications, including serverless APIs, dynamic websites, and real-time applications. They are also highly scalable and can handle large volumes of traffic. Traceable integrates with Cloudflare workers to collect data for further analysis. The following is a high-level architecture diagram:

<img src="/files/Iq6dIVjZsknGUdTBajfq" alt="" width="700">

Traceable Cloudflare worker captures the data from Cloudflare and sends it to the Traceable Platform agent. Note that the request from the Cloudflare worker to the Traceable Platform agent does not block the user request.

> **Note**
>
> Traceable’s Cloudflare worker intergrations is applicable only when Cloudflare is used as a proxy. If Cloudflare is configured as DNS only option, then the network traffic bypasses Cloudflare network including workers and hence not captured by Traceable.

***

## Before you begin

Make sure to take note of the following points before proceeding with the configuration:

* Ensure that a website is available through Cloudflare. In Cloudflare, a website refers to a web application or a set of web pages hosted on a server and accessed by users over the internet.
* Download Traceable's JavaScript code for Cloudflare from Traceable's [download](https://downloads.traceable.ai/#agent/cloudflare/latest/) site. Navigate to **agent → cloudflare → latest**.
* Ensure the Traceable platform agent (TPA) is installed, and you have its URL. For more installation information, see [Platform agent](https://docs.traceable.ai/docs/tpa).
* Ensure the connectivity between the Cloudflare worker and the TPA server to export data to TPA.

  <div data-gb-custom-block data-tag="hint" data-style="warning" class="hint hint-warning"><p>The Traceable Platform Agent must:</p><ul><li>Be enabled for TLS with port 443 available.</li><li>Use a signed CA certificate. Self-signed certificates and non-standard TLS ports are not supported for CloudFlare Workers subrequests.</li></ul></div>

***

## Configuration

Configuring a Cloudflare Worker for Traceable consists of two critical steps:

1. **Creating a Worker —** This step involves deploying a dedicated Cloudflare Worker that integrates Traceable's JavaScript code. The worker is an intermediary that captures and forwards API traffic for analysis, ensuring Traceable can effectively monitor and secure your application traffic.
2. **Enabling Workers on a Website**—Once the worker is created, it must be associated with your website through worker routes. This ensures the worker is active for the desired traffic patterns, enabling Traceable to collect and analyze API requests seamlessly.

### Step 1 – Create a worker

1. Log in to your Cloudflare account.
2. Click on the **Worker & Pages** menu and click **Get started** to create a new worker.

   <img src="/files/c0n2UPgmuJ8vlwMcOyrA" alt="" width="900">
3. Provide a unique worker name and click **Deploy**.

   <img src="/files/sNpPf7jquWTB0tLDYjng" alt="" width="900">
4. After a successful deployment, click on the **Edit Code** button.

   <img src="/files/oWp5nvpxAtmwBAEngNBp" alt="" width="900">
5. Copy the JavaScript code that you downloaded as part of the **Before you begin** steps and replace the existing JavaScript. Ensure that you configure the Traceable Platform agent as shown below. You can optionally also configure the service name.

   <img src="/files/JV08uZhzUf9P6szmd3L2" alt="" width="900">
6. Click **Deploy**.

***

### Step 2 – Enable the worker on a website

Complete the following steps to enable the worker on a website:

1. Click on the Website menu and select the Website for which you wish to enable the worker.
2. On the Website Overview page, click on the \*\*Worker Routes \*\*menu.
3. On the **Worker Routes** page, click on **Add Route**. Configure the Route, service, and environment and click on **Save**.

   <img src="/files/akYsS0n9EsKgH0OMn1XZ" alt="" width="700">

   1. If you wish to capture traffic on every subdomain and all the paths of your Website, use \*your-webiste.com/\*
   2. If you wish to capture traffic for a specific domain, use [www.your-website.com/\*](https://www.your-website.com/*)
   3. If you wish to capture traffic for a subset of paths, you can use [your-website.com/some-path/\*](https://your-website.com/some-path/*)

***

## Verification and troubleshooting

Send traffic through your domain. Log in to the Traceable Platform to view the traffic. Complete the following steps:

1. Navigate to\*\* Catalog → API Endpoints\*\*.
2. Click on the Cloudflare API. On the API details page, click on the **Traces** tab.
3. Click on a trace to expand the trace details.
4. Click on the **Attributes** tab to verify the configuration's success.

If the request and response are not captured, check the Content-Type header. Request and response bodies are captured only if a Content-Type header is present and includes either `json` or `xml` as a substring. You can configure additional types by modifying the `capture_content_types` configuration options of the Worker script mentioned above.

```language-javascript
INSTALL_OPTIONS = {
        "tpa_address": "https://REPLACE_WITH_TPA_HOST",
        "enable_debug": false,
        "service_name": "website",
        "capture_content_types": ["json", "xml", "grpc"]
    };
```

***

## Remove routes and service

If you wish to remove routes and services, complete the following steps:

#### Remove routes

1. Navigate to the **Worker Routes** page. Under **HTTP Routes**, click the Edit link for the route you wish to remove.

   <img src="/files/TqGt3nzrec93NrdMinOH" alt="" width="600">
2. Select the service and environment and click on **Remove**.

#### Remove a service

1. Navigate to Cloudflare Dashboard and click on **Workers**.
2. On the \*\*Workers \*\*page, select the service that you wish to remove.
3. On the service page, click on the \*\*Manage Service \*\*button.
4. On the manage settings page, click \*\*Delete \*\*under the Delete Service section.
