Skip to main content

Overview

NanoGPT supports authorization-code OAuth with PKCE for public clients. The returned credential is a dedicated NanoGPT API key in sk-nano-... format, scoped to the approved app/user grant. Use the returned key as a bearer token against the OpenAI-compatible API:
API base URL:
OAuth-created keys can spend from the user’s NanoGPT balance, subject to account balance, subscriptions, API key settings, optional OAuth spend caps, expiration, allowed origins, and model/provider limits.
Treat the returned access_token like a password. Do not log it, embed it in browser-visible HTML, or expose it to other users.

Choose a Flow

All three flows return the same kind of credential: a dedicated NanoGPT API key.

Discovery

Authorization server metadata:
Example response:
Additional machine-facing metadata:

Scopes

For this MVP, OAuth requests must include api.use because the returned access token is a spend-capable API key. Recommended scope:
NanoGPT normalizes returned scopes to its supported scope order:

PKCE Requirements

NanoGPT requires S256 PKCE.
  • code_challenge_method must be S256.
  • plain is rejected.
  • code_verifier must use RFC 7636 characters: A-Z, a-z, 0-9, -, ., _, ~.
  • code_verifier length must be 43 to 128 characters.
  • code_challenge must be base64url-encoded SHA-256 of the verifier.
  • Authorization codes expire quickly and are one-time use.
JavaScript helper:

Redirect URI Rules

Allowed redirect URIs:
  • HTTPS redirect URIs.
  • Loopback HTTP redirect URIs with an explicit port, such as http://127.0.0.1:8787/callback, http://localhost:8787/callback, or http://[::1]:8787/callback.
Rejected redirect URIs:
  • Wildcards.
  • URL fragments.
  • Credentials in URLs.
  • Non-HTTPS web redirects.
  • Loopback HTTP without an explicit port.
  • Redirect URIs that do not exactly match the registered or callback URI.
Loopback redirects are canonicalized to 127.0.0.1 internally.

Shortcut Key Handoff

Use this flow for local apps and clients that want the shortest browser sign-in integration.

1. Generate PKCE

Generate:
  • code_verifier
  • code_challenge = base64url(sha256(code_verifier))
  • state

2. Redirect to NanoGPT

Send the user to:
Query parameters: Example:
NanoGPT creates or reuses an internal callback client, then redirects the user into the consent flow.

3. Handle the Callback

On approval:
On denial or safe OAuth error:
If the callback URL is invalid, NanoGPT does not redirect to it and shows the error on NanoGPT instead.

4. Exchange the Code

JSON body:
grant_type is optional. If present, it must be authorization_code. Form-encoded bodies are also accepted. Success response:
Use either key or access_token; they contain the same value.

Standard OAuth PKCE

Use this flow for generic OAuth clients that want explicit dynamic registration and standard OAuth endpoint names.

1. Register the Client

Request:
Required fields:
  • client_name
  • redirect_uris
Optional fields:
  • grant_types, defaults to ["authorization_code"]
  • response_types, defaults to ["code"]
  • token_endpoint_auth_method, defaults to none
  • client_uri, must be HTTPS and have no fragment
  • logo_uri, must be HTTPS and have no fragment
Only public PKCE clients are supported. NanoGPT does not issue client secrets. Success response:

2. Redirect to the Authorization Endpoint

Query parameters: Example:
The user signs in to NanoGPT if needed and sees a consent screen showing the app name, redirect host, NanoGPT account, balance, requested scopes, spend warning, and optional daily, weekly, or monthly spend cap. For normal registered web clients, NanoGPT may auto-approve an unchanged active grant. For loopback and callback shortcut clients, the consent screen is shown again.

3. Handle the Callback

On approval:
On denial:
OAuth parameter validation errors are redirected only after the client and redirect URI have been validated. Invalid redirect URIs are shown as errors on NanoGPT and are not redirected.

4. Exchange the Code

Form body:
Required fields:
  • grant_type=authorization_code
  • client_id
  • redirect_uri
  • code
  • code_verifier
JSON bodies are also accepted. Success response:
NanoGPT does not issue refresh tokens in this MVP.

Authenticated Downstream Key Code

An already authenticated app can create a one-time authorization code for a downstream local app.
Request:
Required:
  • redirect_uri or callback_url
  • code_challenge
  • Authenticated source API key in the Authorization header
Optional:
  • code_challenge_method, defaults to S256
  • scope, defaults to api.use models.read
  • key_label or key_name
  • limit
  • usage_limit_type: daily, weekly, or monthly; defaults to monthly when limit is present
  • expires_at
  • client_name, app_name, or name
  • x-title or x-app-name headers as fallback app names
Success response:
The downstream app exchanges this code at:
Restrictions:
  • The source API key must be active.
  • The source API key must be linked to a signed-in NanoGPT account.
  • OAuth-issued API keys cannot create further OAuth key codes.
  • Source keys with existing spend, request, model, provider, origin, or redaction restrictions are rejected for this flow.
  • If the source key has an expiration, the downstream key cannot outlive it.

Use the Returned Credential

List models:
OpenAI-compatible chat:
Common API surfaces:
  • GET /api/v1/models
  • GET /api/v1/image-models
  • GET /api/v1/video-models
  • GET /api/v1/audio-models
  • POST /api/v1/chat/completions
  • POST /api/v1/responses
  • POST /api/v1/messages

Token and Key Behavior

The OAuth access_token is a dedicated NanoGPT API key in this MVP. Implications:
  • It is long-lived unless an expiration is set.
  • It can spend from the user’s NanoGPT balance.
  • It should be stored like a password.
  • It should not be logged or exposed in browser-visible HTML.
  • It can be revoked by deleting or disabling the API key in NanoGPT settings.
  • The same active grant can reuse the same key when settings are unchanged.
  • New app-specific limits or expiration can force a new dedicated key.
OAuth-created keys are visible as API keys and are named:
Consent screens can let users set an optional daily, weekly, or monthly USD spend cap. A $0 cap blocks paid spend for that app. Leaving the cap empty means no app-specific cap. Browser/web OAuth keys are tied to origins derived from the redirect URI when applicable. Loopback keys allow common loopback origins for the chosen callback port.

Revocation

NanoGPT does not expose an OAuth revocation endpoint in this MVP. Users revoke access by deleting, disabling, expiring, or limiting the generated API key in NanoGPT settings. Client behavior:
  • If a stored key starts returning 401, discard it.
  • Ask the user to sign in again.
  • Do not keep retrying a revoked key.

Errors

OAuth JSON errors use this shape:
Common errors:

Rate Limits and Abuse Controls

OAuth endpoints are rate-limited. Developers should:
  • Avoid repeated failed token exchanges.
  • Restart the flow after an invalid_grant.
  • Never reuse authorization codes.
  • Never log raw authorization codes, code verifiers, or API keys.

Local App Example

Example source:
Repository URL:
Run:
Local development:
The example starts a localhost callback server, generates PKCE, sends the user to /auth, exchanges the code at /api/v1/auth/keys, calls /api/v1/models, and masks the key in terminal output.