OpenAI from Python

SDK examples · All languages and providers · API reference

For scripts, tests or a backend. The official openai package works unchanged against OpenAI. 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.

import httpx   # installed with the openai and anthropic packages

API = "https://api.myapptoken.com/api/v1"
APP_ID = "<app-id>"

def session_token(user_id: str) -> str:
    res = httpx.post(f"{API}/auth/simple", json={"app_id": APP_ID, "user_id": user_id})
    res.raise_for_status()
    return res.json()["access_token"]          # also: expires_in (seconds)

Renew it before it expires with POST /auth/refresh (send the current, still-valid token as Authorization: Bearer); if that fails, request a new one. With Firebase App Check enabled, also send the App Check header on proxy calls.

3. Call OpenAI

from openai import OpenAI

PROXY = "https://api.myapptoken.com/api/v1/proxy/<app-id>"
jwt = session_token("user-123")

# Before: OpenAI(api_key="sk-…")
client = OpenAI(api_key=jwt, base_url=f"{PROXY}/openai")

chat = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello"}],
)
print(chat.choices[0].message.content)

# After a token refresh, swap only the credential:
client = client.with_options(api_key=session_token("user-123"))

Checked against the official OpenAI Python SDK 3.63: it sends /chat/completions under the base URL with Authorization: Bearer.

What changed

Direct to the providerThrough MyAppToken
Base URLhttps://api.openai.com/v1https://api.myapptoken.com/api/v1/proxy/<app-id>/openai
Credentialprovider 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.