For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
DocsAPI ReferenceSDKs
DocsAPI ReferenceSDKs
      • Overview
      • Chat Completions
      • Prompt Templates
      • Files & Batches
      • Embeddings
      • Image Generation
      • Responses (Reasoning)
      • Compare (Multi-model)
      • Models
      • Error Handling
LogoLogo
On this page
  • Chat Completions
  • Streaming
  • Streaming with tool calls
  • Cancelling a stream
Node.js SDK

Chat Completions

||View as Markdown|
Was this page helpful?
Edit this page
Previous

Node.js SDK Overview

Next

Prompt Templates

Built with

Chat Completions

1import { MeshAPI } from "meshapi-node-sdk";
2
3const client = new MeshAPI({
4 baseUrl: "https://api.yourdomain.com",
5 token: "rsk_01JXXXXXXXXXXXXXXXXXXXXXXXXX",
6});
7
8const response = await client.chat.completions.create({
9 model: "openai/gpt-4o-mini",
10 messages: [
11 { role: "system", content: "You are a helpful assistant." },
12 { role: "user", content: "What is the capital of France?" },
13 ],
14 temperature: 0.7,
15 max_tokens: 256,
16});
17
18console.log(response.choices[0]?.message.content);
19// → "The capital of France is Paris."
20
21console.log(`Tokens used: ${response.usage?.total_tokens}`);

Streaming

1const stream = client.chat.completions.create({
2 model: "openai/gpt-4o-mini",
3 messages: [{ role: "user", content: "Write a haiku about TypeScript." }],
4 stream: true,
5});
6
7let fullText = "";
8
9for await (const chunk of stream) {
10 const delta = chunk.choices[0]?.delta.content ?? "";
11 process.stdout.write(delta);
12 fullText += delta;
13}
14
15console.log("\n--- Full response:", fullText);

Streaming with tool calls

1const stream = client.chat.completions.create({
2 model: "openai/gpt-4o",
3 messages: [{ role: "user", content: "What's the weather in Paris?" }],
4 tools: [
5 {
6 type: "function",
7 function: {
8 name: "get_weather",
9 description: "Get current weather for a city",
10 parameters: {
11 type: "object",
12 properties: {
13 city: { type: "string" },
14 },
15 required: ["city"],
16 },
17 },
18 },
19 ],
20 tool_choice: "auto",
21 stream: true,
22});
23
24for await (const chunk of stream) {
25 // handle delta content or tool_calls
26 console.log(JSON.stringify(chunk.choices[0]?.delta));
27}

Cancelling a stream

1const controller = new AbortController();
2
3// Cancel after 5 seconds
4setTimeout(() => controller.abort(), 5_000);
5
6const stream = client.chat.completions.create(
7 { model: "openai/gpt-4o-mini", messages: [...], stream: true },
8 { signal: controller.signal },
9);
10
11try {
12 for await (const chunk of stream) {
13 process.stdout.write(chunk.choices[0]?.delta.content ?? "");
14 }
15} catch (err) {
16 if ((err as Error).name === "AbortError") {
17 console.log("Stream cancelled.");
18 }
19}