# OpenRouter

[OpenRouter](https://openrouter.ai/docs/guides/overview/models) aggregates models from many providers behind one API and credential. AISIX adds stable model aliases, caller access controls, rate limits, and usage accounting while keeping the OpenRouter credential at the gateway.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

Before starting, prepare the following:

* One AISIX setup:

  <!-- -->

  * For AISIX Enterprise or AISIX Cloud, an environment with an attached gateway and a write-scoped admin token. For AISIX Enterprise, follow the [AISIX Enterprise getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/aisix-cloud-quickstart.md). To request AISIX Cloud access, [contact API7](https://api7.ai/contact).
  * For the open-source AISIX gateway, prepare either a local AISIX installation or the Docker setup from the [AISIX OSS getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/gateway-quickstart.md). Configure the gateway to load a declarative resources file.

* An OpenRouter API key from the [OpenRouter keys page](https://openrouter.ai/settings/keys). OpenRouter key values begin with `sk-or-`.

* `curl` and `jq`.

## Configure with AISIX Enterprise or AISIX Cloud[​](#configure-with-aisix-enterprise-or-aisix-cloud "Direct link to Configure with AISIX Enterprise or AISIX Cloud")

Export the control-plane connection details:

```
# AISIX_CP is the Admin API base URL; include /api and omit a trailing slash

# The local AISIX Enterprise quickstart uses http://localhost:8080/api

export AISIX_CP="YOUR_AISIX_CONTROL_PLANE_API_URL"

export AISIX_TOKEN="YOUR_ADMIN_TOKEN"

export ENV_ID="YOUR_ENVIRONMENT_ID"
```

Create a provider key, model alias, and caller API key for OpenRouter-backed Chat Completions and native Responses.

Because OpenRouter exposes an OpenAI-compatible API, AISIX connects through the `openai` adapter and uses the OpenRouter API root as `api_base`.

### Create a Provider Key[​](#create-a-provider-key "Direct link to Create a Provider Key")

Create the provider key that stores the OpenRouter credential and API root:

```
# Replace with your values

export OPENROUTER_API_KEY="YOUR_PROVIDER_API_KEY"



PROVIDER_KEY_ID=$(curl -sS -X POST "$AISIX_CP/provider_keys" \

  -H "Authorization: Bearer $AISIX_TOKEN" \

  -H "Content-Type: application/json" \

  -d '{

    "display_name": "openrouter-prod",

    "provider": "openrouter",

    "api_key": "'"${OPENROUTER_API_KEY}"'",

    "api_base": "https://openrouter.ai/api/v1",

    "apis": {

      "responses": {}

    },

    "allowed_environments": ["'"${ENV_ID}"'"]

  }' | jq -r '.provider_key.id')



echo "$PROVIDER_KEY_ID"
```

❶ `provider` is `openrouter`. The AISIX Cloud Admin API derives the adapter from the catalog provider; the `adapter` field is only accepted on BYO provider keys.

❷ `api_key` stores the OpenRouter API key. The value is encrypted before storage and never returned by read endpoints. It follows the credential-handling behavior in [Provider Keys](https://docs.api7.ai/ai-gateway/models/provider-keys.md#credential-handling).

❸ `api_base` is `https://openrouter.ai/api/v1`. OpenRouter serves its API under `/api` on its main host, so the root carries both segments and is not `https://openrouter.ai/v1`. AISIX appends the endpoint path, for example `/chat/completions`, to this value.

❹ `apis.responses` declares that OpenRouter serves the Responses API at the same root. AISIX can then use OpenRouter's native request and response format instead of translating through Chat Completions.

Always set `api_base` explicitly for OpenRouter. Unlike most featured catalog providers, OpenRouter has no curated default base URL in AISIX, so an omitted `api_base` leaves the AISIX Cloud Admin API dependent on synced catalog metadata and the create can fail with `400 INVALID_REQUEST`.

The gateway enforces the same rule at request time. When a provider key for a non-OpenAI vendor reaches the gateway with an empty `api_base`, the OpenAI-family bridge refuses to fall back to the public OpenAI host and returns an upstream configuration error instead. An OpenRouter credential is therefore never sent to `api.openai.com`.

The command captures the returned provider key ID in `PROVIDER_KEY_ID`.

### Create a Model[​](#create-a-model "Direct link to Create a Model")

OpenRouter model IDs are namespaced. The upstream ID is `vendor/model`, and the vendor prefix is part of the ID rather than a routing hint. Check the [OpenRouter models list](https://openrouter.ai/models) for a current ID before creating a model alias.

The naming convention has four forms:

* `vendor/model` is the base form, for example `anthropic/claude-sonnet-5`, `openai/gpt-5.2-pro`, or `deepseek/deepseek-v4-pro`.
* A variant suffix is appended to the slug, for example `:free` or `:thinking`.
* A `~` before the vendor resolves to the newest version in a family, for example `~anthropic/claude-sonnet-latest`.
* `openrouter/auto` selects OpenRouter's own Auto Router instead of a named model.

The model alias and upstream model ID are different names. `display_name` is the AISIX alias that callers put in the request `model` field, while `model_name` is the OpenRouter model ID. AISIX forwards `model_name` to the upstream verbatim, including the `/` in the vendor prefix. It does not split the ID or strip the vendor segment.

Create the model alias callers will send in requests:

```
MODEL_ID=$(curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/models" \

  -H "Authorization: Bearer $AISIX_TOKEN" \

  -H "Content-Type: application/json" \

  -d '{

    "display_name": "openrouter-sonnet-prod",

    "model_name": "anthropic/claude-sonnet-5",

    "provider_key_id": "'"${PROVIDER_KEY_ID}"'"

  }' | jq -r '.model.id')



echo "$MODEL_ID"
```

❶ `display_name` is the alias callers send in `model`. Keep it a short flat name. Copying the namespaced upstream ID into the alias makes client code harder to read and hides which alias is in use.

❷ `model_name` is the OpenRouter model ID, for example `anthropic/claude-sonnet-5` or `openai/gpt-5.2-pro`.

❸ `provider_key_id` attaches the alias to the OpenRouter provider key.

Create one alias per OpenRouter model you intend to expose. A single OpenRouter provider key can back any number of aliases, which is how one credential fans out to several vendors while each alias keeps its own allowlist entry and rate limits.

### Create a Caller API Key[​](#create-a-caller-api-key "Direct link to Create a Caller API Key")

Create the caller API key resource that can access the model alias. The gateway generates the key value; the plaintext is returned once in the create response, so capture it now:

```
AISIX_API_KEY=$(curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/api_keys" \

  -H "Authorization: Bearer $AISIX_TOKEN" \

  -H "Content-Type: application/json" \

  -d '{

    "display_name": "openrouter-caller",

    "allowed_models": ["'"${MODEL_ID}"'"]

  }' | jq -r '.plaintext')



echo "$AISIX_API_KEY"
```

The `allowed_models` value references the model by its ID, so the key can only access the alias you created. After the write, the configuration projects to attached gateways automatically.

## Configure with AISIX OSS[​](#configure-with-aisix-oss "Direct link to Configure with AISIX OSS")

Export the upstream credential and choose the caller API key that applications will send to the gateway:

```
export OPENROUTER_API_KEY="YOUR_PROVIDER_API_KEY"

export CALLER_API_KEY="YOUR_CALLER_API_KEY"

export CALLER_API_KEY_SHA256="$(printf '%s' "$CALLER_API_KEY" | sha256sum | cut -d ' ' -f 1)"
```

For a new gateway, use this complete resources file. For an existing gateway, merge the entries into its [current file](https://docs.api7.ai/ai-gateway/reference/resources-file.md#apply-resource-examples-safely), preserving its other resources:

resources.yaml

```
_format_version: "2"



provider_keys:

  - id: 9ca7288c-17bf-4aca-b7cc-37926dc2a095

    display_name: "openrouter-prod"

    provider: "openrouter"

    adapter: "openai"

    api_key: ${OPENROUTER_API_KEY}

    api_base: "https://openrouter.ai/api/v1"

    apis:

      responses: {}



models:

  - id: 0a553d70-b5c2-4de1-8263-8ee7111f1a53

    display_name: "openrouter-sonnet-prod"

    provider: "openrouter"

    model_name: "anthropic/claude-sonnet-5"

    provider_key_id: 9ca7288c-17bf-4aca-b7cc-37926dc2a095



api_keys:

  - id: 94e4e9fb-34e4-451b-b5e8-ced1e7d7c52e

    display_name: "openrouter-caller"

    key_hash: ${CALLER_API_KEY_SHA256}

    allowed_models:

      - "openrouter-sonnet-prod"
```

If AISIX is installed locally, validate the file before loading it:

```
aisix validate --resources resources.yaml
```

After validation, start the gateway with the referenced environment variables in its process environment. Reload an existing gateway only if those variables are already available to the process; otherwise, restart it with the updated environment.

If you use Docker, adapt the validation and startup commands in the [AISIX OSS getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/gateway-quickstart.md). Mount this `resources.yaml` file and pass every environment variable it references with `-e` in both commands. After the resources load, prepare the shared verification request below:

```
export AISIX_API_KEY="$CALLER_API_KEY"
```

## Verify the Provider Connection[​](#verify-the-provider-connection "Direct link to Verify the Provider Connection")

Export the AISIX gateway origin:

```
# AISIX_PROXY has no trailing slash or endpoint path

# The local quickstarts use http://127.0.0.1:3000

export AISIX_PROXY="YOUR_AISIX_GATEWAY_URL"
```

Send a Responses request through the AISIX proxy:

```
curl -sS -X POST "$AISIX_PROXY/v1/responses" \

  -H "Authorization: Bearer ${AISIX_API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "model": "openrouter-sonnet-prod",

    "input": "Say hello from OpenRouter."

  }'
```

AISIX sends the request to OpenRouter's native Responses endpoint, replaces the AISIX alias with the namespaced upstream model ID, and restores the alias in the response.

If the request fails, check these OpenRouter-specific causes first:

* A `404` from the upstream usually means the vendor prefix is missing or misspelled in `model_name`. `claude-sonnet-5` is not a valid OpenRouter ID; `anthropic/claude-sonnet-5` is.
* A `401` from the upstream usually means `api_key` does not hold an OpenRouter key. Keys issued by the model's original vendor are not accepted by OpenRouter.
* A connection error to an unexpected host usually means `api_base` is missing the `/api` segment.

## Use Native Messages Through Passthrough[​](#use-native-messages-through-passthrough "Direct link to Use Native Messages Through Passthrough")

The provider key above already sends Responses requests to OpenRouter's native endpoint while retaining the AISIX model alias. OpenRouter also publishes a native Messages API on the same API root, but it documents bearer authentication and does not publish Count Tokens. Keep native Messages on passthrough rather than adding `apis.messages`, which sends `x-api-key` and represents both Messages routes.

Call `/passthrough/openrouter/messages` and send the namespaced OpenRouter model ID because passthrough does not rewrite aliases. The detailed route behavior and passthrough setup appear in [Endpoint Support for OpenRouter Models](#endpoint-support-for-openrouter-models).

## Pass OpenRouter Routing and Reasoning Controls[​](#pass-openrouter-routing-and-reasoning-controls "Direct link to Pass OpenRouter Routing and Reasoning Controls")

AISIX does not strip top-level chat-completions fields it does not model. Unmodeled fields are forwarded to the upstream verbatim, so OpenRouter's own request-level controls reach OpenRouter unchanged.

Two OpenRouter control objects are commonly used through the gateway:

* `provider` selects which upstream vendor serves the request. It accepts fields such as `order`, `allow_fallbacks`, and `require_parameters`. See [Provider Routing](https://openrouter.ai/docs/guides/routing/provider-selection).
* `reasoning` controls chain-of-thought behavior with `enabled`, `effort`, and `max_tokens`, and the top-level `reasoning_effort` field is an alias for the effort setting. See [Reasoning Tokens](https://openrouter.ai/docs/guides/best-practices/reasoning-tokens).

The following request pins the serving vendor and disables OpenRouter fallbacks while asking for high reasoning effort:

```
curl -sS -X POST "$AISIX_PROXY/v1/chat/completions" \

  -H "Authorization: Bearer ${AISIX_API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "model": "openrouter-sonnet-prod",

    "messages": [

      {

        "role": "user",

        "content": "Summarize the CAP theorem in two sentences."

      }

    ],

    "provider": {

      "order": ["anthropic"],

      "allow_fallbacks": false

    },

    "reasoning": {

      "effort": "high"

    }

  }'
```

### Read Reasoning Output[​](#read-reasoning-output "Direct link to Read Reasoning Output")

When a model exposes reasoning text, OpenRouter returns it at `message.reasoning` on the non-streaming path and `delta.reasoning` on the streaming path, rather than the `reasoning_content` field that other OpenAI-compatible upstreams use. Some models do not expose their reasoning text.

AISIX normalizes both into the canonical `reasoning_content` field, so clients read `choices[0].message.reasoning_content` and `choices[0].delta.reasoning_content` regardless of which spelling the upstream used. When an upstream sends both fields, `reasoning_content` takes precedence.

This normalization is built into the `openai` adapter for OpenRouter. Do not set a [`response.reasoning_field`](https://docs.api7.ai/ai-gateway/models/provider-keys.md#configure-provider-specific-overrides) override on an OpenRouter provider key. That override exists for upstreams whose reasoning path AISIX does not already recognize.

OpenRouter can also return a structured `reasoning_details` array that must be replayed unchanged for some multi-turn reasoning and tool-calling flows. The normalized AISIX chat path does not retain this array. Use an OpenRouter-native endpoint through a passthrough route when the application must preserve structured reasoning state.

caution

Provider and reasoning controls are OpenRouter-specific request fields. If you later repoint the alias at another provider, or place it behind a [multi-target routing model](https://docs.api7.ai/ai-gateway/routing/routing-and-failover.md), AISIX forwards those fields to whichever upstream serves the request, and a non-OpenRouter upstream may ignore or reject fields it does not recognize.

## Endpoint Support for OpenRouter Models[​](#endpoint-support-for-openrouter-models "Direct link to Endpoint Support for OpenRouter Models")

An OpenRouter model alias does not reach every proxy route. Several routes gate on the configured provider value rather than on the adapter family.

| Route                                                   | OpenRouter support                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `/v1/chat/completions`, including `stream: true`        | Supported through the `openai` adapter.                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `/v1/completions`                                       | Supported for models that accept OpenRouter's legacy prompt-based completions format.                                                                                                                                                                                                                                                                                                                                                                                             |
| `/v1/responses`                                         | Sent to OpenRouter's native [Responses API](https://openrouter.ai/docs/api/reference/responses/overview) because this guide declares `apis.responses`. Without the declaration, AISIX translates the request through the [Responses bridge](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md).                                                                                                                                                                          |
| `/v1/messages`                                          | Supported through translation to chat completions, not OpenRouter's native [Anthropic Messages API](https://openrouter.ai/docs/api/api-reference/anthropic-messages/create-a-message). Fields and response blocks without a chat equivalent do not round-trip. Use `/passthrough/openrouter/messages` for the native contract.                                                                                                                                                    |
| `/v1/messages/count_tokens`                             | Not available. This AISIX route requires an Anthropic-protocol provider key, while the OpenRouter integration uses the `openai` adapter.                                                                                                                                                                                                                                                                                                                                          |
| `/v1/embeddings`                                        | Supported when the alias points at an OpenRouter embedding model. AISIX forwards the OpenAI embeddings request shape to `https://openrouter.ai/api/v1/embeddings`.                                                                                                                                                                                                                                                                                                                |
| `/v1/audio/speech`                                      | Supported when the alias points at an OpenRouter speech model because the JSON request and upstream path are compatible.                                                                                                                                                                                                                                                                                                                                                          |
| `/v1/audio/transcriptions` and `/v1/audio/translations` | Not supported through these normalized routes. AISIX accepts OpenAI-style multipart uploads, while OpenRouter's transcription endpoint accepts a JSON `input_audio` object and OpenRouter does not document a matching translations route. Use `/passthrough/openrouter/audio/transcriptions` with OpenRouter's native JSON shape.                                                                                                                                                |
| `/v1/images/generations`                                | Not available. The route accepts only models whose provider is `openai` or whose provider key uses the `azure-openai` adapter (see [Provider Requirement](https://docs.api7.ai/ai-gateway/endpoints/image-generation.md#provider-requirement)), and OpenRouter's native image endpoint is `/images`, not `/images/generations`. Use `/passthrough/openrouter/images` with the [OpenRouter image request](https://openrouter.ai/docs/guides/overview/multimodal/image-generation). |
| `/v1/rerank`                                            | Not available. The route accepts only the `openai`, `cohere`, and `jina` provider values. Use `/passthrough/openrouter/rerank` with an OpenRouter rerank model ID for OpenRouter's native route.                                                                                                                                                                                                                                                                                  |
| `/v1/systemone` and `/v1/decisions`                     | Supported with an alias for a model that OpenRouter's System One endpoint serves, such as `typesafe/jev-1.13`. `/v1/systemone` is forwarded natively, and `/v1/decisions` requests are converted. See [Decision Models](https://docs.api7.ai/ai-gateway/endpoints/decision-models.md).                                                                                                                                                                                            |
| `/v1/videos` and its status and content routes          | Not available. The `openrouter` provider value returns `501 not_implemented`. For OpenRouter's native [asynchronous video API](https://openrouter.ai/docs/guides/overview/multimodal/video-generation), submit through `/passthrough/openrouter/videos`, then use the returned job ID with `/passthrough/openrouter/videos/{jobId}` and `/passthrough/openrouter/videos/{jobId}/content`.                                                                                         |
| `/v1/files`                                             | Do not use the normalized file routes for OpenRouter-native file references. AISIX wraps returned file IDs for its job-routing contract and does not unwrap those IDs inside native OpenRouter Messages or Responses bodies. Use `/passthrough/openrouter/files` to preserve OpenRouter's raw file IDs.                                                                                                                                                                           |
| `/v1/batches` and `/v1/fine_tuning/jobs`                | Not available because OpenRouter does not publish matching routes.                                                                                                                                                                                                                                                                                                                                                                                                                |
| `/v1/models`                                            | Returns caller-accessible AISIX model aliases, not OpenRouter's model catalog. Use `/passthrough/openrouter/models` for the native list.                                                                                                                                                                                                                                                                                                                                          |
| `/passthrough/openrouter/*rest`                         | Available through a configured [passthrough route](https://docs.api7.ai/ai-gateway/endpoints/provider-passthrough.md).                                                                                                                                                                                                                                                                                                                                                            |

For routes AISIX does not normalize, use a [passthrough route](https://docs.api7.ai/ai-gateway/endpoints/provider-passthrough.md). The `/passthrough/openrouter` paths on this page assume such a route claiming that prefix with `https://openrouter.ai/api/v1` as its `target_url`; grant the route on the caller key's `allowed_routes`. The gateway appends the remaining path to the route's `target_url` and removes one leading `v1` segment when it duplicates the trailing `/v1` of the target, so both `/passthrough/openrouter/models` and `/passthrough/openrouter/v1/models` reach `https://openrouter.ai/api/v1/models`.

A passthrough route forwards the request body without model-alias rewriting. Send the namespaced OpenRouter model ID, such as `anthropic/claude-sonnet-5`, rather than `openrouter-sonnet-prod`. The route relays SSE responses incrementally. AISIX records token usage for the request shapes listed in [Envelope Detection and Usage](https://docs.api7.ai/ai-gateway/endpoints/provider-passthrough.md#envelope-detection-and-token-usage), which include a rerank body with a top-level `query` and a `documents` array, but passthrough usage carries no cost. Prefer normalized endpoints when their contract is sufficient and you need AISIX cost accounting.

## Next Steps[​](#next-steps "Direct link to Next Steps")

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

* [Model Aliases](https://docs.api7.ai/ai-gateway/models/model-aliases.md): configure routing, retry behavior, or cost metadata for this alias.
* [Routing and Failover](https://docs.api7.ai/ai-gateway/routing/routing-and-failover.md): fail over between OpenRouter and a direct vendor account.
* [Passthrough Routes](https://docs.api7.ai/ai-gateway/endpoints/provider-passthrough.md): reach OpenRouter routes that AISIX does not normalize.
* [Provider Compatibility](https://docs.api7.ai/ai-gateway/providers/compatibility.md): review supported proxy endpoints and provider-specific boundaries.
