# OpenAI SDK

In this guide, you will point the official OpenAI SDK at an AISIX gateway instead of sending requests directly to an upstream provider. This flow fits applications that already use the OpenAI-compatible chat-completions format and need to keep that client code mostly unchanged.

The example authenticates to AISIX with a caller API key, sends requests to the AISIX proxy API root, uses an AISIX model alias, and receives OpenAI-compatible chat-completions responses. The upstream provider can still be changed behind the gateway when the configured model alias and provider support that request shape.

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

* A running AISIX gateway that your application can reach.
* A configured model alias that accepts OpenAI-compatible chat-completions requests.
* A caller API key allowed to use that model alias.
* Install Node.js 20 LTS or newer with `npm`.

If AISIX is already deployed for your organization, obtain the gateway URL, model alias, and caller API key from the team that manages it. Otherwise, [get started with AISIX OSS](https://docs.api7.ai/ai-gateway/getting-started/gateway-quickstart.md) or [AISIX Enterprise](https://docs.api7.ai/ai-gateway/getting-started/aisix-cloud-quickstart.md), or [contact API7](https://api7.ai/contact) for AISIX Cloud access.

## Configure the SDK[​](#configure-the-sdk "Direct link to Configure the SDK")

Create a small Node.js project, install the SDK, and point it at the AISIX proxy API root.

### Install the SDK[​](#install-the-sdk "Direct link to Install the SDK")

Create a small demo project:

```
mkdir aisix-openai-demo && cd aisix-openai-demo

npm init -y
```

Install the OpenAI SDK in the demo project:

```
npm install openai
```

Set the caller API key, model alias, and gateway base URL:

```
# AISIX_BASE_URL ends in /v1 and has no trailing slash

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

export AISIX_BASE_URL="YOUR_AISIX_GATEWAY_URL/v1"

export AISIX_API_KEY="YOUR_CALLER_API_KEY"

export AISIX_MODEL="gpt-4o-mini"
```

### Create the Chat Example[​](#create-the-chat-example "Direct link to Create the Chat Example")

Use the `.mjs` extension so Node treats top-level `await` and `import` as ES modules without extra configuration.

openai-sdk-example.mjs

```
import OpenAI from "openai";



const client = new OpenAI({

  apiKey: process.env.AISIX_API_KEY,

  baseURL: process.env.AISIX_BASE_URL,

});



const response = await client.chat.completions.create({

  model: process.env.AISIX_MODEL ?? "gpt-4o-mini",

  messages: [{ role: "user", content: "Say hello from AISIX." }],

});



console.log(response.choices[0]?.message.content);
```

### Run the Example[​](#run-the-example "Direct link to Run the Example")

Run the chat example from the demo project:

```
node openai-sdk-example.mjs
```

You should see a short assistant response. The exact text depends on the upstream model.

When the gateway can resolve `gpt-4o-mini` and the upstream provider is reachable, the SDK returns a standard OpenAI chat-completions object with an assistant message. AISIX resolves the model alias and injects the stored provider credential before calling the upstream provider.

## Streaming Responses[​](#streaming-responses "Direct link to Streaming Responses")

The same `baseURL` works for streaming.

openai-sdk-streaming.mjs

```
import OpenAI from "openai";



const client = new OpenAI({

  apiKey: process.env.AISIX_API_KEY,

  baseURL: process.env.AISIX_BASE_URL,

  maxRetries: 0,

});



const stream = await client.chat.completions.create({

  model: process.env.AISIX_MODEL ?? "gpt-4o-mini",

  messages: [{ role: "user", content: "Stream a short greeting." }],

  stream: true,

});



for await (const chunk of stream) {

  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");

}
```

Run the streaming example from the demo project:

```
node openai-sdk-streaming.mjs
```

You should see streamed text printed to the terminal.

## Production Setup Pattern[​](#production-setup-pattern "Direct link to Production Setup Pattern")

In most deployments, application code needs only the gateway base URL, caller API key, and AISIX model alias. Upstream credentials, provider base URLs, upstream model IDs, routing policies, rate limits, guardrails, and observability hooks stay behind the gateway.

This separation lets you rotate provider credentials, change upstream model IDs, or add gateway policy without changing the SDK call site.

## If the SDK Request Fails[​](#if-the-sdk-request-fails "Direct link to If the SDK Request Fails")

First check the caller API key, gateway base URL, and AISIX model alias with a direct request to the gateway. Then use the same values in the SDK.

If the SDK still sends traffic directly to OpenAI, check `baseURL`. It must point to the AISIX proxy API root, not to the upstream OpenAI API URL.

If AISIX returns `404`, the requested model alias is not configured. If AISIX returns `403`, the caller API key exists but is not allowed to use that alias.

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

You have now called AISIX from an OpenAI SDK client. From here, review [OpenAI-Compatible API](https://docs.api7.ai/ai-gateway/endpoints/openai-compatible-chat.md) for proxy behavior. For SSE responses and tool definitions, see [Streaming](https://docs.api7.ai/ai-gateway/endpoints/streaming.md) and [Tool Calling](https://docs.api7.ai/ai-gateway/endpoints/tool-calling.md). If your application expects Claude-style requests, continue with [Anthropic SDK](https://docs.api7.ai/ai-gateway/getting-started/anthropic-sdk.md).
