A small AWS Lambda that forwards requests to the Anthropic API. Use it when your server can't reach api.anthropic.com directly, for example because of where it's hosted.
The relay checks a shared token, passes the request through unchanged and streams the response back, so streaming messages keep working.
your app ──► Lambda Function URL (allowed region) ──► api.anthropic.com
x-relay-token: <token>
x-api-key: <your Anthropic key>
- Requests without a valid
x-relay-tokenget403. - Only
x-api-key,Authorization,Content-Type,Acceptandanthropic-*headers (such asanthropic-versionandanthropic-beta) go upstream. The relay token is never forwarded. - The base URL works with or without
/v1. - The relay doesn't store or log request bodies.
- Create a Lambda function:
- runtime: Node.js 24 (
nodejs24.x) - handler:
index.handler - memory: 256 MB
- timeout: 120 s
- runtime: Node.js 24 (
- Upload the code:
npm run package aws lambda update-function-code --function-name anthropic-relay --zip-file fileb://function.zip
- Add a Function URL with auth type
NONEand invoke modeRESPONSE_STREAM. - Set the tokens (see below).
| Variable | Meaning |
|---|---|
RELAY_TOKENS |
Comma-separated list of accepted tokens |
RELAY_TOKEN |
A single token, kept for older setups |
Give each client its own token. That way you can revoke one client by removing its token, and nobody else is affected. To generate a token:
openssl rand -hex 32Point the Anthropic base URL at the Function URL and send the token in x-relay-token.
Python SDK:
from anthropic import Anthropic
client = Anthropic(
base_url="https://<id>.lambda-url.<region>.on.aws",
default_headers={"x-relay-token": RELAY_TOKEN},
)Plain HTTP:
curl https://<id>.lambda-url.<region>.on.aws/v1/messages \
-H "x-relay-token: $RELAY_TOKEN" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model": "claude-haiku-5-5", "max_tokens": 64, "messages": [{"role": "user", "content": "Hi"}]}'- Requests are capped at 6 MB by Lambda. Large images or documents in base64 can exceed that.
- Responses can be up to 200 MB when streamed.
- A request can't run longer than the function timeout. Long generations need streaming or a higher timeout.
The Function URL is public. Anyone who knows it can call it, but without a valid token they only get 403. Keep the URL and the tokens out of public repos and logs.
npm testTests run against a mocked Lambda runtime and fetch, with no network calls.
MIT