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

# Cloudflare

> Integrate with Cloudflare to send analytics data to Hall

Use Cloudflare Workers to forward data to the [Log visit](api-reference/visit) endpoint.

Cloudflare Workers allow you to run JavaScript code at the edge, intercepting and modifying requests before they reach your origin server. Read the [official documentation](https://developers.cloudflare.com/workers/) for more details.

## Setup

1. Set up the `HALL_API_KEY` environment variable with your [Hall API key](/authorization).
2. Create a new Worker in your [Cloudflare dashboard](https://dash.cloudflare.com) and configure it to run on your domain routes.

## Example

The example below demonstrates how to forward data from incoming requests using Cloudflare Workers.

```ts worker.ts theme={null}
export default {
  async fetch(request, env, ctx) {
    const url = new URL(request.url)
    const requestPath = url.pathname + url.search
    const requestMethod = request.method
    const requestIp = request.headers.get('cf-connecting-ip') || '127.0.0.1'

    const requestHeaders = {
      'User-Agent': request.headers.get('user-agent'),
      'Host': request.headers.get('host'),
      'Referer': request.headers.get('referer'),
    }

    ctx.waitUntil(
      fetch('https://analytics.usehall.com/visit', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          Authorization: `Bearer ${env.HALL_API_KEY}`,
        },
        body: JSON.stringify({
          request_path: requestPath,
          request_method: requestMethod,
          request_ip: requestIp,
          request_headers: requestHeaders,
          request_timestamp: Date.now()
        }),
      })
    )

    return fetch(request)
  },
}
```
