export const handler = async (event, context, callback) => {
const request = event.Records[0].cf.request;
request.headers.host[0].value = "api.statsig.com";
// Rewrite custom endpoint to Statsig endpoint
// Replace 'my-product-data' with your custom endpoint name
if (request.uri.includes('/v1/my-product-data')) {
request.uri = request.uri.replace('/v1/my-product-data', '/v1/log_event');
}
return callback(null, request);
};
```python
- In Settings,
- Add an Alternate domain name (CNAME) to be your preferred domain name to use for the custom proxy, e.g. `statsig.example.com`.
- Add a Custom SSL certificate. You will need to follow the [AWS guide for Alternate domain name](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CNAMEs.html#alternate-domain-names-requirements) to add a SSL certificate.
- Click on Create distribution.
<Frame>
<img src="/images/infrastructure/api_proxy/custom_proxy/178337890-828e9f37-dd28-43a3-adc1-061052916045.png" alt="Settings" />
</Frame>
- You will get a Distribution domain name (e.g. `d111111abcdef8.cloudfront.net`) once it is provisioned.
In your DNS settings (depending on your DNS provider),
- Add a CNAME record in your custom DNS record:
- Host name: `statsig.example.com`
- Type: `CNAME`
- Data: `d111111abcdef8.cloudfront.net` (The Distribution domain name from AWS)
- Your proxy should now be setup. See [Using Your Proxy](#using-your-proxy) for instructions on how to configure your Statsig SDK.
### Cloudflare Worker
#### Prerequisites
You will need a Cloudflare account. Visit [https://www.cloudflare.com](https://www.cloudflare.com/) to set one up.
#### Setup
Once you are logged into Cloudflare. You can follow these steps:
1. Navigate to "Workers & Pages > Overview" in the left rail to create a new worker.
<Frame>
<img src="/images/infrastructure/api_proxy/custom_proxy/39bcd1ad-ddcc-4be9-9d71-905ed6a90b8b.png" alt="1-cloudflare-create" />
</Frame>
<Note>
You may see a different experience if you already have workers on your account.
</Note>
3. Name you new worker whatever you would like and then click "Deploy".
<Frame>
<img src="/images/infrastructure/api_proxy/custom_proxy/9d728e12-675e-4648-b6ee-ce84c72b305c.png" alt="2-cloudflare-deploy" />
</Frame>
4. Once deployed, click "Edit Code".
<Frame>
<img src="/images/infrastructure/api_proxy/custom_proxy/8c971a58-5bb7-4faa-a7ba-4574ab29f0ce.png" alt="3-cloudflare-edit-code" />
</Frame>
5. Copy and paste the following snippet into the `worker.js` file, then hit "Deploy". This example rewrites a custom endpoint `/v1/my-product-data` to the Statsig endpoint `/v1/log_event`:
```javascript
export default {
async fetch(request, _env, _ctx) {
const url = new URL(request.url);
// Rewrite custom endpoint to Statsig endpoint
// Replace 'my-product-data' with your custom endpoint name
let pathname = url.pathname;
if (pathname.includes('/v1/my-product-data')) {
pathname = pathname.replace('/v1/my-product-data', '/v1/log_event');
}
const original = new Request(request);
original.headers.delete("cookie");
return fetch(
`https://statsigapi.net${pathname}${url.search}`,
original
);
},
};
```text
<Frame>
<img src="/images/infrastructure/api_proxy/custom_proxy/558498dd-159f-409e-acef-a31e0dff86c2.png" alt="4-cloudflare-paste-snippet" />
</Frame>
6. Your worker should now be deployed and ready to use. See [Using Your Proxy](#using-your-proxy) for instructions on how to configure your Statsig SDK.
## Using Your Proxy
Once you have a proxy setup, you will need to take its URL and apply it to the SDK. To do this, you can use `StatsigOptions.networkConfig.api`. You can visit [Statsig Options](/client/javascript-sdk#statsig-options) to read about the Javascript specific StatsigOptions, but all SDKs have the ability to override the api via `StatsigOptions.networkConfig.api`.
The following is pseudo code of what initializing with a proxy looks like:
```typescript
Statsig.initialize(mySdkKey, myUser, {
networkConfig: {
api: "https://my-statsig-proxy.com/v1",
},
});
```ruby
<Note>
Depending on the SDK type, version, and proxy approach you are using, you may not need to append `'/v1'` to the end of your api string. eg `"https://my-statsig-proxy.com/"`
</Note>
### Configuring Custom Endpoints
If you've configured your proxy to use custom endpoint names (recommended to avoid tracking blockers), you'll need to configure the SDK to use those custom endpoints. The SDK will append the endpoint path to your base URL.
For example, if your proxy rewrites `/v1/my-product-data` to `/v1/log_event`, you would configure:
```typescript
Statsig.initialize(mySdkKey, myUser, {
networkConfig: {
api: "https://my-statsig-proxy.com/v1",
logEventUrl: "https://my-statsig-proxy.com/v1/my-product-data",
},
});