Skip to main content
Prerequisite: Complete the quickstart to create an access key. See Google provider setup to configure Google routing or bring your own Google key.
Use Google’s Gen AI SDK for Python or TypeScript with the AI Gateway. Set the base URL to https://gateway.ngrok.ai, use API version v1beta, and authenticate with your ngrok access key.
Google models also work through OpenAI-compatible Chat Completions. See the OpenAI SDK guide to use your existing OpenAI client with a Google model name. The OpenAI Responses API (/v1/responses) doesn’t reach Google models.

Installation

Basic usage

Set NGROK_AI_GATEWAY_KEY to your ngrok access key, then send a GenerateContent request:

Streaming

Use the SDK’s streaming method to print text as it arrives:

Interactions

Send model and input to create a stateless interaction. Set store=false explicitly:
Interactions through the gateway are stateless. The gateway rejects store=true and sends every valid request to Google with store=false, even if you omit store or set it to null. Both ngrok.ai inference and bring your own key (BYOK) requests have these restrictions:
  • Stored conversation chaining (previous_interaction_id) and background execution (background=true) are unsupported.
  • Interaction retrieval, deletion, cancellation, and stream resumption are unsupported.
  • Agents, environments, webhooks, and triggers are unsupported.
Managed Interactions through ngrok.ai inference supports text input and text or structured JSON output only. Use the Standard tier: omit service_tier or set it to standard. The gemini-3.1-pro-preview and gemini-3.1-pro-preview-customtools models require BYOK for Interactions because they use context-band pricing. With BYOK, supported input and output formats and service tiers depend on Google and the selected model.

Authentication

Use your ngrok access key to authenticate requests to the gateway. The gateway checks credentials in this order:
  1. Authorization: Bearer <NGROK_AI_GATEWAY_KEY>
  2. X-Api-Key: <NGROK_AI_GATEWAY_KEY>
  3. X-Goog-Api-Key: <NGROK_AI_GATEWAY_KEY>, which the Google SDK sets from api_key in Python or apiKey in TypeScript.
The URL query parameter key fills an empty X-Goog-Api-Key header. It doesn’t override a header credential. Prefer headers to keep credentials out of URLs. Your access key identifies your gateway configuration. A Google provider key is a separate credential stored in the gateway. For BYOK, attach the stored Google key to your configuration; continue to send your ngrok access key from the client. The gateway replaces client credentials with the selected Google provider credential before forwarding the request.

Supported endpoints

The gateway supports these native Gemini endpoints:

Generate content requests

Point a Gemini client at https://gateway.ngrok.ai and send your access key as described in Authentication. Use POST /v1beta/models/{model}:generateContent for a JSON response. For streaming, use POST /v1beta/models/{model}:streamGenerateContent?alt=sse. The alt=sse query parameter is required.
Use Google’s native GenerateContent payload format. The gateway filters request parameters for the selected backend and applies output-token limits. Responses include the gateway response headers: Ngrok-AIG-Request-Id on every response, Ngrok-AIG-Attempts once the upstream attempt loop runs, and Ngrok-AIG-Model on success. See Response headers.

Interactions requests

Send POST /v1beta/interactions with model and input in the request body. The REST base URL is https://gateway.ngrok.ai/v1beta/. Omit stream or set it to false for a JSON response. Set stream=true for server-sent events (SSE). Responses use Google’s native Interactions format and include the gateway response headers.

Next steps