/chat/completions endpoint works in n8n through the HTTP Request node (or the OpenAI node with a custom Base URL): set the base URL, a Bearer API key stored in a Header Auth credential, and the model name. Protect your own intake webhooks with Header Auth, Basic Auth or JWT, and validate every payload.Two questions come up together when you add AI to an intake workflow: how do I call a free-tier model, and how do I stop strangers posting junk to my webhook? This guide answers both with the n8n features that exist for the purpose.
Part 1: calling an OpenAI-compatible API
"OpenAI-compatible" means the provider accepts the same request shape as OpenAI's chat completions API. The differences are the base URL, the key and the model names.
| Provider | Base URL | Notes (checked 4 Dec 2026) |
|---|---|---|
| Google Gemini API | https://generativelanguage.googleapis.com/v1beta/openai | Model e.g. gemini-3.5-flash-lite. Free tier without a card; commonly reported free limits for Flash-Lite are 15 requests/min and 500/day, but Google says limits are not guaranteed. On the free tier Google may use prompts to improve its products. |
| Groq | https://api.groq.com/openai/v1 | Free plan with per-model request and token limits; check your console. |
| OpenRouter | https://openrouter.ai/api/v1 | Models with a :free suffix have low daily request caps unless you add credit. |
| Ollama (local) | http://localhost:11434/v1 | Runs on your machine; if n8n is in Docker use host.docker.internal instead of localhost. Any text works as the key. |
| OpenAI or other paid | https://api.openai.com/v1 | Paid usage. |
Set up with the HTTP Request node
- Create a credential: Header Auth, name
Authorization, valueBearer YOUR_API_KEY. (Note the space after Bearer.) - Add an HTTP Request node: method POST, URL
{{ $json.llm_base_url }}/chat/completionsor a fixed URL, Authentication Generic Credential Type > Header Auth. - Send body as JSON:
{
"model": "gemini-3.5-flash-lite",
"temperature": 0.3,
"messages": [
{ "role": "system", "content": "You write a two-sentence summary. Use only the facts given." },
{ "role": "user", "content": "Client goals: ..." }
]
}
The reply text is at choices[0].message.content. Read it in the next node with the expression {{ $json.choices[0].message.content }}.
Or use the OpenAI node
n8n's OpenAI credential has a Base URL field. Set it to a provider's base URL from the table, paste the key, and the OpenAI and chat-model nodes will talk to that provider. Compatibility varies: some providers do not implement every option (such as strict JSON response formats or tool calling), so test the exact features you use.
Make it fail safely
- Retries. In the HTTP Request node's Settings, enable Retry On Fail (for example 3 tries, 5 seconds apart) and Batching (one item per 1.5 seconds) to stay inside per-minute limits.
- Continue on error. Set On Error to continue with the regular output, then branch: if the response has no
choices, use a template text instead and record the error. A 429 from a free tier should not stop your workflow. - Validate the output. If you asked for JSON,
JSON.parsein a try/catch and check allowed values. If the text must contain an invoice number or amount, check that it still does. - Treat input as data. Say so in the system prompt for anything that includes text from outsiders (forms, emails). It reduces, but does not remove, prompt-injection risk, so keep outputs as drafts.
- Privacy. Read the provider's data terms. Free tiers often permit use of your prompts for improvement; send only what you would be comfortable with that in mind.
Part 2: securing an intake webhook
A production webhook URL is a public address. Anyone who learns it can post to it. n8n's Webhook node offers several protections:
| Option | How it works | When to use |
|---|---|---|
| Header Auth | Caller must send a header such as X-Api-Key with the value you set in a credential | Server-to-server calls (your form tool, CRM, Zapier or Make) |
| Basic Auth | Username and password via HTTP basic auth | Simple tools that support basic auth. Use HTTPS |
| JWT Auth | Caller sends a signed JSON Web Token that n8n verifies | When the caller can sign tokens |
| IP allowlist (Webhook node option) | Only listed IP addresses are accepted | When the caller has fixed IPs |
Header Auth, step by step
- In the Webhook node set Authentication to Header Auth and create a credential with name
X-Api-Keyand a long random value. - Configure your calling tool to send the same header. Test:
curl -X POST YOUR_PRODUCTION_URL -H "X-Api-Key: YOUR_VALUE" -H "Content-Type: application/json" -d '{"proposal_id":"P-1"}'. Without the header n8n should answer with an authorisation error. - Rotate the value if it leaks, by editing the credential and your caller.
For forms you share with clients, the Form Trigger supports Basic Auth; but a public lead form usually needs to be open, so protect it with validation and rate awareness instead of a password.
Validate what arrives
- Check required fields exist and have the right type before doing anything else; return or log an error for the rest.
- Limit text length before it goes into a prompt or a file.
- Never build file paths, shell commands or SQL from raw input. Slugify IDs first (lowercase letters, digits and hyphens only).
- Respond quickly: set Respond to Immediately if the caller does not need the result, so slow AI calls do not cause timeouts upstream.
- Keep test and production separate: the Test URL works only during test listening and the Production URL only while the workflow is published.
Quick checklist
- Authentication enabled on the webhook.
- Key stored in an n8n credential, not in the workflow body.
- Payload validated and truncated.
- AI call with retries and a template fallback.
- Outputs saved as files or drafts, never sent automatically.
The kits' intake and approval workflows use these patterns: a Config node holding the model name and base URL, a switch that skips AI when it is off, fallback text when the call fails, and no auto-send. See the Proposal & Client Onboarding Kit.
Testing a provider before wiring it in
Before touching n8n, check the key and model with a single command. Replace the URL and model with the ones from the provider table:
curl https://generativelanguage.googleapis.com/v1beta/openai/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" -H "Content-Type: application/json" \
-d '{"model":"gemini-3.5-flash-lite","messages":[{"role":"user","content":"Say OK"}]}'
A JSON reply with choices means the key, URL and model name are right. A 401 or 403 is a key problem, a 404 is usually a wrong base URL or model name, and a 429 is a rate limit. Fixing it here is faster than debugging inside a workflow.
Common mistakes
- Putting the full
/chat/completionspath into the OpenAI credential's Base URL. The credential wants the base; the node adds the path. - Forgetting the word
Bearerand the space before the key in a Header Auth value. - Using a model name from a blog post that your provider has retired. Model names change; copy the current one from the provider's own page.
- Sending a whole inbox thread to a free tier and hitting the per-minute token limit. Truncate input.
- Putting a real key inside an exported workflow JSON that you share. Credentials stay in n8n's store, not in nodes. Check exports before sharing.
Choosing a model for this kind of task
Drafting a short paragraph, summarising a form entry or classifying an email is well within the reach of small, fast models; you do not need the largest model for it. Pick a small model first, test it on your own sample, and move up only if it fails on your examples. Cheaper models are also less likely to hit free-tier limits. Whatever you choose, keep the AI's role narrow (wording, not facts) and keep a template fallback.
Sources
- n8n docs: Webhook node, HTTP Request node, OpenAI credentials.
- Provider docs: Gemini OpenAI compatibility, Groq OpenAI compatibility, OpenRouter quickstart, Ollama OpenAI compatibility.