Use Domain Proxy to relay events
Use a domain proxy to control the data that you send to Amplitude. This guide explains the basics of setting up a self-owned proxy service and using it with Amplitude SDKs.
This guide focuses on building a proxy to Amplitude’s HTTP V2 API (api2.amplitude.com). This configuration sends events directly from the proxy service to Amplitude.
Talk with your developer operations and information security teams before choosing and deploying a proxy server.
How Domain Proxy works
A proxy service relays requests between a client and another service, acting as an intermediary step. Building a proxy service for your Amplitude org lets you proxy requests to the Amplitude API through your own domain. For example, you can send requests on client browsers to your.domain.com/amplitude instead of directly to api2.amplitude.com. A proxy service relays the requests to the Amplitude platform.
You can integrate proxy services into existing API endpoints or build them as standalone services. Many cloud providers have tools for flexible and reliable proxy service configuration.
Sending data through a self-owned proxy service gives you more control over the data you collect and send to Amplitude. A proxy service can provide these benefits:
- Ability to toggle event flow to Amplitude.
- Self-owned audit logging of data.
- Easier debugging, filtering, and blocking of events.
- Anonymizing end-users. For example, remove originating IP address, location, userID, and more.
Proxy services and the JavaScript snippet
You can build a proxy that supports the loading of the Amplitude JavaScript Snippet, but Amplitude recommends that you bundle Amplitude into your production builds using the npm distribution.
Available services on major cloud providers
Most major cloud providers have services for developing and deploying scalable APIs. You can also use API services to set up outbound traffic to Amplitude. If you use a cloud provider to deploy API services, refer to its documentation to set up a proxy service:
- Amazon Web Services: API Gateway
- Microsoft Azure: API Management
- Google Cloud: API Gateway
Build a proxy solution
The example in this guide uses NGINX to build a proxy server. NGINX is an open-source proxy solution. If you already have a Node API server, you could consider using the Node SDK to pass events from your own endpoint to the main Amplitude event servers.
Set up an NGINX server
First, install NGINX for local development. Then, configure NGINX to proxy requests on a particular URL to Amplitude. This example nginx.conf file proxies requests from the /amplitude route to api2.amplitude.com:
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
location /amplitude {
proxy_pass https://api2.amplitude.com/;
}
}
}
Validation and deployment
After you create the configuration file, start and test your proxy. Using the Amplitude HTTP API, send the requests to your endpoint instead of Amplitude's endpoint.
The HTTP API uses a slightly different endpoint from the SDKs, so to test, you need to temporarily set proxy_pass to https://api2.amplitude.com/2/httpapi/.
This curl command tests your reverse proxy:
curl -X POST http://localhost:8888/amplitude -H "Content-Type: application/json" --data '{"api_key":"API_Key","events":[{"user_id":"12345", "event_type":"test_proxy_event", "time":1396381378123}]}
This call should return a 200 response code. In the web app, confirm that Amplitude received the event using User Lookup. After you confirm that the proxy works locally, deploy the configuration to a production server. Refer to the NGINX deployment guides for more help.
Configure the SDKs to work with alternate endpoints
After the proxy works correctly, configure your SDK. Amplitude's SDKs are open source and have built-in options to send events to your defined server endpoint.
The SDKs point to special endpoints for their custom payloads. Find the endpoint for your SDK in this table:
| SDK | Endpoint | Set server url |
|---|---|---|
| Amplitude-JavaScript | https://api.amplitude.com | Set the apiEndpoint option when initializing the SDK. |
| Amplitude-Node | https://api2.amplitude.com/2/httpapi | Set the serverUrl option when initializing the SDK. |
| Amplitude-Android | https://api2.amplitude.com/ | Use the setServerUrl function to configure the server URL. |
| Amplitude-Java | https://api2.amplitude.com/2/httpapi | Use the setServerUrl function to configure the server URL. |
| Amplitude-iOS | https://api2.amplitude.com/ | Use the setServerUrl function to configure the server URL. |
| Amplitude-TypeScript | https://api2.amplitude.com/2/httpapi | Set the serverUrl option when initializing the SDK. |
| Amplitude-Kotlin | https://api2.amplitude.com/2/httpapi | Set the serverUrl option when initializing the SDK. |
| Amplitude-Swift | https://api2.amplitude.com/2/httpapi | Set the serverUrl option when initializing the SDK. |
| Amplitude-Python | https://api2.amplitude.com/2/httpapi | Set the server_url option when initializing the SDK. |
| Amplitude-Go | https://api2.amplitude.com/2/httpapi | Set the serverUrl option when initializing the SDK. |
After you configure the SDK, you can send events through your proxy and see them logged in Amplitude.
Was this helpful?