Why use an OpenAI-compatible API in n8n?
n8n has built-in OpenAI nodes and AI agent nodes that speak the OpenAI format. By changing the Base URL on the credential, those same nodes can call any model on the gateway. You keep one credential for every workflow, switch models by typing a different name, and see each call's token count and cost in one usage log. This works on n8n Cloud and self-hosted n8n.
Set up the credential
- Create an API key at API Keys and name it "n8n".
- In n8n, open Credentials (or Overview, then Create, then Credential) and choose OpenAI.
- Paste your
sk-key into API Key. - Set Base URL to
https://api.macloudai.com/v1. Leave Organization ID empty. - Save. n8n tests the credential; if the test fails, check the key and the
/v1at the end of the URL. - In any OpenAI or OpenAI Chat Model node, select this credential and enter a model name from the models page.
If the model dropdown in a node is empty or shows only some models, switch the model field to By ID (or use an expression) and type the exact name, for example hk-5-5. The Base URL must end in /v1.
Example workflow: summarize and classify incoming messages
A simple, useful pattern: a form or webhook receives a customer message, the AI writes a one-line summary and a category, and the result goes to a sheet or chat channel.
- Webhook node (POST), which receives JSON like
{"message": "..."}. - Basic LLM Chain node with an OpenAI Chat Model sub-node using your credential and model
deepseek-v4.1-flashor another cheap model. - Prompt: "Summarize this customer message in one line and give one category: billing, bug, feature, other. Reply as JSON with keys summary and category."
- Turn on the output parser option, or add a Code or Edit Fields node to parse the JSON.
- Send the result to Google Sheets, Slack, email or wherever your team works.
For tool-using workflows, replace the Basic LLM Chain with an AI Agent node and attach the same OpenAI Chat Model. Choose a model that supports tool calling well; Claude and GPT models are common choices for agents.
Alternative: the HTTP Request node
If you want full control over the request, or your n8n version does not show a Base URL field, call the API directly with an HTTP Request node. Set Method to POST, URL to https://api.macloudai.com/v1/chat/completions, add the header Authorization with value Bearer sk-... (better: store it in a Header Auth credential), and send this JSON body:
{
"model": "tt-6-luna",
"max_tokens": 300,
"messages": [
{"role": "system", "content": "Reply as JSON with keys summary and category."},
{"role": "user", "content": "{{ $json.body.message }}"}
]
}
The answer is in choices[0].message.content of the response, and the token counts are in usage. You can reference them in later nodes with an expression such as {{ $json.choices[0].message.content }}.
When you insert user text into a JSON body with an expression, quotes or line breaks in the text can break the JSON. Use the HTTP Request node's "Using Fields Below" option for the body, or build the body in a Code node with JSON.stringify, to avoid this.
Test the webhook
Activate the workflow (or click Listen for test event) and send a test message:
curl -X POST https://YOUR-N8N-HOST/webhook/YOUR-PATH \
-H "Content-Type: application/json" \
-d '{"message": "I was charged twice this month, please check my invoice."}'
Then open Usage in the console to see the call, its input and output tokens, and the cost.
Choosing a model for automations
- High-volume classification, tagging and extraction: start with a cheap model such as
deepseek-v4.1-flashor a small Gemini or GPT model. - Agents that call tools and make decisions:
hk-5-5ortt-6-luna. - Long documents, PDFs turned into text, transcripts: consider
gem-3.8-flash.
Workflows run without anyone watching, so set max_tokens (Maximum Number of Tokens in the node options) to keep each call bounded, and add an error branch that retries 429 and 5xx errors after a short wait. More on picking models: which model for which job. Prices per 1M tokens:
Real prices now
Estimated from the prices on the models page: 2,000 input tokens and 500 output tokens per chat. The console shows the actual charge.
| Models | Per chat | 1,000 chats |
|---|---|---|
| GEM 3.8 Flash | Rp 4 | Rp 4.434 |
| TT-6 Luna | Rp 11 | Rp 10.642 |
| TT-6.1 Terra | Rp 7 | Rp 7.263 |
| HK-5.5 | Rp 3 | Rp 3.487 |
| GEM 4 Argon | Rp 85 | Rp 84.649 |
| TT-6.1 Sol | Rp 15 | Rp 14.526 |
Common errors in n8n
| Error | Cause | Fix |
|---|---|---|
| 401 | Wrong or deleted key | Paste a fresh key into the credential |
| 403 | Balance too low | Top up, then rerun the execution |
| 404 | Base URL missing /v1, or model name typo | Use https://api.macloudai.com/v1; type the model name by ID |
| 429 | Too many executions at once | Add Wait or Retry On Fail; lower the batch size |
| 5xx | Temporary upstream error | Enable Retry On Fail in node settings |
FAQ
Q1What Base URL do I use in the n8n OpenAI credential?
Q2Can I use Claude in n8n with the OpenAI node?
Q3The model list in n8n is empty. What should I do?
Q4Does this work on self-hosted n8n?
Q5How do I control costs in automated workflows?
More guides
- One API key for Claude, GPT, Gemini and DeepSeek
- How to use Claude Code with a custom API endpoint
- Claude Code API errors and fixes: 401, 403, 404, 429, 5xx
- How to use Cursor with your own API key and base URL
- Claude vs GPT vs Gemini vs DeepSeek: which model for which job
How AI API token pricing works and how to estimate your bill