Skip to main content

OpenAI

In this guide, you will connect AISIX AI Gateway to OpenAI. Callers reach GPT models through the gateway's OpenAI-compatible API, while AISIX owns the credential, model allowlist, rate limits, and usage accounting.

Use this configuration when applications should authenticate to AISIX with a caller API key instead of holding your OpenAI key directly. OpenAI is the gateway's native upstream: it uses the openai adapter, and it is the one provider where api_base is optional.

Prerequisites

Before starting, prepare the following:

  • A gateway with the admin API on :3001 and the proxy API on :3000.
  • The admin key from the gateway config.yaml.
  • An OpenAI API key from the OpenAI platform and the model ID you want to expose, for example gpt-4o.

Supported Capabilities

OpenAI has the broadest endpoint coverage of any upstream, because the gateway's own API surface is OpenAI-shaped.

EndpointSupportedStreamingNotes
/v1/chat/completionsYesYesPrimary path.
/v1/embeddingsYesEmbeddings require the openai adapter.
/v1/responsesYesYesForwarded to OpenAI verbatim.
/v1/images/generationsYesRequires provider: "openai".
/v1/audio/transcriptions, /v1/audio/translations, /v1/audio/speechYesOpenAI-style audio forwarding (transcription, translation, speech).
/v1/rerankYesprovider must be in the rerank allowlist (openai, cohere, jina).

See Provider Compatibility for the exact per-endpoint rules.

Configure the OpenAI Upstream

Create a provider key, model alias, and caller API key for the OpenAI-backed route.

Create a Provider Key

export AISIX_ADMIN_KEY="YOUR_ADMIN_KEY"
export OPENAI_API_KEY="YOUR_PROVIDER_API_KEY"

curl -sS -X POST "http://127.0.0.1:3001/admin/v1/provider_keys" \
-H "Authorization: Bearer ${AISIX_ADMIN_KEY}" \
-H "Content-Type: application/json" \
-d '{
"display_name": "openai-prod",
"provider": "openai",
"adapter": "openai",
"secret": "'"${OPENAI_API_KEY}"'"
}'

provider is openai. This is the only provider value that lets AISIX fall back to the default base URL https://api.openai.com/v1.

adapter selects the OpenAI wire format.

secret stores the OpenAI API key. It follows the credential-handling behavior in Provider Credentials.

You can omit api_base for OpenAI. To target a compatible OpenAI endpoint such as an Azure-fronted proxy or a regional gateway, set api_base explicitly. Copy the returned provider key ID.

Create a Model

export PROVIDER_KEY_ID="YOUR_PROVIDER_KEY_ID"

curl -sS -X POST "http://127.0.0.1:3001/admin/v1/models" \
-H "Authorization: Bearer ${AISIX_ADMIN_KEY}" \
-H "Content-Type: application/json" \
-d '{
"display_name": "gpt-4o-prod",
"provider": "openai",
"model_name": "gpt-4o",
"provider_key_id": "'"${PROVIDER_KEY_ID}"'"
}'

display_name is the alias callers send in model.

model_name is the OpenAI model ID, for example gpt-4o or gpt-4o-mini.

provider_key_id attaches the alias to the OpenAI provider key.

Create a Caller API Key

Choose the caller API key value that the application will send to AISIX, then hash it for the admin resource:

export AISIX_API_KEY="YOUR_CALLER_API_KEY"

CALLER_KEY_HASH=$(printf '%s' "${AISIX_API_KEY}" | shasum -a 256 | awk '{print $1}')

curl -sS -X POST "http://127.0.0.1:3001/admin/v1/apikeys" \
-H "Authorization: Bearer ${AISIX_ADMIN_KEY}" \
-H "Content-Type: application/json" \
-d '{
"key_hash": "'"${CALLER_KEY_HASH}"'",
"allowed_models": ["gpt-4o-prod"]
}'

allowed_models must match the model alias you created.

Verify the Upstream

Send a chat-completions request through the AISIX proxy:

curl -sS -X POST "http://127.0.0.1:3000/v1/chat/completions" \
-H "Authorization: Bearer ${AISIX_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-prod",
"messages": [{"role": "user", "content": "Say hello from OpenAI."}]
}'

The gateway returns an OpenAI-compatible response that echoes the caller-facing alias gpt-4o-prod. Confirm the request on the OpenAI usage dashboard. If AISIX returns an upstream authentication error, check the provider key secret.

Behavior and Limits

Point OpenAI SDK clients at the gateway by setting the base URL to http://127.0.0.1:3000/v1 and the API key to the caller key. See the OpenAI SDK Quickstart.

The gateway hashes and stores caller keys as key_hash; the raw key is never stored. Provider key secrets are returned as *** in Admin API list views.

Next Steps

API7.ai Logo

The digital world is connected by APIs,
API7.ai exists to make APIs more efficient, reliable, and secure.

Sign up for API7 newsletter

Product

API7 Gateway

SOC2 Type IIISO 27001HIPAAGDPRRed Herring

Copyright © APISEVEN PTE. LTD 2019 – 2026. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the Apache Software Foundation