Skip to main content

OpenAI

Connect AISIX AI Gateway to OpenAI so applications can call GPT models through the gateway's OpenAI-compatible API. AISIX keeps the OpenAI credential on the gateway side. It authorizes model access with caller API keys and allowlists, and applies the same rate-limit and usage-accounting controls as other model aliases.

OpenAI is the gateway's native OpenAI-compatible upstream. It uses the openai adapter and is the only provider where api_base is optional.

Prerequisites

The following examples use a self-hosted AISIX gateway. Before configuring the upstream, prepare the following:

Configure the OpenAI Upstream

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

Create a Provider Key

Create the provider key that stores the OpenAI credential:

# Replace with your values
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" \
--data-binary @- <<EOF
{
"display_name": "openai-prod",
"provider": "openai",
"adapter": "openai",
"secret": "${OPENAI_API_KEY}"
}
EOF

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

Create the model alias callers will send in requests:

# Replace with your values
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" \
--data-binary @- <<EOF
{
"display_name": "gpt-4o-prod",
"provider": "openai",
"model_name": "gpt-4o",
"provider_key_id": "${PROVIDER_KEY_ID}"
}
EOF

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:

# Replace with your values
export AISIX_API_KEY="YOUR_CALLER_API_KEY"

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

Create the caller API key resource that can access the model alias:

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

The allowed_models value must match the model alias you created.

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.

Verify the Provider Connection

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 the request fails with an upstream authentication error, check the provider key secret.

Use an OpenAI SDK

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.

Next Steps

You have now connected AISIX to OpenAI and verified the model alias. Continue with these guides: