Ephemeral tokens: how a mobile app should get an access token for AI APIs

Search for how to get an access token in a mobile app and you find two kinds of advice: OAuth flows for signing users in, and "never ship your API key". The missing piece is what the app should send when it calls an AI API such as OpenAI or Claude. The answer is an ephemeral token: a short-lived credential the app obtains at runtime, tied to one user, and useless an hour later.

Ephemeral token vs API key

The flow

  1. The app identifies the user: a signed-in account ID, or an anonymous install ID.
  2. It asks a token endpoint for a token. The endpoint checks what it can — the app ID, a limit on token requests, ideally an attestation such as App Check — and returns a signed token with an expiry.
  3. The app calls the API with Authorization: Bearer <token>. A server in between verifies the token, applies per-user limits and adds the real provider key.
  4. Before the token expires, the app renews it. If renewal fails, it requests a new one.
POST https://api.myapptoken.com/api/v1/auth/simple
{"app_id": "<app-id>", "user_id": "user-123"}

→ {"access_token": "eyJhbGciOi…", "token_type": "Bearer", "expires_in": 3600, …}

How long should it live?

Long enough that the app is not constantly fetching tokens, short enough that a leaked one is worth little. One hour is a common default. Renew a few minutes before expiry instead of waiting for a 401, so a user in the middle of a conversation never sees a failed request.

What the token does not prove

A token says who a request is for, not that it came from your app. If the token endpoint accepts any user ID, a script can mint tokens too. So the token is one layer of three: per-user tokens for identity and revocation, server-side rate limits for volume, and App Check to reject requests that do not come from a genuine build.

What about provider-native ephemeral tokens?

Some providers issue ephemeral tokens themselves, but only for specific realtime APIs — for example OpenAI's Realtime API and Gemini's Live API. They do not cover the regular chat, image or message endpoints most apps use. For those you still need your own token layer in front of the provider.

Without building the token server

MyAppToken is that layer as a service: /auth/simple issues one-hour per-user tokens, /auth/refresh renews them, and the proxy verifies each request and injects your provider key server-side. Your app keeps the official OpenAI or Anthropic SDK and changes two settings — see the SDK examples for TypeScript, Kotlin, Swift, Flutter and Python.

Stop shipping your API key inside your app.

Swap the base URL, keep the provider's native API, and let the proxy inject your key server-side.

Start free