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

# OpenAI SDK

> The OpenAI SDK provides an interface for interacting with OpenAI-compatible endpoints from Python and JavaScript applications. Follow the directions below to route requests through the Connect AI LLM Gateway.

## Prerequisites

Before you can configure the OpenAI SDK to use the Connect AI LLM Gateway, you must register a provider, create a routing rule, create a project, and issue a virtual key. See [LLM Gateway](/en/LLM-Gateway/LLM-Gateway) for details.

<Note>A Connect AI Personal Access Token (PAT) is not accepted by the LLM Gateway. Only a virtual key authenticates requests.</Note>

## Install the SDK

<Tabs>
  <Tab title="Python">
    ```bash theme={null}
    pip install openai
    ```
  </Tab>

  <Tab title="JavaScript">
    ```bash theme={null}
    npm install openai
    ```
  </Tab>
</Tabs>

## Send a Request

Set `base_url` (Python) or `baseURL` (JavaScript) to the Connect AI LLM Gateway endpoint and pass your virtual key as `api_key` or `apiKey`. The gateway selects the provider and model based on your project's routing rule.

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from openai import OpenAI

    client = OpenAI(
        base_url="https://cloud.cdata.com/gateway/v1",
        api_key="<your-virtual-key>",
    )

    response = client.chat.completions.create(
        messages=[{"role": "user", "content": "Hello"}],
    )
    print(response.choices[0].message.content)
    ```
  </Tab>

  <Tab title="JavaScript">
    ```typescript theme={null}
    import OpenAI from "openai";

    const client = new OpenAI({
      baseURL: "https://cloud.cdata.com/gateway/v1",
      apiKey: "<your-virtual-key>",
    });

    const response = await client.chat.completions.create({
      messages: [{ role: "user", content: "Hello" }],
    });

    console.log(response.choices[0].message.content);
    ```
  </Tab>
</Tabs>

## Configure with Environment Variables

As an alternative to passing connection details in code, you can set environment variables. The OpenAI SDK reads these automatically.

<Tabs>
  <Tab title="macOS / Linux">
    ```bash theme={null}
    export OPENAI_BASE_URL="https://cloud.cdata.com/gateway/v1"
    export OPENAI_API_KEY="<your-virtual-key>"
    ```
  </Tab>

  <Tab title="Windows (PowerShell)">
    ```powershell theme={null}
    $env:OPENAI_BASE_URL = "https://cloud.cdata.com/gateway/v1"
    $env:OPENAI_API_KEY = "<your-virtual-key>"
    ```
  </Tab>
</Tabs>

With environment variables set, you can instantiate the client without explicit arguments:

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from openai import OpenAI

    client = OpenAI()  # reads OPENAI_BASE_URL and OPENAI_API_KEY from env

    response = client.chat.completions.create(
        messages=[{"role": "user", "content": "Hello"}],
    )
    print(response.choices[0].message.content)
    ```
  </Tab>

  <Tab title="JavaScript">
    ```typescript theme={null}
    import OpenAI from "openai";

    const client = new OpenAI(); // reads OPENAI_BASE_URL and OPENAI_API_KEY from env

    const response = await client.chat.completions.create({
      messages: [{ role: "user", content: "Hello" }],
    });

    console.log(response.choices[0].message.content);
    ```
  </Tab>
</Tabs>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.