> ## 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 LangChain with CometAPI

> Use this guide to configure LangChain with CometAPI by setting the base URL, API key, and model or provider options.

[LangChain](https://www.langchain.com/) is a popular framework for building LLM-powered applications. CometAPI is fully compatible with `langchain-openai` — just point it at our base URL.

## Prerequisites

* Python 3.8+ and `langchain-openai` installed (`pip install langchain-openai`)
* A [CometAPI](https://www.cometapi.com) account with an active API key

## Setup

<Steps>
  <Step title="Get your CometAPI API key">
    Log in to the [CometAPI dashboard](https://www.cometapi.com/console). Click **Add API Key** and copy your key and base URL (`https://api.cometapi.com`).

    <Frame>
      <img src="https://mintcdn.com/cometapi/SZhlxZhCnMLn__BW/images/overview/810968_364191.png?fit=max&auto=format&n=SZhlxZhCnMLn__BW&q=85&s=aef81a83f29f8eb16655ed4060425f50" alt="CometAPI dashboard — Add API Key" width="3824" height="1892" data-path="images/overview/810968_364191.png" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/cometapi/HhtmQffktazbxUvS/images/overview/810968_364193.png?fit=max&auto=format&n=HhtmQffktazbxUvS&q=85&s=d893f659267150d0faf45f99eb5dffc1" alt="CometAPI API key and base URL" width="2434" height="1232" data-path="images/overview/810968_364193.png" />
    </Frame>
  </Step>

  <Step title="Configure the base URL and API key">
    Set your CometAPI key and base URL when initializing `ChatOpenAI`.

    **Option A — Environment variables (recommended)**:

    ```bash theme={null}
    export OPENAI_API_KEY=$COMETAPI_KEY
    export OPENAI_API_BASE=https://api.cometapi.com/v1
    ```

    **Option B — Inline configuration**:

    <Frame>
      <img src="https://mintcdn.com/cometapi/E4FFvyMYmaOZoldp/images/integrations/810968_360327.png?fit=max&auto=format&n=E4FFvyMYmaOZoldp&q=85&s=62fd70793e6356ac39eedc0f4694f3f2" alt="LangChain code showing ChatOpenAI initialized with CometAPI base URL and key" width="989" height="493" data-path="images/integrations/810968_360327.png" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/cometapi/mA1ovj6s0e3RM42l/images/integrations/810968_351552.png?fit=max&auto=format&n=mA1ovj6s0e3RM42l&q=85&s=b74bba832f1639b8135475f50fe307c6" alt="Code example — ChatOpenAI initialization with CometAPI parameters" width="591" height="189" data-path="images/integrations/810968_351552.png" />
    </Frame>
  </Step>

  <Step title="Test the connection">
    Run a simple chain (e.g., a prompt asking for the current date). A successful response confirms CometAPI is connected.
  </Step>
</Steps>

<Tip>
  Use `base_url="https://api.cometapi.com/v1"` and `api_key=os.environ["COMETAPI_KEY"]` when initializing `ChatOpenAI` or `OpenAI` from `langchain-openai`. Any model ID from the [CometAPI model list](/overview/models) will work.
</Tip>
