Skip to main content
Complete the Quickstart to create an access key on app.ngrok.ai before continuing.
The AI Gateway is compatible with OpenAI’s official SDKs. Set the base URL to https://gateway.ngrok.ai/v1 and use your access key.

Installation

Basic usage

Point the SDK at your AI Gateway endpoint and pass your access key as api_key. See Access keys vs provider keys for how credentials flow through the gateway.

Try using this prompt on your agent to verify your gateway connection: 'State your exact model name and provider.'

Open in Cursor

Streaming

The AI Gateway supports streaming responses:

Try using this prompt on your agent to test streaming — tokens should appear one by one: 'Write a haiku about APIs'

Open in Cursor

Using different providers

Route to different providers using model prefixes:

Model failover

List fallback models in the request:
To try another model when the first one fails, see Configure fallback models.

Function calling

Tool/function calling works exactly as documented by OpenAI:

Try using this prompt on your agent to test tool calling — your get_weather function should be invoked: 'What is the current weather in Paris?'

Open in Cursor

Async usage

Use async clients for better performance:

Error handling

The gateway handles many errors automatically through failover. For errors that reach your app:

Supported endpoints

The AI Gateway supports these OpenAI API endpoints:

Next steps