Generate content, stream content, or count tokens
const url = 'https://api.saygm.com/v1beta/models/gemini-3.6-flash%3AgenerateContent?alt=sse';const options = { method: 'POST', headers: { 'X-GM-Routing': 'default', Authorization: 'Bearer <token>', 'Content-Type': 'application/json' }, body: '{"contents":[{"role":"user","parts":[{"text":"example","inlineData":{},"functionCall":{},"functionResponse":{}}]}],"generationConfig":{"maxOutputTokens":1,"candidateCount":1,"temperature":1,"topP":1,"topK":1},"systemInstruction":{"role":"user","parts":[{"text":"example","inlineData":{},"functionCall":{},"functionResponse":{}}]},"safetySettings":[{}],"tools":[{}],"toolConfig":{}}'};
try { const response = await fetch(url, options); const data = await response.json(); console.log(data);} catch (error) { console.error(error);}curl --request POST \ --url 'https://api.saygm.com/v1beta/models/gemini-3.6-flash%3AgenerateContent?alt=sse' \ --header 'Authorization: Bearer <token>' \ --header 'Content-Type: application/json' \ --header 'X-GM-Routing: default' \ --data '{ "contents": [ { "role": "user", "parts": [ { "text": "example", "inlineData": {}, "functionCall": {}, "functionResponse": {} } ] } ], "generationConfig": { "maxOutputTokens": 1, "candidateCount": 1, "temperature": 1, "topP": 1, "topK": 1 }, "systemInstruction": { "role": "user", "parts": [ { "text": "example", "inlineData": {}, "functionCall": {}, "functionResponse": {} } ] }, "safetySettings": [ {} ], "tools": [ {} ], "toolConfig": {} }'Gemini-compatible model action endpoint. modelAndAction joins a model id and action with :. Supported actions are countTokens, generateContent, and streamGenerateContent; streaming requires alt=sse. Authenticate with a SayGM API key using x-goog-api-key, Authorization: Bearer, or the key query parameter. X-GM-Routing and X-GM-Providers apply to the generation actions.
Authorizations
Section titled “Authorizations”Parameters
Section titled “Parameters”Path Parameters
Section titled “Path Parameters”Model id and supported action joined by :.
Examples
Non-streaming generation
gemini-3.6-flash:generateContentSSE generation
gemini-3.6-flash:streamGenerateContentToken count
gemini-3.6-flash:countTokensQuery Parameters
Section titled “Query Parameters”Must be sse when modelAndAction uses streamGenerateContent; the gateway rejects other or missing values for that action.
Example
sseHeader Parameters
Section titled “Header Parameters”Per-request routing mode. default (or omitting the header) follows GM’s configured routing; cheapest ranks eligible routes by predicted final charge, preferring routes with at least 95% recent reliability; fastest ranks them by measured time to first token and output speed. Predictions are not a price cap or latency guarantee. A conversation stays on its current healthy route regardless of mode. Send the header at most once. Fusion, multi-model cascade, and some multimodal requests reject cheapest and fastest with 400.
Comma-separated allowlist of provider identifiers that may serve the request. Use 1 to 16 distinct case-insensitive names, each 1 to 64 letters, digits, underscores, or hyphens, starting with a letter or digit. The restriction applies under every supported routing mode and on every retry. Invalid values return 400 with code invalid_provider_restriction; a model with no route on a listed provider returns 400 with code no_allowed_provider; listed providers that cannot serve now return 503. Fusion and multi-model cascade requests reject this header with 400.
Request Bodyrequired
Section titled “Request Bodyrequired”The provider-facing fields required by Gemini GenerateContent. Additional provider fields are forwarded unchanged.
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
Request fields are passed through for the countTokens action.
object
Responses
Section titled “Responses”A JSON response for countTokens or generateContent, or an SSE stream for streamGenerateContent.
object
object
object
object
object
object
object
object
object
Example
{ "candidates": [ { "content": { "role": "user" } } ]}Invalid request, unsupported action, or an upstream provider 400 response.
Gemini-shaped gateway error. Billing fields are included after dispatch.
object
object
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
Example
{ "billing_status": "settled"}The API key is missing or invalid.
Gemini-shaped gateway error. Billing fields are included after dispatch.
object
object
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
Example
{ "billing_status": "settled"}Insufficient prepaid balance on generateContent and streamGenerateContent actions. The error message links to the funding page; when the available balance is nonzero it also reports estimated maximum cost and request-sizing guidance.
Gemini-shaped gateway error. Billing fields are included after dispatch.
object
object
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
Examples
No available balance
{ "error": { "code": 402, "message": "Your gm balance is empty. Add credits: https://saygm.com/topup", "status": "PERMISSION_DENIED" }}Estimated maximum cost exceeds available balance
{ "error": { "code": 402, "message": "This request's estimated maximum cost of $1.01 exceeds your available balance of $0.51. Add credits: https://saygm.com/topup. Try shortening the conversation or attachments, or lowering max output tokens.", "status": "PERMISSION_DENIED" }}Guardrail block (guardrail_blocked).
OpenAI-shaped gateway error. Image errors after dispatch may also include billing fields at the top level.
object
object
Machine-readable error code. Account request limits use rate_limit_exceeded.
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
object
object
Gateway request identifier.
Gemini-shaped gateway error. Billing fields are included after dispatch.
object
object
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
Example
{ "billing_status": "settled"}Request body too large (request_too_large).
OpenAI-shaped gateway error. Image errors after dispatch may also include billing fields at the top level.
object
object
Machine-readable error code. Account request limits use rate_limit_exceeded.
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
object
object
Gateway request identifier.
Gemini-shaped gateway error. Billing fields are included after dispatch.
object
object
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
Example
{ "billing_status": "settled"}Upstream 422 response.
OpenAI-shaped gateway error. Image errors after dispatch may also include billing fields at the top level.
object
object
Machine-readable error code. Account request limits use rate_limit_exceeded.
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
object
object
Gateway request identifier.
Gemini-shaped gateway error. Billing fields are included after dispatch.
object
object
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
Example
{ "billing_status": "settled"}Account, model, or upstream request limit. OpenAI-shaped account-limit errors use rate_limit_exceeded.
OpenAI-shaped gateway error. Image errors after dispatch may also include billing fields at the top level.
object
object
Machine-readable error code. Account request limits use rate_limit_exceeded.
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
object
object
Gateway request identifier.
Gemini-shaped gateway error. Billing fields are included after dispatch.
object
object
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
Example
{ "billing_status": "settled"}Headers
Section titled “Headers”Seconds before retrying, when a delay is available.
Gateway internal error (internal_error).
OpenAI-shaped gateway error. Image errors after dispatch may also include billing fields at the top level.
object
object
Machine-readable error code. Account request limits use rate_limit_exceeded.
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
object
object
Gateway request identifier.
Gemini-shaped gateway error. Billing fields are included after dispatch.
object
object
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
Example
{ "billing_status": "settled"}Upstream connection failure (upstream_connect_failed).
OpenAI-shaped gateway error. Image errors after dispatch may also include billing fields at the top level.
object
object
Machine-readable error code. Account request limits use rate_limit_exceeded.
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
object
object
Gateway request identifier.
Gemini-shaped gateway error. Billing fields are included after dispatch.
object
object
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
Example
{ "billing_status": "settled"}The credit service, guardrail policy, or model serving is unavailable.
OpenAI-shaped gateway error. Image errors after dispatch may also include billing fields at the top level.
object
object
Machine-readable error code. Account request limits use rate_limit_exceeded.
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
object
object
Gateway request identifier.
Gemini-shaped gateway error. Billing fields are included after dispatch.
object
object
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
Example
{ "billing_status": "settled"}Headers
Section titled “Headers”Seconds before retrying, when a delay is available.
Upstream timeout (upstream_timeout) or idle stream timeout (upstream_stream_idle_timeout).
OpenAI-shaped gateway error. Image errors after dispatch may also include billing fields at the top level.
object
object
Machine-readable error code. Account request limits use rate_limit_exceeded.
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
object
object
Gateway request identifier.
Gemini-shaped gateway error. Billing fields are included after dispatch.
object
object
Gateway request identifier.
Settled charge in nano-dollars; present when billing_status is settled.
Example
{ "billing_status": "settled"}