Use the OpenAI SDK with Fabric AI Gateway
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.
- A gateway key for the route. Send it in the
fg-api-keyheader, which every route accepts, unless its Key Auth instance is an older one that sets aheaderslist. 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. - 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:
Python
TypeScript
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.
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:
- Run your code. A successful request prints the model’s reply.
- In the Fabric console, select Traces. To see traces from every namespace, select Admin, and then select Traces.
- 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.
Next steps
- To call other endpoints through the route, see Use the OpenAI-compatible API.
- To fail over to another provider when OpenAI returns errors, see Fail over across providers with the LLM Router.
- To use the Vercel AI SDK instead, see Use the Vercel AI SDK with Fabric AI Gateway.

