> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini API quickstart: Use native and OpenAI-compatible requests with CometAPI

> Call Gemini text models through CometAPI with native generateContent requests or OpenAI-compatible Chat Completions requests.

## What you will build

You will send a native Gemini `POST /v1beta/models/\{model\}:generateContent` request, then compare it with the OpenAI-compatible `POST /v1/chat/completions` option for apps that already use Chat Completions request shapes.

## Prerequisites

* A CometAPI API key stored in `COMETAPI_KEY`
* A Gemini text model ID from the [Models page](/overview/models)
* `curl`, Python 3.10+, or Node.js 18+

## API key, base URL, authentication

Use the Gemini native endpoint when you want Gemini request fields:

```text theme={null}
https://api.cometapi.com/v1beta/models/{model}:generateContent
```

Use `x-goog-api-key` for direct native Gemini HTTP requests:

```text theme={null}
x-goog-api-key: $COMETAPI_KEY
```

Use the OpenAI-compatible base URL only when your application already uses Chat Completions:

```text theme={null}
https://api.cometapi.com/v1
```

## Native Gemini format

Native Gemini requests use `contents`, `parts`, and `generationConfig`. Use this path when you need Gemini-specific fields such as thinking controls, media parts, Google Search grounding, or native streaming operators.

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://api.cometapi.com/v1beta/models/your-gemini-model-id:generateContent" \
    -H "x-goog-api-key: $COMETAPI_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "contents": [
        {
          "parts": [
            {
              "text": "Explain why base URL configuration matters."
            }
          ]
        }
      ],
      "generationConfig": {
        "temperature": 0.3
      }
    }'
  ```

  ```python Python theme={null}
  import os
  from google import genai

  client = genai.Client(
      api_key=os.environ["COMETAPI_KEY"],
      http_options={"api_version": "v1beta", "base_url": "https://api.cometapi.com"},
  )

  response = client.models.generate_content(
      model="your-gemini-model-id",
      contents="Explain why base URL configuration matters.",
      config={
          "temperature": 0.3,
      },
  )

  print(response.text)
  ```

  ```javascript Node.js theme={null}
  import { GoogleGenAI } from "@google/genai";

  const ai = new GoogleGenAI({
    apiKey: process.env.COMETAPI_KEY,
    httpOptions: {
      baseUrl: "https://api.cometapi.com",
      apiVersion: "v1beta",
    },
  });

  const response = await ai.models.generateContent({
    model: "your-gemini-model-id",
    contents: "Explain why base URL configuration matters.",
    config: {
      temperature: 0.3,
    },
  });

  console.log(response.text);
  ```
</CodeGroup>

## OpenAI-compatible option

Use the OpenAI-compatible route when you are migrating an existing OpenAI SDK or Chat Completions app and do not need Gemini native request fields.

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.cometapi.com/v1/chat/completions \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "your-gemini-model-id",
      "messages": [
        {
          "role": "user",
          "content": "Explain why base URL configuration matters."
        }
      ]
    }'
  ```

  ```python Python theme={null}
  import os
  from openai import OpenAI

  client = OpenAI(
      api_key=os.environ["COMETAPI_KEY"],
      base_url="https://api.cometapi.com/v1",
  )

  completion = client.chat.completions.create(
      model="your-gemini-model-id",
      messages=[
          {
              "role": "user",
              "content": "Explain why base URL configuration matters.",
          }
      ],
  )

  print(completion.choices[0].message.content)
  ```

  ```javascript Node.js theme={null}
  import OpenAI from "openai";

  const client = new OpenAI({
    apiKey: process.env.COMETAPI_KEY,
    baseURL: "https://api.cometapi.com/v1",
  });

  const completion = await client.chat.completions.create({
    model: "your-gemini-model-id",
    messages: [
      {
        role: "user",
        content: "Explain why base URL configuration matters.",
      },
    ],
  });

  console.log(completion.choices[0].message.content);
  ```
</CodeGroup>

## Flow explanation

| Path              | Endpoint                                        | Request shape                                      | SDK                             | Use when                                                                                              |
| ----------------- | ----------------------------------------------- | -------------------------------------------------- | ------------------------------- | ----------------------------------------------------------------------------------------------------- |
| Gemini native     | `POST /v1beta/models/\{model\}:generateContent` | `contents`, `parts`, `generationConfig`            | Google GenAI SDK or direct HTTP | You need Gemini-specific fields, multimodal parts, thinking controls, grounding, or native streaming. |
| OpenAI-compatible | `POST /v1/chat/completions`                     | `messages`, `temperature`, `max_completion_tokens` | OpenAI SDK or direct HTTP       | Your app already uses Chat Completions and only needs a Gemini text model behind that shape.          |

Do not mix the two request formats. Native Gemini fields such as `contents` and `generationConfig` belong on the `generateContent` route. Chat Completions fields such as `messages` belong on the OpenAI-compatible route.

## Troubleshooting / FAQ

<AccordionGroup>
  <Accordion title="Which path should I start with">
    Start with native Gemini `generateContent` when you are building a new Gemini workflow. Use the OpenAI-compatible route when an existing app already depends on OpenAI SDK or Chat Completions request shapes.
  </Accordion>

  <Accordion title="Native Gemini fields fail on Chat Completions">
    Send `contents`, `parts`, `generationConfig`, and `streamGenerateContent` requests to the Gemini native endpoint. The OpenAI-compatible route expects `messages` and Chat Completions parameters.
  </Accordion>

  <Accordion title="The Gemini model ID fails">
    Confirm that the model ID is available to your account and supports the route you are calling. Use the [Models page](/overview/models) to find current model IDs.
  </Accordion>

  <Accordion title="The SDK points to the wrong service">
    For Google GenAI SDK requests, set the base URL to `https://api.cometapi.com`. For OpenAI SDK requests, set `base_url` in Python or `baseURL` in Node.js to `https://api.cometapi.com/v1`.
  </Accordion>
</AccordionGroup>

## Next steps

* Use the [Gemini native API reference](/api/text/gemini-generating-content) for full `generateContent` request and response fields.
* Read the [Chat Completions API reference](/api/text/chat) for the OpenAI-compatible request shape.
* Configure OpenAI SDK clients in [Use CometAPI with OpenAI SDKs](/guides/use-cometapi-with-openai-sdk).
* List available models with [List available CometAPI models](/guides/how-to-list-available-models).
* Add retry and rate-limit handling with [Error codes and retry strategy](/guides/error-codes-and-retry-strategy).
