← Blog

Anthropic API Proxy Server Setup: A Practical Guide

2026-11-18 · 5 min read · SubToAPI Team

If you're building anything beyond a local script with Claude, you shouldn't call the Anthropic API directly from a browser, mobile app, or any client the public can inspect. An Anthropic API proxy server sits between your application and Anthropic's endpoints, holding the real API key server-side while your clients talk to a URL you control. This guide walks through why that setup matters and how to build one.

The short answer to "how do I set up a proxy for the Anthropic API": stand up a lightweight backend (Node, Python, Go — anything that can make HTTP requests), store your ANTHROPIC_API_KEY as a server-side environment variable, expose your own endpoint that your frontend calls, and have that endpoint forward the request to api.anthropic.com with the key attached. Add auth, logging, and rate limiting on your side. Below is a working version of that, plus what to watch out for with streaming and tool use, and when it makes more sense to use a managed proxy instead of maintaining your own.

Why You Need a Proxy in Front of the Anthropic API

Anthropic's API keys are bearer tokens with full account access. If you embed one in a mobile app binary, a browser extension, or client-side JavaScript, anyone can extract it with basic reverse-engineering tools and run up charges on your account. A proxy solves this by making sure the key never leaves your infrastructure.

Beyond security, a proxy gives you a single place to:

Minimal Proxy Server Setup (Node.js Example)

Here's a bare-bones Express proxy that forwards chat requests to the Anthropic Messages API:

import express from "express";

const app = express();
app.use(express.json());

app.post("/api/chat", async (req, res) => {
  const response = await fetch("https://api.anthropic.com/v1/messages", {
    method: "POST",
    headers: {
      "x-api-key": process.env.ANTHROPIC_API_KEY,
      "anthropic-version": "2023-06-01",
      "content-type": "application/json",
    },
    body: JSON.stringify({
      model: "claude-opus-4",
      max_tokens: 1024,
      messages: req.body.messages,
    }),
  });

  const data = await response.json();
  res.status(response.status).json(data);
});

app.listen(3000);

Your frontend never sees ANTHROPIC_API_KEY. It only knows about /api/chat, which lives on your own domain. Deploy this on any server, container platform, or serverless function — the pattern doesn't change.

Handling Streaming Responses Through Your Proxy

Most chat UIs need token-by-token streaming, which complicates a plain request/response proxy. You need to forward the upstream stream chunk-by-chunk instead of buffering the whole response:

app.post("/api/chat/stream", async (req, res) => {
  const upstream = await fetch("https://api.anthropic.com/v1/messages", {
    method: "POST",
    headers: {
      "x-api-key": process.env.ANTHROPIC_API_KEY,
      "anthropic-version": "2023-06-01",
      "content-type": "application/json",
    },
    body: JSON.stringify({ ...req.body, stream: true }),
  });

  res.setHeader("Content-Type", "text/event-stream");
  for await (const chunk of upstream.body) {
    res.write(chunk);
  }
  res.end();
});

This works, but it's also where a lot of hand-rolled proxies start to fall apart — you now own reconnect logic, partial-chunk handling, and keeping the SSE format intact end to end. It's not hard, but it's one more piece of infrastructure to maintain correctly.

Adding Authentication for Your Own Clients

Your proxy is useless as a security boundary if anyone can hit /api/chat without identifying themselves. At minimum, require a bearer token issued by your own system:

app.use((req, res, next) => {
  const token = req.headers.authorization?.replace("Bearer ", "");
  if (token !== getValidTokenForUser(req)) {
    return res.status(401).json({ error: "unauthorized" });
  }
  next();
});

From here you can layer on per-user quotas, team-based access, or different rate limits per plan tier — all logic that lives in your proxy, not in Anthropic's API.

Logging, Usage Tracking and Rate Limiting

Once requests flow through a single point, you can record what matters: tokens used per request, latency, error rates, and which user or API key triggered each call. A simple approach is to log the usage object Anthropic returns in each response and write it to your own database, then build rate limiting on top using a counter (Redis works well) keyed by user ID.

This is also the point where most teams realize they're rebuilding — token accounting, per-key limits, team seats, request logs — a fair amount of infrastructure just to make the Anthropic API safe and usable for their application.

When to Use a Managed Proxy Instead

If you're already comfortable maintaining the pieces above, a self-built proxy is fine. But if you'd rather skip streaming edge cases, usage dashboards, and key rotation plumbing, a managed layer like SubToAPI does this for you: it sits between your app and Claude, issues scoped application keys (sub_live_...) instead of exposing your raw Anthropic key, and handles streaming, tool use, and usage metadata out of the box.

Setup looks like this instead of writing your own forwarding server:

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-opus-4",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Hello"}]
  }'

You get team seats, per-key usage visibility, and streaming support (/docs/streaming) and tool use (/docs/tools) without writing the forwarding, logging, and auth layers yourself. Check /docs/quickstart to see the full request format, or /pricing for plan details — Solo, Team, and Scale tiers all start with a free trial at /signup.

Whether you build your own proxy or use a hosted one, the core principle is the same: your Anthropic API key should never reach a client you don't fully control.

Questions

Do I need a proxy if my app is server-side only (no browser or mobile client)? If your entire stack runs server-side and nothing user-facing calls the API directly, you can call Anthropic's API straight from your backend. A proxy becomes necessary once any untrusted client — browser, mobile app, third-party integration — needs to trigger Claude calls.

Can I run a proxy on serverless platforms like Vercel or Cloudflare Workers? Yes, but streaming responses need careful handling since some serverless runtimes buffer or time out long-lived connections differently than a traditional server. Test your streaming path specifically before relying on it in production.

Is a self-hosted proxy the same as using SubToAPI? No — SubToAPI is a managed API layer, not infrastructure you host yourself. It gives you scoped application keys, usage tracking, and team management on top of Claude access without you building or maintaining the forwarding server, auth, or logging described above.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →