> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.fabricgateway.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.fabricgateway.ai/_mcp/server.

# Use the OpenAI SDK with Fabric AI Gateway

> Point the OpenAI SDK at a Fabric AI Gateway route and send your gateway key in the fg-api-key header.

You can send OpenAI SDK traffic through Fabric AI Gateway by changing the client's base URL to a gateway route and adding your gateway key as a default header. Your code doesn't change otherwise. This topic shows you how to configure the Python and TypeScript SDKs and confirm that requests arrive.

## Prerequisites

Before you begin, make sure you have the following:

* The OpenAI SDK for Python (`openai`) or TypeScript (`openai`).
* A route for an OpenAI connection. For the steps, see [Connect an LLM provider](/configure/connections/llm-providers).
* A gateway key for the route. Send it in the `fg-api-key` header, which every route accepts, unless its Key Auth instance is an older one that sets a `headers` list. In that case, send the key in one of the listed headers, and ask your admin which. For more information, see [Authenticate and send a request](/use/authenticate).
* The base URL of the route. In the console, select **LLMs** > **Routes**, find the route, and then select **Copy LLM base URL**.

## Configure the client

Set the client's base URL to the route's base URL followed by `/v1`, and send the gateway key in the `fg-api-key` header. Keep the key in an environment variable:

```bash
export FABRIC_GATEWAY_KEY="GATEWAY_KEY"
```

#### Python

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="BASE_URL/v1",
    api_key="unused",
    default_headers={"fg-api-key": os.environ["FABRIC_GATEWAY_KEY"]},
)

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello from Fabric Gateway!"}],
)
print(response.choices[0].message.content)
```

#### TypeScript

```typescript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "BASE_URL/v1",
  apiKey: "unused",
  defaultHeaders: { "fg-api-key": process.env.FABRIC_GATEWAY_KEY! },
});

const response = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "Hello from Fabric Gateway!" }],
});
console.log(response.choices[0].message.content);
```

Replace `BASE_URL` with the URL that you copied, and `GATEWAY_KEY` with your gateway key. Use a model that the connection allows.

The SDK requires an API key, but you don't need a real OpenAI key. When it forwards the request, the gateway adds the OpenAI key saved on the connection in place of the SDK's `Authorization` header, so any placeholder value works.

> **Warning**
>
> Treat the gateway key as a secret. Don't commit it to source control.

## Confirm that requests pass through the gateway

To confirm that the SDK uses the gateway, do the following:

1. Run your code. A successful request prints the model's reply.
2. In the Fabric console, select **Traces**. To see traces from every namespace, select **Admin**, and then select **Traces**.
3. Find the request on the route.

If the request returns `401 Unauthorized`, check that the `fg-api-key` header carries a key for this route. For more information, see [Troubleshoot authentication errors](/configure/authentication/key-auth#troubleshoot-authentication-errors).

## Next steps

* To call other endpoints through the route, see [Use the OpenAI-compatible API](/use/llm/openai-compatible).
* To fail over to another provider when OpenAI returns errors, see [Fail over across providers with the LLM Router](/configure/routing/failover).
* To use the Vercel AI SDK instead, see [Use the Vercel AI SDK with Fabric AI Gateway](/guides/sdks/vercel-ai).