WaQi
API Integration

Using the Library

Call the API using the @waqi-agent/ai library.

The @waqi-agent/ai package is the official TypeScript/JavaScript client for the Agent Platform. It abstracts the underlying HTTP connection and SSE parsing into an async iterable interface.

Installation

npm install @waqi-agent/ai

Streaming Responses

Use the streamText function to initiate a request. It requires the request payload and your configuration (endpoint and credentials).

import { streamText } from "@waqi-agent/ai";

const response = await streamText(
  {
    model: "<your-model-id>",
    messages: [{ type: "message", role: "user", content: "What is on oslojs.dev?" }],
    tools: {
      web_fetch: { tool_id: "<your-tool-id>" },
    },
  },
  {
    endpoint: "https://api.agent-platform.waqitech.com",
    baseURL: process.env.BASE_URL as string,
    apiKey: process.env.API_KEY as string,
    token: process.env.WAQI_PLATFORM_API_TOKEN as string,
  }
);

// Iterate over the stream chunks
for await (const { chunk } of response) {
  if (chunk.event === "start") {
    console.log(`\n--- Starting: ${chunk.type} ---`);
  } else if ("delta" in chunk) {
    process.stdout.write(chunk.delta);
  }
}

Accessing Final State

The library automatically aggregates the stream chunks into a complete state object in the background.

Once the stream has finished, you can await response.done to retrieve the fully assembled items, the finish reason, and token usage.

const finalState = await response.done;

if (finalState.usage) {
  console.log("Token Usage:", finalState.usage);
}

// Access the complete array of generated messages, reasoning, and tool calls
console.log(finalState.items);

You can also access the state synchronously at any point during iteration via response.state, or by destructuring state from the iterator (for await (const { chunk, state } of response)).

On this page