OpenAI from TypeScript / React Native
SDK examples · All languages and providers · API reference
The official openai package works unchanged against OpenAI: you set the base URL and pass the user's token as the API key. Your OpenAI 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 OpenAI key in it:
openai → https://api.openai.com/v1
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 OpenAI
import OpenAI from "openai"; const PROXY = "https://api.myapptoken.com/api/v1/proxy/<app-id>"; const jwt = await sessionToken(userId); // Before: new OpenAI({ apiKey: "sk-…" }) const client = new OpenAI({ apiKey: jwt, baseURL: `${PROXY}/openai` }); const chat = await client.chat.completions.create({ model: "gpt-4o-mini", messages: [{ role: "user", content: "Hello" }], }); console.log(chat.choices[0].message.content); // After a token refresh, swap only the credential: const fresh = client.withOptions({ apiKey: await sessionToken(userId) });
Streaming (stream: true) works unchanged. In React Native the SDK runs as-is. In a web build (for example Expo web) it refuses to start in a browser unless you pass dangerouslyAllowBrowser: true. That is acceptable here, because the browser holds a one-hour per-user token, not your provider key.
Checked against the official OpenAI Node SDK 7.69: it sends /chat/completions under the base URL with Authorization: Bearer.
What changed
| Direct to the provider | Through MyAppToken | |
|---|---|---|
| Base URL | https://api.openai.com/v1 | https://api.myapptoken.com/api/v1/proxy/<app-id>/openai |
| Credential | provider key (sk-…) | the user's token from /auth/simple |
More: OpenAI guide · rate limits · async jobs.
Start free — no credit card; the Free plan covers 10,000 requests a month.