Function tools (TypeScript)
For models supporting function calling, send tool definitions with Chat Completions and inspect the assistant's tool_calls. Validate arguments before executing local code and return tool results in a subsequent request. Model support varies.
This guide uses the official OpenAI client with TheRouter; it is not a TheRouter-owned SDK. The therouter and @therouter/sdk packages previously named here are not currently published in the public package registries.
Install and run
bash
npm install openaiSet THEROUTER_API_KEY on your server, then run:
typescript
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.THEROUTER_API_KEY, baseURL: "https://api.therouter.ai/v1" });
const result = await client.chat.completions.create({
model: "openai/gpt-4o-mini",
messages: [{ role: "user", content: "What is the weather in Paris? Call get_weather." }],
tools: [{ type: "function", function: { name: "get_weather", description: "Get weather for a city", parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] } } }],
tool_choice: "required", max_tokens: 80,
});
console.log(result.choices[0]?.message.tool_calls);