> ## 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.

# OpenAI-compatible API quickstart: Send Chat Completions requests with CometAPI

> Use CometAPI as an OpenAI API-compatible base URL for Chat Completions requests with curl, Python, Node.js, and OpenAI SDKs.

This page is a CometAPI OpenAI-compatible API quickstart. It helps you reuse the Chat Completions request shape, OpenAI SDKs, and a CometAPI base URL. It is not an OpenAI account setup guide or an OpenAI-only model page.

## What you will build

You will send one text request to CometAPI's OpenAI-compatible `POST /v1/chat/completions` route, print the assistant message, and keep the request shape ready for apps that already use OpenAI SDKs.

## When to use this page

Use this quickstart when one of these matches your project:

* You already use OpenAI SDKs or Chat Completions request shapes.
* You want to switch the base URL to CometAPI.
* You want to call a CometAPI model ID through an OpenAI API-compatible route.

## Prerequisites

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

## API key, base URL, authentication

Use the CometAPI base URL with OpenAI-compatible clients:

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

Authenticate direct HTTP requests with a Bearer token:

```text theme={null}
Authorization: Bearer $COMETAPI_KEY
```

## Code examples

Use the tabs below to send the same Chat Completions request with cURL, Python, and Node.js.

<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-model-id",
      "messages": [
        {
          "role": "user",
          "content": "Write one sentence about CometAPI."
        }
      ]
    }'
  ```

  ```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-model-id",
      messages=[
          {
              "role": "user",
              "content": "Write one sentence about CometAPI.",
          }
      ],
  )

  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-model-id",
    messages: [
      {
        role: "user",
        content: "Write one sentence about CometAPI.",
      },
    ],
  });

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

## Flow explanation

OpenAI-compatible means your application keeps the Chat Completions endpoint, request body, and SDK method names while changing the base URL and model ID to CometAPI values.

The route is synchronous by default. The API returns the completed response object in one HTTP response, and your application reads `choices[0].message.content`.

For incremental output, set `stream` to `true`. The response becomes Server-Sent Events and ends with `data: [DONE]`. Use streaming for chat interfaces and long responses. Keep the synchronous form for background jobs and simple tests.

## Common parameters

| Parameter               | Use                                                                          |
| ----------------------- | ---------------------------------------------------------------------------- |
| `model`                 | The CometAPI model ID for a text-capable model.                              |
| `messages`              | The conversation array. Start with one `user` message for a minimal request. |
| `temperature`           | Controls randomness. Lower values make output more deterministic.            |
| `max_completion_tokens` | Caps generated output for model families that use completion-token budgets.  |
| `stream`                | Streams incremental response chunks when set to `true`.                      |
| `response_format`       | Requests JSON output when the selected model supports it.                    |

## Troubleshooting and FAQ

<AccordionGroup>
  <Accordion title="Is this the OpenAI API?">
    No. This is CometAPI's OpenAI-compatible API route. You use a CometAPI API key, the CometAPI base URL, and a CometAPI model ID.
  </Accordion>

  <Accordion title="The request returns 401">
    Confirm that `COMETAPI_KEY` is set in the same shell or runtime that sends the request. Do not paste a real key into source files.
  </Accordion>

  <Accordion title="The model is not found">
    Use a CometAPI model ID that supports text or chat requests. Check the Models page before retrying.
  </Accordion>

  <Accordion title="The SDK still calls OpenAI">
    Confirm the client sets `base_url` in Python or `baseURL` in Node.js to `https://api.cometapi.com/v1`.
  </Accordion>
</AccordionGroup>

## Next steps

* Read the [Chat Completions API reference](/api/text/chat).
* Configure 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).
