DeepSeek from TypeScript / React Native

SDK examples · All languages and providers · API reference

The official openai package works unchanged against DeepSeek, because its API is OpenAI-compatible: you set the base URL and pass the user's token as the API key. Your DeepSeek 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 DeepSeek key in it:

deepseek  →  https://api.deepseek.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 DeepSeek

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-…", baseURL: "https://api.deepseek.com/v1" })
const client = new OpenAI({ apiKey: jwt, baseURL: `${PROXY}/deepseek` });

const chat = await client.chat.completions.create({
  model: "deepseek-chat",
  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.

What changed

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

More: DeepSeek guide · rate limits · async jobs.

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