Skip to content

About

AWS Lambda relay to the Anthropic API for servers in blocked regions

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

anthropic-relay

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.

How it works

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-token get 403.
  • Only x-api-key, Authorization, Content-Type, Accept and anthropic-* headers (such as anthropic-version and anthropic-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.

Deploy

  1. Create a Lambda function:
    • runtime: Node.js 24 (nodejs24.x)
    • handler: index.handler
    • memory: 256 MB
    • timeout: 120 s
  2. Upload the code:
    npm run package
    aws lambda update-function-code --function-name anthropic-relay --zip-file fileb://function.zip
  3. Add a Function URL with auth type NONE and invoke mode RESPONSE_STREAM.
  4. Set the tokens (see below).

Tokens

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 32

Client setup

Point 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"}]}'

Limits

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

Security

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.

Development

npm test

Tests run against a mocked Lambda runtime and fetch, with no network calls.

License

MIT

About

AWS Lambda relay to the Anthropic API for servers in blocked regions

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages