Vercel AI SDK
Use the AI SDK's OpenAI-compatible provider with TheRouter's API endpoint. Keep the API key on your server.
Install
bash
npm install ai @ai-sdk/openai-compatibleMake a text request
typescript
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { generateText } from "ai";
const therouter = createOpenAICompatible({
name: "therouter",
baseURL: "https://api.therouter.ai/v1",
apiKey: process.env.THEROUTER_API_KEY,
});
const result = await generateText({
model: therouter.chatModel("openai/gpt-4o-mini"),
prompt: "Write a short changelog entry.",
maxOutputTokens: 64,
});
console.log(result.text);Select a model from the model catalog. Streaming and tool support depend on the model and should be tested with your SDK version.
This configuration was checked against the AI SDK's request format with a local mock endpoint; the underlying OpenAI-compatible Chat Completions API was tested against TheRouter. See the AI SDK provider documentation.