Anthropic from TypeScript / React Native

SDK examples · All languages and providers · API reference

The official @anthropic-ai/sdk package works unchanged: you set the base URL and pass the user's token as authToken. Your Anthropic key stays on the server; the app holds only a token that expires in one hour.

1. Set up the service

In the dashboard, create an app, add a service and store your Anthropic key in it:

claude  →  https://api.anthropic.com

Set the base URL without /v1: the Anthropic API path is /v1/messages, so a service that ends in /v1 would produce /v1/v1/messages.

2. Get a token for the user

The app asks MyAppToken for a token with its app ID and your identifier for the user. The token lasts one hour; rate limits and analytics are tied to that user.

const API = "https://api.myapptoken.com/api/v1";
const APP_ID = "<app-id>";
let current: { jwt: string; expiresAt: number } | null = null;

async function requestToken(path: string, init: RequestInit) {
  const res = await fetch(`${API}${path}`, { method: "POST", ...init });
  if (!res.ok) throw new Error(`${path} failed: ${res.status}`);
  const body = await res.json();               // { access_token, expires_in, … }
  current = { jwt: body.access_token, expiresAt: Date.now() + body.expires_in * 1000 };
  return current.jwt;
}

export async function sessionToken(userId: string): Promise<string> {
  if (current && Date.now() < current.expiresAt - 5 * 60_000) return current.jwt;
  if (current) {
    try {
      return await requestToken("/auth/refresh",
        { headers: { Authorization: `Bearer ${current.jwt}` } });
    } catch { current = null; }                  // expired: fall through and mint anew
  }
  return requestToken("/auth/simple", {
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ app_id: APP_ID, user_id: userId }),
  });
}

With Firebase App Check enabled, also send the App Check header on proxy calls.

3. Call Anthropic

import Anthropic from "@anthropic-ai/sdk";

const PROXY = "https://api.myapptoken.com/api/v1/proxy/<app-id>";
const jwt = await sessionToken(userId);

// Before: new Anthropic({ apiKey: "sk-ant-…" })
const client = new Anthropic({ authToken: jwt, apiKey: null, baseURL: `${PROXY}/claude` });

const msg = await client.messages.create({
  model: "claude-sonnet-5-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Hello" }],
});

// After a token refresh, swap only the credential:
const fresh = client.withOptions({ authToken: await sessionToken(userId) });

Streaming works unchanged. Use authToken, not apiKey: it sends Authorization: Bearer in every SDK version. In a web build pass dangerouslyAllowBrowser: true; the browser holds a one-hour per-user token, not your provider key.

Checked against the official Anthropic Node SDK 1.17: it sends /v1/messages under the base URL with Authorization: Bearer.

What changed

Direct to the providerThrough MyAppToken
Base URLhttps://api.anthropic.comhttps://api.myapptoken.com/api/v1/proxy/<app-id>/claude
Credentialprovider key (sk-ant-…)the user's token from /auth/simple

More: Anthropic guide · rate limits · async jobs.

Start free — no credit card; the Free plan covers 10,000 requests a month.