n4nAI

How authentication works in OpenAI-compatible APIs

OpenAI compatible API authentication relies on bearer tokens in the Authorization header. Learn how keys, scopes, and gateways manage LLM access.

n4n Team4 min read978 words

Audio narration

Coming soon — every post will get a voice note here.

OpenAI compatible API authentication is the mechanism by which a client proves its identity to an HTTP service that emulates the OpenAI REST contract. It relies on a static bearer token sent in the Authorization header, allowing the server to authorize access to model endpoints such as /v1/chat/completions without per-request interactive login.

What “OpenAI-compatible” means for auth

The OpenAI API popularized a narrow, predictable surface: a base URL, a v1 prefix, JSON request bodies, and a single auth scheme. When a vendor says they offer an OpenAI-compatible API, they mean you can point your existing OpenAI SDK at their base URL, keep the same method calls, and authenticate with the same header shape. The token format differs, but the transport does not.

This convergence is practical. It lets you swap a direct OpenAI connection for a proxy, a self-hosted vLLM instance, or an inference gateway without rewriting client code. The auth boundary stays identical: one secret, one header.

How the bearer token flows

Token issuance

You generate an API key out-of-band, usually in a dashboard or via a CLI. The provider stores a hashed copy. The plaintext string—often prefixed sk- for OpenAI, or some other prefix elsewhere—is shown once and never again. Treat it like a password.

Providers typically store only a hash of the key, sometimes keeping the first few characters for visual identification in logs. They do not need the plaintext to validate a request; they hash the incoming token and compare.

Request shape

Every authenticated request carries:

POST /v1/chat/completions HTTP/1.1
Host: api.example.com
Authorization: Bearer sk-your-key-here
Content-Type: application/json

A minimal curl call:

curl https://api.example.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-4o","messages":[{"role":"user","content":"hi"}]}'

Server-side validation

On receipt, the gateway or provider strips the Bearer prefix, looks up the key, and checks:

  • Is the key active (not revoked)?
  • Does it belong to an org with quota?
  • Is the requested model allowed for this key?
  • Is the current rate limit exhausted?

If any check fails, the service returns 401 Unauthorized or 403 Forbidden. No LLM inference runs. The validation is synchronous and happens before the request body is parsed for model execution.

Why openai compatible api authentication matters in LLM stacks

LLM endpoints are not cheap or side-effect-free. A leaked key can drain a budget in minutes. Centralizing auth at the API boundary gives you:

  • Cost attribution: each key maps to a project or tenant.
  • Access scoping: restrict a key to specific models or routes.
  • Revocation: rotate or kill a key without touching application logic.

When you run a gateway in front of multiple upstream providers, this single auth scheme becomes the linchpin. The client sends one credential; the gateway translates it into downstream provider keys, applies fallback, and meters usage.

Concrete example across languages

Python with the OpenAI SDK

from openai import OpenAI

client = OpenAI(
    base_url="https://api.example.com/v1",
    api_key="sk-your-key-here",
)

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

TypeScript with fetch

const res = await fetch("https://api.example.com/v1/chat/completions", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.OPENAI_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "gpt-4o",
    messages: [{ role: "user", content: "Hello" }],
  }),
});

const data = await res.json();
console.log(data);

The pattern is identical because openai compatible api authentication does not care about the SDK—only the header.

Common misconceptions

“The API key is a user credential”

It is not. A key represents a project or service account, not a person. You do not get OAuth consent screens. If you need user-level auth, put an app auth layer in front and map sessions to a single server-side key, or use a gateway that issues short-lived keys.

“HTTPS is optional because it’s just a token”

Plaintext HTTP exposes the bearer token to anyone on the path. OpenAI-compatible endpoints must be TLS-only. Any provider offering plain HTTP is negligent; refuse it.

“Scopes are standardized”

They are not. OpenAI’s keys have broad access to the org’s models and billing. Some gateways add fine-grained scopes (e.g., models:read, chat:write), but there is no RFC. Read the docs for your specific endpoint.

“One key works identically across all providers”

The header is the same; the semantics differ. A key issued for OpenAI will not authenticate to Anthropic’s native API, but it will work against a gateway that accepts OpenAI-compatible auth and holds the Anthropic key server-side. That abstraction is the whole point of a compatible layer.

“Rotation requires downtime”

It does not. Issue a second key, deploy it to clients via env vars or secret stores, then revoke the first. Because the client only needs the header, no code change is required if the key is injected at runtime.

Gateway behavior: what happens after auth

A gateway that speaks the OpenAI protocol terminates your TLS, validates your bearer token, then makes its own downstream calls. For example, n4n.ai exposes one OpenAI-compatible endpoint covering 240+ models; it validates your key, honors any client routing directives you send, forwards provider cache-control hints, and applies automatic fallback when an upstream provider is rate-limited or degraded. Your openai compatible api authentication remains unchanged regardless of whether the request lands on a GPT, Claude, or open-weight model.

This central termination also enables per-token usage metering. The gateway counts prompt and completion tokens after the fact and attributes them to your key, something a raw provider key may not expose uniformly.

Differences from OAuth 2.0

OAuth issues short-lived access tokens via an interactive consent flow. OpenAI-compatible auth uses long-lived static keys. That is fine for server-to-server or CI scenarios where no human is present. If you need delegated access, wrap the static key behind your own OAuth layer and map subjects to internal keys.

Key practices for engineers

  • Store keys in environment variables or a secret manager. Never embed them in client-side code.
  • Use separate keys per environment (dev, staging, prod) and per tenant.
  • Log the key ID, not the secret, in access logs.
  • Set expiry or rotation policy if your gateway supports it.
  • Monitor 401 rates; a spike means a leaked or misconfigured key.
  • Send custom routing headers only if the gateway documents them; unknown headers are ignored.

Closing note on the standard

The value of openai compatible api authentication is boring predictability. You learn it once, and every compliant endpoint—whether a single GPU box or a multi-provider gateway—speaks the same language. Build your client against the header, not the vendor.

Tagsrest-apiauthenticationapi-keysopenai-api

Written by

n4n Team

The team building n4n — a single OpenAI-compatible API in front of 240+ models, with automatic fallback, load balancing and pay-per-token metering.

More from n4n Team →

All rest api fundamentals for llm gateways posts →