Connect Dify to an AI API from Iran
Dify is an open-source platform for building AI apps: chatbots, workflows, agents and knowledge bases (RAG) through a visual interface, on Dify Cloud or your own server. Models come to Dify from Model Provider plugins; with the OpenAI-API-compatible plugin you add each 1xAi model by its exact name, the address https://1xai.ir/v1 and a 1xai-* key.
Setting up Dify
- [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 or open Dify. Sign in to Dify Cloud, or run the self-hosted edition with Docker: clone the langgenius/dify repository, copy .env.example to .env in its docker folder and run docker compose up -d. Then open http://localhost/install and create the administrator account.
- [03]
Install the OpenAI-API-compatible plugin. In Dify go to Plugins and install the OpenAI-API-compatible plugin from the Marketplace. If your server cannot reach the Marketplace, download the plugin file (.difypkg) separately and install it with the Local Package File option.
- [04]
Add an LLM. Under Settings → Model Provider, press Add Model beside OpenAI-API-compatible. Set Model Type to LLM and Model Name to the exact model name from the models page; set API Key to the 1xai-* key and API Base URL to https://1xai.ir/v1, with no /chat/completions at the end.
- [05]
Set the chat mode, the context and tools. Keep Completion mode on Chat and API Type on Chat Completions. Fill in Model context size and Upper bound for max tokens for the model, set Function Call Type to Tool Call for agents, and set Vision Support to Support for a model that takes images; then save.
- [06]
An embedding model for the knowledge base. For Knowledge, do the same once more with Model Type set to Text Embedding and the model text-embedding-3-small; then make the chat model and the embedding model the defaults under System Model Settings.
- [07]
A first app. In Studio create a Chatbot, pick the 1xAi model from the model list and send a message in Debug and Preview. The Toman cost of each call is recorded in the 1xAi dashboard.
Ready to copy
; https://marketplace.dify.ai/plugin/langgenius/openai_api_compatible Model Type = LLM Model Name = claude-sonnet-5 API Key = 1xai-XXXXXXXXXXXXXXXX API Base URL = https://1xai.ir/v1 Completion mode = Chat API Type = Chat Completions API (/chat/completions) Model context size = 200000 Upper bound for max tokens = 8192 Function Call Type = Tool Call Vision Support = Support
Model Type = Text Embedding Model Name = text-embedding-3-small API Key = 1xai-XXXXXXXXXXXXXXXX API Base URL = https://1xai.ir/v1 Model context size = 8191 Model max chunks per batch = 32
git clone https://github.com/langgenius/dify.git cd dify/docker cp .env.example .env docker compose up -d
Recommended model
Recommended: claude-sonnet-5. A cheaper alternative: gpt-5-mini. For agents and multi-step workflows the recommended model calls tools reliably; for simple, frequent steps such as classification and summaries the alternative keeps the cost low. For Knowledge, text-embedding-3-small is inexpensive and enough for most documents. 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). In Dify every LLM node is a call of its own, and a workflow or agent may make several calls per run; building a knowledge base also costs once, to embed the documents. Create a separate key with a spend cap for each app, so that each one's usage shows on its own. API keys (in Persian).
Common errors
- Credentials validation failed when saving the model
- API Base URL has to be exactly https://1xai.ir/v1; Dify adds /chat/completions itself. Write Model Name exactly as the models page shows it, and paste the key without extra spaces.
- The model is not in an app's model list
- After Add Model, the model has to be switched on in the OpenAI-API-compatible list. If you chose the wrong Model Type, for example Text Embedding for a chat model, remove the model and add it again.
- 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.
Why the OpenAI-API-compatible plugin and not the OpenAI plugin?
Dify's OpenAI plugin has a fixed list of OpenAI models. OpenAI-API-compatible speaks the same protocol with an address of your choice and any model name, so claude-*, gemini-* and deepseek-* models are added with the same 1xai-* key.
Is the text of the knowledge base's documents stored anywhere?
The documents and their vectors stay in that Dify's own database. To build embeddings and answers, pieces of the text are sent to 1xAi and from there to the vendor of the model. Next to each usage record, the start of the request and of the response (up to 64 KB each) and, when it is longer, its last 16 KB are kept so that you can read them in your own usage report. That text is erased after 30 days. The numbers (model, tokens, cost, status and time) stay for accounting.
Other guides: Cursor, Claude Code, Codex CLI, n8n, Cline, Open WebUI, LibreChat, Continue, LangChain, LlamaIndex, Vercel AI SDK, 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.