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

# OpenAI Compatibility

> Use official OpenAI Python and Node.js SDKs with Friendli endpoints. Migrate existing OpenAI applications by changing the base URL and API key.

Friendli Model APIs, Dedicated Endpoints, and Container are [OpenAI-compatible](/openapi/introduction). \
Existing applications can migrate with minimal effort, still using the official OpenAI SDKs.

## Specify the Base URL and API Key

Initialize the OpenAI client using Friendli’s base URL and your Personal API key.

* **Model APIs**: `https://api.friendli.ai/serverless/v1`.
* **Dedicated Endpoints**: `https://api.friendli.ai/dedicated/v1`.
* **Container**: your own container's URL (e.g., `http://HOST:PORT/v1`).

Get your Personal API key in [Friendli Suite > Personal Settings > API Keys](https://friendli.ai/suite/~/setting/keys).

<CodeGroup>
  ```python Python theme={null}
  client = OpenAI(
      api_key=os.getenv("API_KEY"),
      base_url="https://api.friendli.ai/serverless/v1",
  )
  ```

  ```javascript Node.js theme={null}
  const client = new OpenAI({
    apiKey: process.env.API_KEY,
    baseURL: "https://api.friendli.ai/serverless/v1",
  });
  ```
</CodeGroup>

## Usage

Choose any model available on Friendli Model APIs, Dedicated Endpoints, or Container.

### Completions API

Generate text completions using a simple prompt-based approach.

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

  client = OpenAI(
      api_key=os.getenv("API_KEY"),
      base_url="https://api.friendli.ai/serverless/v1",
  )

  completion = client.completions.create(
      model="zai-org/GLM-5.2",
      prompt="Tell me a funny joke about programming.",
      max_tokens=100,
      temperature=0.7,
  )
  print(completion.choices[0].text)
  ```

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

  const client = new OpenAI({
    apiKey: process.env.API_KEY,
    baseURL: "https://api.friendli.ai/serverless/v1",
  });

  async function main() {
    const completion = await client.completions.create({
      model: "zai-org/GLM-5.2",
      prompt: "Tell me a funny joke about programming.",
      max_tokens: 100,
      temperature: 0.7,
    });

    console.log(completion.choices[0].text);
  }

  main().catch(console.error);
  ```
</CodeGroup>

### Chat Completions API

Generate chat completions using a conversational message-based approach.

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

  client = OpenAI(
      api_key=os.getenv("API_KEY"),
      base_url="https://api.friendli.ai/serverless/v1",
  )

  completion = client.chat.completions.create(
      model="zai-org/GLM-5.2",
      messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Tell me a funny joke."},
      ],
      stream=False,
  )
  print(completion.choices[0].message.content)
  ```

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

  const client = new OpenAI({
    apiKey: process.env.API_KEY,
    baseURL: "https://api.friendli.ai/serverless/v1",
  });

  async function main() {
    const completion = await client.chat.completions.create({
      model: "zai-org/GLM-5.2",
      messages: [
        { role: "system", content: "You are a helpful assistant." },
        { role: "user", content: "Tell me a funny joke." },
      ],
    });

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

  main().catch(console.error);
  ```
</CodeGroup>

### Streaming Mode

Receive responses in real-time, enabling better user experience for long responses.

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

  client = OpenAI(
      api_key=os.getenv("API_KEY"),
      base_url="https://api.friendli.ai/serverless/v1",
  )

  stream = client.chat.completions.create(
      model="zai-org/GLM-5.2",
      messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Tell me a funny joke."},
      ],
      stream=True,
  )

  for chunk in stream:
      print(chunk.choices[0].delta.content or "", end="", flush=True)
  ```

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

  const client = new OpenAI({
    apiKey: process.env.API_KEY,
    baseURL: "https://api.friendli.ai/serverless/v1",
  });

  async function main() {
    const stream = await client.chat.completions.create({
      model: "zai-org/GLM-5.2",
      messages: [
        { role: "system", content: "You are a helpful assistant." },
        { role: "user", content: "Tell me a funny joke." },
      ],
      stream: true,
    });

    for await (const chunk of stream) {
      process.stdout.write(chunk.choices[0].delta?.content || "");
    }
  }

  main().catch(console.error);
  ```
</CodeGroup>
