> ## 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 Node.js SDK

> Use the OpenAI Node.js SDK with FriendliAI endpoints. Migrate existing Node.js apps by changing the base URL. Covers chat, streaming, and tool calls.

You can use [**OpenAI Node.js SDK**](https://github.com/openai/openai-node) to interact with FriendliAI.
This makes migration of existing applications already using OpenAI particularly easy.

## How to Use

Before you start, ensure the `baseURL` and `apiKey` refer to FriendliAI.
FriendliAI is fully compatible with the OpenAI SDK, so you can follow the examples below.

Choose one of the [available models](https://friendli.ai/models?products=SERVERLESS) for the `model` parameter.

<CodeGroup>
  ```bash npm theme={null}
  npm i openai
  ```

  ```bash yarn theme={null}
  yarn add openai
  ```

  ```bash pnpm theme={null}
  pnpm add openai
  ```
</CodeGroup>

### Chat Completion

Chat completion API that generates a response from a given conversation.

Choose the example that best fits your needs:

<CodeGroup>
  ```ts Default theme={null}
  import OpenAI from "openai";

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

  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: "Hello!" },
      ],
    });

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

  main();
  ```

  ```ts Streaming theme={null}
  import OpenAI from "openai";

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

  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: "Hello!" },
      ],
      stream: true,
    });

    for await (const chunk of completion) {
      console.log(chunk.choices[0].delta.content);
    }
  }

  main();
  ```

  ```ts Functions theme={null}
  import OpenAI from "openai";

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

  async function main() {
    const messages = [
      { role: "user", content: "What's the weather like in Boston today?" },
    ];
    const tools = [
      {
        type: "function",
        function: {
          name: "get_current_weather",
          description: "Get the current weather in a given location",
          parameters: {
            type: "object",
            properties: {
              location: {
                type: "string",
                description: "The city and state, e.g. San Francisco, CA",
              },
              unit: { type: "string", enum: ["celsius", "fahrenheit"] },
            },
            required: ["location"],
          },
        },
      },
    ];

    const completion = await client.chat.completions.create({
      model: "zai-org/GLM-5.2",
      messages: messages,
      tools: tools,
      tool_choice: "auto",
    });

    console.log(completion);
  }

  main();
  ```

  ```ts Logprobs theme={null}
  import OpenAI from "openai";

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

  async function main() {
    const completion = await client.chat.completions.create({
      model: "zai-org/GLM-5.2",
      messages: [{ role: "user", content: "Hello!" }],
      logprobs: true,
      top_logprobs: 2,
    });

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

  main();
  ```
</CodeGroup>
