Claude API in React Native: Mobile App Integration
Adding Claude to a React Native app means answering one question first: where does the Anthropic API key live? It cannot live in your app bundle. Mobile apps are distributed as compiled binaries that anyone can decompile, inspect, or intercept traffic from, so any key shipped inside the app — hardcoded, in .env, in AsyncStorage — is public within hours of release. The fix is a thin backend layer between your React Native UI and Claude, either one you build yourself or a hosted proxy you plug in with a drop-in key.
This guide covers the architecture, the React Native-specific gotchas (streaming on iOS/Android, token limits on mobile networks, background/foreground state), and working code for both a custom Node proxy and a managed option.
Why you can't call Claude directly from the app
Anthropic's API expects an x-api-key header tied to your account's billing and rate limits. If that key is embedded in a mobile build:
- It can be extracted with basic reverse-engineering tools (even with obfuscation, it's a matter of effort, not possibility).
- Anyone with the key can run requests against your account, consuming your quota and budget.
- You lose the ability to rotate the key without shipping a new app version and waiting for store review.
- You can't enforce per-user limits, logging, or moderation before requests hit Claude.
The standard architecture is:
React Native app → your backend (or a proxy) → Claude API
Your backend holds the real credentials, applies auth/rate limiting for your own users, and forwards requests. The app only ever talks to an endpoint you control over HTTPS.
Option 1: Build your own proxy
A minimal Express proxy looks like this:
// server.js
const express = require("express");
const Anthropic = require("@anthropic-ai/sdk");
const app = express();
app.use(express.json());
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
app.post("/chat", async (req, res) => {
const { messages } = req.body;
// TODO: verify the calling user (JWT, session, etc.) before proceeding
const response = await anthropic.messages.create({
model: "claude-opus-4",
max_tokens: 1024,
messages,
});
res.json(response);
});
app.listen(3000);
This works, but you now own: deployment, uptime, streaming infrastructure, per-user usage tracking, retries on 429s, and key rotation. For a side project that's fine. For a shipping product with real users, it's a maintenance line item you'll revisit every time Anthropic changes rate limits or releases a new model.
Option 2: Use a hosted API layer
SubToAPI exists for exactly this gap: it turns your Claude access into a clean HTTPS API with application keys (sub_live_...), streaming, tool use, and usage metadata per key — so your React Native app calls a stable endpoint instead of managing its own backend proxy infrastructure from scratch.
// api.js - called from your React Native app
const SUBTOAPI_KEY = "sub_live_..."; // stored on YOUR server, not in the app
export async function askClaude(messages) {
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${SUBTOAPI_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "claude-opus-4",
max_tokens: 1024,
messages,
}),
});
return res.json();
}
The important detail: even with SubToAPI, the sub_live_... key stays on a server you control — a serverless function, a lightweight Node service, or your existing backend. Your React Native app calls that endpoint, which forwards to SubToAPI. You still need one network hop between the app and the key, but you no longer need to build message formatting, streaming, or usage tracking yourself. See the quickstart and messages endpoint docs for the full request/response shape.
Streaming responses in React Native
Claude's chat UX depends on streaming tokens as they're generated. React Native's fetch doesn't expose a readable stream the same way browsers do, so most teams use one of two approaches:
Server-Sent Events via react-native-sse or a polyfill, reading chunks as they arrive and appending to a message state variable.
A WebSocket bridge, where your backend streams Claude's output over a socket connection the app subscribes to — more reliable across iOS background/foreground transitions than raw SSE.
import EventSource from "react-native-sse";
const es = new EventSource("https://your-backend.com/chat-stream", {
method: "POST",
body: JSON.stringify({ messages }),
});
es.addEventListener("message", (event) => {
const chunk = JSON.parse(event.data);
setReply((prev) => prev + chunk.text);
});
es.addEventListener("error", () => es.close());
SubToAPI's streaming docs cover the event format your backend receives from the proxy, which you then re-emit to the app in whatever transport fits your stack.
Mobile-specific things to handle
- Network interruptions mid-stream. Cellular connections drop. Buffer partial responses and support resuming or at least gracefully truncating with a retry button.
- Token limits vs. screen size. Long Claude responses are harder to scroll through on a phone. Consider summarizing or paginating rather than dumping full output.
- App backgrounding. iOS suspends network activity when the app backgrounds. If a user switches apps mid-stream, either cancel the request or hand it off to a background task; don't assume the connection survives.
- Offline state. Queue messages locally and send when connectivity returns rather than failing silently.
- Tool use from mobile. If your app triggers Claude tool calls (e.g., looking up data, calling device APIs), keep tool execution on your backend — never let the client execute arbitrary tool results unchecked.
Minimal checklist before shipping
- No Anthropic or SubToAPI key anywhere in the app bundle, ever.
- All requests go through a backend you control, over HTTPS.
- Streaming has a timeout and retry path for flaky mobile networks.
- Per-user rate limiting exists somewhere in the chain (your backend or your proxy's key-level limits).
- You've tested backgrounding mid-request on both iOS and Android.
Whether you build the proxy yourself or use a service like SubToAPI (plans start at €9/month with a free trial at signup), the architecture is the same: React Native talks to your backend, your backend talks to Claude. That boundary is what keeps your API key — and your bill — safe.
Questions
Can I call the Claude API directly from a React Native app without a backend? Not safely. Any API key bundled in a mobile app can be extracted. You need at least one server-side hop that holds the real credentials.
How do I stream Claude responses into a React Native chat UI? Use Server-Sent Events (via a library like react-native-sse) or a WebSocket bridge from your backend, appending text chunks to message state as they arrive.
Is SubToAPI a replacement for Anthropic's API? No — it's a layer on top of your existing Claude access that gives you application API keys, streaming, and usage tracking so you don't have to build that infrastructure yourself. See the docs for details.