Skip to main content
Getting Started

Gemini API (Google)

Use the Gemini-native API at /v1beta/models/{model}:generateContent with the google-genai SDK. Text, images, tools, structured output, streaming, and current limits.

Use this page with an AI assistant

Opens a new chat with this docs URL and the correct AICredits base URLs.

AICredits accepts requests in the Gemini API format, so you can point the google-genai SDK at AICredits and keep your existing code.

MethodPathPurpose
POST/v1beta/models/{model}:generateContentSingle response
POST/v1beta/models/{model}:streamGenerateContentStreaming response

Setup

Use the bare base URL https://api.aicredits.in (without /v1 or /v1beta). The SDK adds /v1beta/models/... itself.

from google import genai
from google.genai import types

client = genai.Client(
    api_key="sk-your-key-here",  # your AICredits API key
    http_options=types.HttpOptions(base_url="https://api.aicredits.in"),
)

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="Explain UPI in one sentence.",
)
print(response.text)
curl "https://api.aicredits.in/v1beta/models/gemini-2.5-flash:generateContent" \
  -H "x-goog-api-key: sk-your-key-here" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"role": "user", "parts": [{"text": "Explain UPI in one sentence."}]}]
  }'

Authentication

Send your AICredits API key in any one of these ways:

  • x-goog-api-key: sk-... header (what the google-genai SDK sends)
  • ?key=sk-... query parameter (accepted on /v1beta/ routes only)
  • Authorization: Bearer sk-... header

Which models work

The {model} in the path can be any model on AICredits, not only Gemini models. The request is converted to chat format internally, routed and billed like any other request, and the answer is converted back to Gemini format. Model IDs containing a slash, such as openai/gpt-4o, work as written in the path.

Streaming

:streamGenerateContent has two output modes:

  • With ?alt=sse, you receive server-sent events, each a data: line holding a Gemini response chunk. The google-genai SDK uses this mode.
  • Without it, the response is a single JSON array of chunks.
curl -N "https://api.aicredits.in/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse" \
  -H "x-goog-api-key: sk-your-key-here" \
  -H "Content-Type: application/json" \
  -d '{"contents": [{"role": "user", "parts": [{"text": "Tell me a short story."}]}]}'

Text arrives chunk by chunk. The closing chunk carries finishReason and usageMetadata.

Tool calls in streams

Function calls are not streamed piece by piece. They arrive whole, as functionCall parts in the closing chunk.

Supported features

  • Text and system prompts: contents with user and model roles, and systemInstruction.
  • Images: inlineData parts with an image/* MIME type and base64 data.
  • generationConfig: temperature, topP, topK, maxOutputTokens, stopSequences, responseMimeType, responseSchema, and responseJsonSchema.
  • Structured output: responseSchema or responseJsonSchema is enforced as a JSON schema. responseMimeType: "application/json" on its own requests plain JSON output.
  • Function calling: tools[].functionDeclarations (with parameters or parametersJsonSchema), and toolConfig.functionCallingConfig with mode AUTO, NONE, or ANY. With ANY and exactly one name in allowedFunctionNames, that function is forced. With ANY and any other number of names, the model is required to call a tool.
  • Tool round trips: functionCall parts from the model and functionResponse parts in your next user turn.
from google.genai import types

get_weather = types.FunctionDeclaration(
    name="get_weather",
    description="Get the weather for a city",
    parameters={
        "type": "OBJECT",
        "properties": {"city": {"type": "STRING"}},
        "required": ["city"],
    },
)

response = client.models.generate_content(
    model="anthropic/claude-sonnet-4.6",
    contents="Weather in Mumbai?",
    config=types.GenerateContentConfig(tools=[types.Tool(function_declarations=[get_weather])]),
)
print(response.function_calls)

Response

Responses use Gemini's shape: candidates with content.parts, a finishReason, usageMetadata (promptTokenCount, candidatesTokenCount, totalTokenCount), modelVersion, and responseId. finishReason is MAX_TOKENS when the output hit the length limit, SAFETY when a content filter stopped it, and STOP otherwise.

Errors

Errors on these routes use Gemini's envelope:

{
  "error": {
    "code": 400,
    "message": "Invalid request format: ...",
    "status": "INVALID_ARGUMENT"
  }
}

Common statuses: 400 INVALID_ARGUMENT, 401 UNAUTHENTICATED, 402 FAILED_PRECONDITION, 403 PERMISSION_DENIED, 404 NOT_FOUND, 429 RESOURCE_EXHAUSTED, 501 UNIMPLEMENTED, 503 UNAVAILABLE, 504 DEADLINE_EXCEEDED. Other 5xx errors return INTERNAL.

Limitations

  • :countTokens is not supported. It returns 501 UNIMPLEMENTED. Read token usage from usageMetadata on each response instead.
  • topK only reaches Anthropic-routed models. For other models it is accepted and ignored.
  • Images only for inlineData. Audio, video, PDF, and other non-image MIME types return 400.
  • Ignored fields: thought parts, candidateCount above 1 (you get one candidate), and Gemini built-in tools such as googleSearch and codeExecution. Only functionDeclarations are used from tools.
  • Request body: 10 MB maximum.

For the broadest provider compatibility, Chat Completions is the most fully featured surface.