Use @ai-sdk/openai-compatible to connect Vercel AI SDK to XiuRouter Chat Completions. Keep both the provider and API key on the server, outside browser bundles.
Setup options
- First setup: install the provider, save a server environment variable, and create a XiuRouter instance.
- Existing setup: the product page offers only the first-setup path. Retain the old provider instance when switching so you can roll back.
- Chat-assisted setup: unavailable; the deployment environment manages secrets and production variables.
Before you start
Use a JavaScript or TypeScript project with server-side execution, a project-specific XiuRouter key, and an exact model ID from the current catalog.
Install dependencies
npm install ai @ai-sdk/openai-compatibleUse @ai-sdk/openai-compatible, not @ai-sdk/openai.
Save the server key
Use .env.local in Next.js or an uncommitted .env in a Node project:
XIUROUTER_API_KEY=YOUR_XIUROUTER_API_KEYIgnore the file in Git. Do not prefix the variable with NEXT_PUBLIC_, which exposes it to the browser.
Create the provider
Create a server-only file such as lib/xiurouter.ts:
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
export const xiurouter = createOpenAICompatible({
name: "xiurouter",
apiKey: process.env.XIUROUTER_API_KEY,
baseURL: "https://router-api.xiu.ai/v1",
});Generate text
import { generateText } from "ai";
import { xiurouter } from "@/lib/xiurouter";
const { text, usage } = await generateText({
model: xiurouter("YOUR_MODEL_ID"),
prompt: "Explain why the sky is blue in two sentences.",
});
console.log(text);
console.log(usage);The script should print a complete response and token usage.
Stream output
import { streamText } from "ai";
import { xiurouter } from "@/lib/xiurouter";
const result = streamText({
model: xiurouter("YOUR_MODEL_ID"),
prompt: "Write a four-line poem.",
});
for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}Verify
- Run the
generateTextexample on the server. - Confirm both text and
usageare returned. - Inspect the key, model,
/v1/chat/completions, and successful result in XiuRouter. - Run
streamTextand verify that the stream finishes completely. - Test tools and structured output separately for the selected model.
Troubleshoot
| Symptom | Next step |
|---|---|
apiKey is undefined |
Restart Next.js; for Node use node --env-file=.env or set the startup environment |
| Browser CORS errors or exposed key | Move the provider from client code to a Server Component, Route Handler, or backend |
| Model not found | Copy the exact ID and check the key’s model scope |
| Wrong endpoint | Check the import and ensure Base URL ends with /v1 |
Roll back
Restore the old provider instance, or remove xiurouter and its environment variable. Confirm production traffic uses the old route before revoking the project key.
Sources and review date
Reviewed on 2026-08-31. This translation preserves that date. Basic setup requires a full server response and matching usage record; verify tools and structured output with the target model.