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

# Use CometAPI with OpenAI SDKs

> Configure the OpenAI Python and Node.js SDKs for CometAPI by setting the CometAPI API key and base URL.

Use the OpenAI SDK with CometAPI by changing two client settings: the API key and the base URL. Keep your existing OpenAI-compatible request code, then replace the model ID with an available CometAPI model ID.

## Install the SDK

The following command installs the OpenAI Python SDK:

```bash theme={null}
pip install openai
```

The following command installs the OpenAI Node.js SDK:

```bash theme={null}
npm install openai
```

## Python client

The following Python example creates a CometAPI client and sends a chat request:

```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": "Answer in one short sentence: What is CometAPI?",
        }
    ],
)

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

The response object contains the assistant message:

```json theme={null}
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "CometAPI provides API access to models from multiple providers."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 16,
    "completion_tokens": 12,
    "total_tokens": 28
  }
}
```

## Node.js client

The following Node.js example uses `baseURL` with the OpenAI SDK:

```javascript 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: "Answer in one short sentence: What is CometAPI?",
    },
  ],
});

console.log(completion.choices[0].message.content);
```

The response shape is the same OpenAI-compatible chat completion shape:

```json theme={null}
{
  "id": "chatcmpl_example",
  "object": "chat.completion",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "CometAPI provides API access to models from multiple providers."
      }
    }
  ]
}
```

## Common errors

| Error                  | Fix                                                                         |
| ---------------------- | --------------------------------------------------------------------------- |
| `401`                  | Confirm that `COMETAPI_KEY` is set and that the request uses `Bearer` auth. |
| SDK still calls OpenAI | Set `base_url` in Python or `baseURL` in Node.js.                           |
| Invalid model ID       | Choose a model ID from the [Models page](/overview/models).                 |
| Missing `/v1`          | Use `https://api.cometapi.com/v1` for OpenAI-compatible SDKs.               |

## Related links

* [CometAPI quickstart](/overview/quick-start)
* [Chat Completions](/api/text/chat)
* [Responses](/api/text/responses)
* [Models page](/overview/models)
* [Model directory](https://www.cometapi.com/models/)
* [Pricing](https://www.cometapi.com/pricing/)
* [OpenAI Python SDK](https://github.com/openai/openai-python)
* [OpenAI Node.js SDK](https://github.com/openai/openai-node)

<script type="application/ld+json">
  {`
    {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "TechArticle",
        "@id": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk",
        "headline": "How do I use CometAPI with the OpenAI SDK?",
        "description": "Use CometAPI with the OpenAI Python and Node.js SDKs by setting the CometAPI API key and base URL.",
        "url": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk",
        "author": {
          "@type": "Organization",
          "name": "CometAPI"
        },
        "publisher": {
          "@type": "Organization",
          "name": "CometAPI",
          "url": "https://www.cometapi.com"
        }
      },
      {
        "@type": "BreadcrumbList",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "CometAPI Docs",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "Guides",
            "item": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "How do I use CometAPI with the OpenAI SDK?",
            "item": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk"
          }
        ]
      }
    ]
    }
    `}
</script>
