Skip to main content

OpenAI SDK Quickstart

In this guide, you will point the official OpenAI SDK at AISIX AI 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

  • Complete the Quickstart.
  • Install Node.js 20 LTS or newer with npm.

Configure the SDK

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

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 gateway values that the examples use:

export AISIX_API_KEY="sk-demo-caller"
export AISIX_MODEL="gpt-4o-mini"
export AISIX_BASE_URL="http://127.0.0.1:3000/v1"

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 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

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

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

First check that the SDK uses the same gateway values that worked in the main quickstart: the caller API key, gateway base URL, and AISIX model alias.

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

You have now called AISIX from an OpenAI SDK client. From here, review OpenAI-Compatible API for proxy behavior. For SSE responses and tool definitions, see Streaming and Tool Calling. If your application expects Claude-style requests, continue with Anthropic SDK Quickstart.

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