# OpenAI SDK

The
[OpenAI Node.js SDK](https://platform.openai.com/docs/libraries/node-js-library)
is the official SDK for working with LLMs provided by OpenAI as well as other
OpenAI compatible models from other providers in Node.js.

## Prerequisites

In order to use the AI Gateway with any OpenAI SDK powered app you will need to
complete these steps first:

<Stepper>

1. Create a [new provider](../managing-providers.mdx) in the AI Gateway for
   OpenAI

2. [Set up a new pool](../managing-pools.mdx)

3. Create a [new app](../managing-apps.mdx) to use specifically with the OpenAI
   SDK and assign it to the pool you created

4. Copy the **API URL** and **API Key** shown at the top of the app page

</Stepper>

## Configure the OpenAI SDK

To route all OpenAI SDK requests through Zuplo instead of directly to the OpenAI
API, you must set the API `baseUrl` in the SDK configuration.

The app page in the Zuplo Portal shows each app's API URL. It ends in the app's
ID, so append `/v1` to it to get the base URL for your API client.

Additionally, change the value of `apiKey` to the API key of the app you have
configured in Zuplo

Always name the model as `providerName/model`, where `providerName` is the
provider name configured in your gateway. The gateway needs the prefix to know
which provider to route to, and a request with no model, or a model without that
prefix, gets a `400`. Adding the
[Model Filtering](../../policies/ai-gateway-model-filtering-inbound.mdx) policy
restricts an app to certain models, and an allow list also supplies a default so
requests may omit the model.

```typescript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.ZUPLO_AI_GATEWAY_API_KEY,
  baseURL:
    "https://my-gateway-main-2e18f50.zuplo.app/config_fe0a04972d2848e0a94ae4b8bcd1497e/v1",
});

const response = await client.chat.completions.create({
  model: "openai/gpt-6-luna",
  messages: [
    {
      role: "user",
      content: "Write a one-sentence bedtime story about a unicorn.",
    },
  ],
});

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

When configured in this way, the SDK will switch from using the default OpenAI
APIs to using Zuplo's AI Gateway for all requests.

## Supported Endpoints

The AI Gateway supports all major OpenAI API endpoints through the
[universal API](../universal-api.mdx):

- **Chat Completions** (`/v1/chat/completions`) - For conversational AI
  interactions
- **Embeddings** (`/v1/embeddings`) - For generating vector embeddings
- **Responses** (`/v1/responses`) - For OpenAI models that support the responses
  endpoint (such as GPT-6.1 Sol and other compatible models)

## Using the Responses Endpoint

For models that support the `/v1/responses` endpoint, you can use the OpenAI
SDK's `responses` API:

```typescript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.ZUPLO_AI_GATEWAY_API_KEY,
  baseURL:
    "https://my-gateway-main-2e18f50.zuplo.app/config_fe0a04972d2848e0a94ae4b8bcd1497e/v1",
});

const response = await client.responses.create({
  model: "openai/gpt-6.1-sol",
  input: "Write a one-sentence bedtime story about a unicorn.",
});

console.log(response.output_text);
```

The responses endpoint is automatically routed through the AI Gateway, providing
the same monitoring, cost controls, and security features as other endpoints.
