> ## 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.

# AWS CloudFront

> Integrate with AWS CloudFront to send analytics data to Hall

Use AWS Lambda\@Edge to forward data to the [Log visit](api-reference/visit) endpoint.

AWS Lambda\@Edge allows you to run JavaScript (Node.js) code at AWS CloudFront edge locations, intercepting and modifying requests before they reach your origin server. Read the [official documentation](https://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html) for more details.

## Setup

1. Create a Lambda function in the `us-east-1` region (required for Lambda\@Edge).
2. Deploy the function to CloudFront as a viewer request or origin request trigger.
3. Set up the `HALL_API_KEY` environment variable with your [Hall API key](/authorization).

## Example

The example below demonstrates how to forward data from incoming requests using AWS Lambda\@Edge.

```js index.js theme={null}
exports.handler = async (event) => {
    const request = event.Records[0].cf.request;
    const headers = request.headers;
    
    const requestPath = request.uri + (request.querystring ? '?' + request.querystring : '');
    const requestMethod = request.method;
    const requestIp = headers['cloudfront-viewer-address'] 
        ? headers['cloudfront-viewer-address'][0].value.split(':')[0] 
        : '127.0.0.1';
    
    const requestHeaders = {
        'User-Agent': headers['user-agent'] ? headers['user-agent'][0].value : '',
        'Host': headers['host'] ? headers['host'][0].value : '',
        'Referer': headers['referer'] ? headers['referer'][0].value : '',
    };

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

    return request;
};
```

## Deployment

To deploy this function to Lambda\@Edge, follow the [AWS Lambda@Edge deployment guide](https://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html) in the AWS documentation.
