Anthropic API Proxy Server Setup Guide
If you're building anything beyond a personal script with the Anthropic API, you'll eventually hit the same wall: your ANTHROPIC_API_KEY is sitting in client code, a CI pipeline, or a shared .env file, and you have no way to control who uses it, how much they use, or what happens when it leaks. The fix is a proxy server — a thin HTTPS layer that sits between your application and api.anthropic.com, forwarding requests while adding the controls Anthropic's raw API doesn't give you out of the box.
This guide walks through what an Anthropic API proxy actually needs to do, how to build one yourself, and when it makes more sense to use a hosted option instead of maintaining your own.
Why put a proxy in front of the Anthropic API
The Anthropic API is designed for direct, trusted server-to-server use. It doesn't have built-in concepts like per-application keys, per-user rate limits, or team-level usage dashboards. Once you have more than one app, more than one developer, or any client-side calls, you need a layer that handles:
- Key isolation — your real Anthropic key never leaves your infrastructure
- Per-app or per-user credentials — so you can revoke one integration without breaking others
- Rate limiting and quotas — to stop one buggy script from burning your whole budget
- Logging and usage metadata — token counts, latency, error rates per key
- A stable interface — so you can switch underlying providers or models without touching every client
A proxy server solves all of this by acting as the single point of contact with Anthropic, while everything downstream talks to your proxy instead.
Minimal proxy setup (self-hosted)
The simplest version is a small Node.js (or Python, Go — anything with HTTP) service that accepts requests, validates a caller-supplied key, and forwards to Anthropic with your real key attached server-side.
// server.js - minimal Anthropic API proxy
import express from "express";
import fetch from "node-fetch";
const app = express();
app.use(express.json());
const VALID_KEYS = new Set(["app1_key_abc", "app2_key_def"]);
app.post("/v1/messages", async (req, res) => {
const authHeader = req.headers["authorization"] || "";
const token = authHeader.replace("Bearer ", "");
if (!VALID_KEYS.has(token)) {
return res.status(401).json({ error: "Invalid proxy key" });
}
const response = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"content-type": "application/json",
"x-api-key": process.env.ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01",
},
body: JSON.stringify(req.body),
});
const data = await response.json();
res.status(response.status).json(data);
});
app.listen(3000, () => console.log("Proxy running on port 3000"));
This covers the absolute basics: clients authenticate against your proxy, not Anthropic, and your real key stays in an environment variable on the server.
What this minimal setup is missing
The example above is a starting point, not production-ready infrastructure. Before you rely on it, you need to add:
- Streaming support — Claude's
stream: trueresponses are Server-Sent Events, which need to be piped through, not buffered and re-serialized - Rate limiting — per-key request and token limits, usually with a sliding window or token-bucket algorithm
- Retry and timeout handling — Anthropic's API can return 429s and transient 5xxs that need backoff logic
- Usage logging — capturing
usage.input_tokensandusage.output_tokensfrom responses so you can bill or budget per key - Key rotation — a way to issue, revoke, and rotate caller keys without redeploying the proxy
- TLS termination — if you're running this outside a managed platform, you need certificates and HTTPS configured properly
Each of these is solvable, but together they add up to a real piece of infrastructure you now own, monitor, and patch.
Adding tool use and multi-turn conversations
If your application uses Claude's tool calling, your proxy needs to pass through the tools and tool_choice fields unchanged, and correctly relay tool_use and tool_result content blocks on subsequent turns. This is a common place where hand-rolled proxies break: developers forward the messages array but mangle content block types during JSON re-serialization, especially with nested objects or base64 image data. Test tool-use flows explicitly, not just plain text completions.
Securing the proxy itself
A proxy that forwards to Anthropic with a hardcoded server key is still a single point of failure. At minimum:
- Store the Anthropic key in a secrets manager, not plaintext environment files, in production
- Use distinct per-application keys at the proxy layer so you can revoke access individually
- Log every request with caller identity, timestamp, and token usage — not request bodies, which may contain sensitive prompt content
- Put the proxy behind your own authentication (mTLS, signed JWTs, or simple bearer tokens depending on your threat model)
When to use a hosted proxy instead
Building and maintaining this yourself makes sense if you have very specific routing logic or need to run entirely inside your own VPC. For most teams, though, the list above — streaming, retries, per-key quotas, usage dashboards, team seats — is exactly what a hosted proxy already provides without the maintenance burden.
SubToAPI is built specifically for this: you connect your existing Claude access once, and it exposes a clean HTTPS API with sub_live_... application keys, streaming support, tool use, usage metadata, and team seat management in one dashboard. Instead of writing and operating the proxy described above, you issue a scoped key per app from the dashboard and point your existing Anthropic-style request code at it.
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Summarize this proxy setup."}]
}'
That request behaves the same as a direct Anthropic call, but the key is revocable per application, usage is tracked automatically, and streaming and tool use work without extra plumbing. See the quickstart, messages, streaming, and tools docs for the full request/response shapes, and check pricing for the Solo, Team, and Scale plans. You can try it with a free trial at signup.
Choosing your approach
If you're a solo developer with one app and low traffic, the minimal proxy above might genuinely be enough — just be honest with yourself about the gaps in streaming and error handling. If you have multiple apps, multiple developers, or need usage visibility and team access control, either invest properly in the production checklist above, or skip the maintenance entirely with a hosted proxy that already handles it.
Questions
Do I need a proxy if I only have one app calling the Anthropic API? Not strictly — a single trusted server-side app can call Anthropic directly with its key in an environment variable. A proxy becomes valuable once you have multiple apps, client-side calls, or need per-key usage tracking.
Can a proxy server support streaming responses? Yes, but it requires piping Server-Sent Events through without buffering the full response first. This is one of the most commonly broken features in hand-built proxies.
Is a proxy the same as a rate limiter? No. Rate limiting is one feature a proxy can implement, but a full proxy also handles authentication, key isolation, logging, and request forwarding — rate limiting alone doesn't give you those.