How to Integrate OpenRouter in your N8N Workflows

Written by

Jannick

Published on

BlogUncategorized

Lately I’m building workflows in N8N. During the building of workflows that utilize Large Language Models (LLMs) I noticed there is no native integration with OpenRouter. Let me tell you in this blog how to make use of the wide range of available models from OpenRouter in your N8N workflows.

What is N8N?

N8N is an open-source no / low-code automation tool. You can compare N8N with Make.com (previously Integromat), Zapier and Pabbly Connect. The difference is that N8N is more geared towards developers, is open-source and it’s possible to self-host!

What is OpenRouter?

OpenRouter is a service that gives you access to a wide range of LLM-models without the need of having to create accounts and buy credits at a lot of different LLM providers (like OpenAI or Anthropic). Another benefit of OpenRouter is that it gives you access to models you otherwise would not have access to.

Some models OpenRouter provides are:

  • OpenAI’s GPT 3.5 / GPT 4 Turbo / GPT-4o
  • Anthropic’s Claude 3
  • Perplexity’s Sonar-Small and Sonar-Medium
  • Google’s Gemini 1.5 Pro
  • Mistral and Mixtral
  • and many more!

How to use OpenRouter in N8N?

To start using one of the many models available through OpenRouter.AI you follow the following steps.

1. Add an AI Agent to your flow

First, go into your workflow. Then add the node ‘AI Agent’.

Select the AI Agent node in N8N

2. Choose the OpenAI Chat Model

After you’ve added the ‘AI Agent’ go back to your workflow and click underneath ‘Model’ on the ‘+’ button. Then select ‘OpenAI Chat Model’

Add a model and select OpenAI Chat Model

3. (Optional) Add your OpenRouter API-key

If you haven’t added your OpenRouter key already to N8N, you should do this first. In the OpenAI Chat Model node click on ‘Select Credential’ after that hit ‘- Create New Credential -‘.

Create a new credential

If you haven’t generated an API key, first go to OpenRouter and generate a new API key. After that go back to N8N and paste the API key in the field underneath ‘API Key’

Fill in your OpenRouter API key and save

After saving you’ll see an error message saying N8N could not connect to OpenAI using this API key. You can safely ignore this warning and close the window. The API key has been saved in N8N.

Ignore the couldn't connect with these settings notice.

4. Set a new Base URL

When returned back to the settings of the OpenAI Chat Model node, make sure the ‘OpenAi account’ API key has been selected. After that, click on ‘Add option’ and choose ‘Base URL’.

Make sure the right credential is selected and add the Option 'Base URL'

Now replace the Base URL to https://openrouter.ai/api/v1

Set the Base URL to https://openrouter.ai/api/v1

5. Replace the Model

Last step is to replace the model. Sadly, the dropdown that’s present in the OpenAI Chat Model does not work with OpenRouter, so you have to fill in your own model name.

Go to https://openrouter.ai/docs#models and pick one of the many models. For testing purposes I would suggest to pick a free model like google/gemma-7b-it:free.

After choosing your model, in the node, hover over the Model field and select ‘Expression’. After that paste in the full model name into the field.

Make the model field an Expression and paste in the LLM you've selected.

6. Test it out!

The last step is to test out the OpenRouter integration you just set up. Go back to your workflow and hit the ‘Chat’ button on the bottom.

Hit the 'Chat' button in your N8N workflow overview

Now a Chat window will open and you’ll be able to send the first message to the model through OpenRouter. Here’s an example of my question “Tell me when the Eiffel Tower was build.”.

An example of the LLM in the N8N chat window that answers the question Tell me when the Eiffel Tower was build. It responded 'The Eiffel Tower was completed in 1889.'

As you can see the Google Gemma was able to successfully answer the question, without having to sign up for a Google-account to get access to the model.

Having issues using OpenRouter?

If you’re encountering the error code like [ERROR: Cannot read properties of undefined (reading 'text')], check your OpenRouter account credits. Visit https://openrouter.ai/credits to see your remaining balance.

cleanshot 2024 05 16 at 16.07.39

Start Exploring LLMs using OpenRouter

That’s it! You’ve successfully implemented OpenRouter in your N8N workflow. You’re now able to use a wide range of LLMs without the need of signing up in a lot of different places. Start using Gemini 1.5, Claude 3 (Haiku, Sonnet, and Opus), GPT-4o and many more models from one API connection.