Connect the Vercel AI SDK to an AI API from Iran
The AI SDK is Vercel's TypeScript library for building AI apps in Node.js and Next.js: generateText and streamText, tools and structured output. The @ai-sdk/openai provider takes a baseURL through createOpenAI; point it at 1xAi and call models with .chat() so that every model is reachable with one key.
Setting up Vercel AI SDK
- [01]
Create an account and a 1xAi key. Register at 1xai.ir (the app is in Persian) and confirm your email or mobile number. You spin the sign-up gift wheel (from 100,000 to 1,000,000 Toman of gift credit) once, which is enough to test the connection. Then create a 1xai-* key in the dashboard and copy it at once: a key is shown only once.
- [02]
Install the packages. In a Node or Next.js project, install the ai and @ai-sdk/openai packages with npm i ai @ai-sdk/openai.
- [03]
Put the key in an environment variable. Put ONEXAI_API_KEY=1xai-* in .env.local or the server's environment. The key stays on the server: do not give it the NEXT_PUBLIC_ prefix, and do not call the API directly from a browser.
- [04]
Create the provider with the base URL. Call createOpenAI with baseURL set to https://1xai.ir/v1 and apiKey from process.env.ONEXAI_API_KEY, and create the model with onexai.chat and the model name. .chat() uses Chat Completions, which every model on 1xAi speaks.
- [05]
Run the example. Run index.mts with npx tsx index.mts. usage in the result gives the token counts, and the Toman cost of each call is recorded in the 1xAi dashboard.
Ready to copy
// npm i ai @ai-sdk/openai
// https://ai-sdk.dev/providers/ai-sdk-providers/openai
import { createOpenAI } from "@ai-sdk/openai";
import { generateText, streamText } from "ai";
const onexai = createOpenAI({
baseURL: "https://1xai.ir/v1",
apiKey: process.env.ONEXAI_API_KEY,
});
const { text, usage } = await generateText({
model: onexai.chat("claude-sonnet-5"),
prompt: "Explain prompt caching in two sentences.",
});
console.log(text, usage);
const result = streamText({
model: onexai.chat("gpt-5-mini"),
prompt: "Write a haiku about Tehran.",
});
for await (const delta of result.textStream) process.stdout.write(delta);// app/api/chat/route.ts: the key stays on the server
import { createOpenAI } from "@ai-sdk/openai";
import { streamText } from "ai";
const onexai = createOpenAI({ baseURL: "https://1xai.ir/v1", apiKey: process.env.ONEXAI_API_KEY });
export async function POST(req: Request) {
const { prompt } = await req.json();
const result = streamText({ model: onexai.chat("gpt-5-mini"), prompt });
return result.toTextStreamResponse();
}ONEXAI_API_KEY=1xai-XXXXXXXXXXXXXXXX
Recommended model
Recommended: claude-sonnet-5. A cheaper alternative: gpt-5-mini. For chat and tools the recommended model is good; for short, numerous answers the alternative is cheaper. A model is only a string, so switching means changing the name in onexai.chat. Every model and its Toman price is on the models page.
What it costs
Each call is billed at the model's Toman price: the vendor's list price plus a markup of 15–30% depending on the model, at the day's dollar rate (refreshed every day at 9 in the morning, Tehran time). Each generateText or streamText is one call, and multi-step tools make several; each result's usage gives its token counts. On the /v1 route, OpenAI and Gemini models cache the repeated part of a prompt automatically. Estimate with the calculator (in Persian).
Common errors
- Claude or Gemini fails when called without .chat()
- From AI SDK 5 on, calling the provider without .chat() goes to the Responses API, which on 1xAi takes OpenAI models only. For every model write onexai.chat with the model name.
- apiKey is empty, or the answer is 401
- process.env.ONEXAI_API_KEY is read on the server only. In Next.js keep the code in a Route Handler or a Server Action, put .env.local beside package.json and restart the development server.
- 401: invalid key
- The key is wrong, has expired, or you revoked it. Copy it again from the dashboard or create a new one, with no space before or after it.
- 402: insufficient balance
- The account's Toman credit is used up. A wallet top-up by the Zarinpal gateway or the Digipay gateway or card-to-card transfer with a receipt; calls work again as soon as a payment succeeds.
- Model name or routing error
- 1xAi routes by model name: claude-* to Anthropic, gemini-* to Google, deepseek-* to DeepSeek; grok-*, kimi-*, glm-*, qwen*, MiniMax-* and hy* reach the Grok, Kimi, GLM, Qwen, MiniMax and Hunyuan models; every other name goes to OpenAI. Write a model id exactly as the model list shows it.
Every error code is in the error reference.
Does Claude prompt caching work in the AI SDK?
Not on the /v1 route; Claude's cache is on the native Anthropic route only. For it, create the @ai-sdk/anthropic provider with baseURL https://1xai.ir/anthropic/v1 and the same key, and set cacheControl in the message's providerOptions.
Does the key reach the user's browser?
It should not. Call the AI SDK on the server only, in a Route Handler, a Server Action or a Node back end, and keep the key in the server's environment; the browser talks only to your own route. The 1xAi API is not built for direct calls from a browser.
Other guides: Cursor, Claude Code, Codex CLI, n8n, Dify, Cline, Open WebUI, LibreChat, Continue, LangChain, LlamaIndex, LiteLLM.
1xAi is an independent gateway. It is not affiliated with, endorsed by, or a reseller for OpenAI, Anthropic, Google, DeepSeek, xAI, Moonshot AI, Z.ai, Alibaba Qwen, MiniMax or Tencent Hunyuan. Requests are relayed upstream rather than answered by models of our own, so each provider's own usage policies apply to what their models will and will not do, and their model availability is theirs to change. Current reachability is published on the status page.