Skip to main content
You can use OpenAI Node.js SDK 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 for the model parameter.
npm i openai
yarn add openai
pnpm add openai

Chat Completion

Chat completion API that generates a response from a given conversation. Choose the example that best fits your needs:
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();
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();
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();
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();
Last modified on July 6, 2026