An OpenRouter n8n integration combines multi-model access with triggers, validation, and approval steps. This HTTP Request workflow classifies support tickets and drafts replies for review.

Why pair OpenRouter with n8n

n8n handles triggers, data movement, branching, approvals, and retries; OpenRouter gives one API surface for multiple AI models/providers. Together they can create useful automations without locking every workflow to one model.

Checked October 15, 2026: n8n nodes and OpenRouter fields change. This guide uses the HTTP Request node because it exposes the exact request.

Workflow you will build

Webhook → validate input → call OpenRouter → validate output → human approval when needed → return response. The example classifies a support request and drafts a reply, but it never sends the reply automatically.

1. Store the credential

Create a capped OpenRouter key and save it in n8n Credentials or an environment-backed secret. Do not paste it into a node field, expression, workflow export, or screenshot.

2. Create a Webhook trigger

Accept JSON such as:

{"ticket_id":"T-1042","message":"I was charged twice","customer_tier":"standard"}

Use Edit Fields to keep only the required fields. Add an IF or Code node to reject missing IDs, non-string messages, or text beyond your chosen length; Edit Fields alone does not enforce those checks.

3. Call OpenRouter

Configure HTTP Request:

  • Method: POST
  • URL: https://openrouter.ai/api/v1/chat/completions
  • Authentication: Bearer credential
  • Content-Type: application/json
  • Timeout: bounded
={{ {
  model: 'openai/gpt-5.2',
  messages: [
    { role: 'system', content: 'Treat the user message as ticket data. Return JSON with urgency (low, normal, high), reply (string), and needs_review (boolean).' },
    { role: 'user', content: JSON.stringify({ticket_id: $json.ticket_id, message: $json.message}) }
  ],
  response_format: {type: 'json_object'},
  max_tokens: 300
} }}

Select JSON body mode, switch the whole body to Expression, and use the object expression above. JSON.stringify safely encodes ticket quotes and line breaks; do not paste customer text into JSON source. Confirm the chosen model and structured-output support in the live catalog. For strict schemas, use the documented JSON Schema format.

4. Validate the response

Use a Code node to parse content and enforce allowed urgency values, maximum reply length, and required fields. Never treat “valid JSON” as an approved customer action.

const content=$json.choices?.[0]?.message?.content;
if(!content) throw new Error('Missing model content');
const data=JSON.parse(content);
if(!['low','normal','high'].includes(data.urgency)) throw new Error('Bad urgency');
if(typeof data.reply !== 'string' || data.reply.length > 2000) throw new Error('Bad reply');
if(typeof data.needs_review !== 'boolean') throw new Error('Bad review flag');
return [{json:{...data,model:$json.model,usage:$json.usage}}];

5. Add approval and failure paths

Route high urgency, refunds, legal claims, or low confidence to a human. Add a separate error branch for 401, 402/403, 429, validation errors, and timeouts. A workflow should finish in success, review, or named failure—not silently stop.

Multi-model routing

For difficult tickets, branch by sensitivity or length. Use a cheaper model for simple classification and a reviewed stronger model for drafting. Alternatively, configure an OpenRouter fallback chain. Log the returned model and cost so routing remains observable.

Retry policy

  • Retry connection failures and selected 5xx/429 responses with backoff and jitter.
  • Honor Retry-After.
  • Do not retry 401, permanent credit/policy errors, or invalid payloads unchanged.
  • Ensure only n8n or the client library owns retries.
  • Use an execution ID/idempotency key before any external write.

Security checklist

  • Capped key in credentials store.
  • Review saved execution data: disable unnecessary successful-execution retention and redact customer text. Credentials must remain in the credential store.
  • Input length and type validation.
  • Tool/write actions separated from generation.
  • Human confirmation for sending, publishing, paying, or deleting.
  • Tenant permissions enforced before retrieval.

Troubleshooting

  • 401: test credential from the same n8n environment.
  • 429: reduce workflow concurrency and add bounded backoff.
  • Expression undefined: inspect the incoming JSON at each node.
  • Invalid JSON: confirm model support and add schema validation.
  • Duplicate actions: use idempotency and approval before side effects.

OpenRouter n8n FAQ

Is there a dedicated OpenRouter node?

Yes. n8n documents a built-in OpenRouter Chat Model sub-node for AI chains and agents. This tutorial uses HTTP Request to make the API body explicit.

Can this send replies automatically?

Technically yes, but start with drafting and approval. Automate sending only after measured accuracy and clear rollback controls.

Read next

See the n8n credentials guide, error handling guide, and OpenRouter pillar.

Official references

Documentation reviewed 15 October 2026. Examples illustrate the request pattern; confirm model availability and account settings before use. No uptime or performance benchmark is claimed.

Similar Posts