This is the example that i just created in my n8n, just copy the json and paste it in yours node.
Code node 1 Build requests:
// ---- put your keys here ----
const OPENAI_KEY = ‘PUT_OPENAI_KEY_HERE’;
const GEMINI_KEY = ‘PUT_GEMINI_KEY_HERE’;
const PERPLEXITY_KEY = ‘PUT_PERPLEXITY_KEY_HERE’;
// ----------------------------
const prompt = $json.prompt || ‘Explain quantum entanglement in one short paragraph.’;
return [
{
json: {
provider: ‘openai’,
url: ‘https://api.openai.com/v1/chat/completions’,
headers: { Authorization: 'Bearer ’ + OPENAI_KEY },
body: {
model: ‘gpt-4o’,
messages: [{ role: ‘user’, content: prompt }]
}
}
},
{
json: {
provider: ‘gemini’,
url: ‘https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent’,
headers: { ‘x-goog-api-key’: GEMINI_KEY },
body: {
contents: [{ parts: [{ text: prompt }] }]
}
}
},
{
json: {
provider: ‘perplexity’,
url: ‘https://api.perplexity.ai/chat/completions’,
headers: { Authorization: 'Bearer ’ + PERPLEXITY_KEY },
body: {
model: ‘sonar’,
messages: [{ role: ‘user’, content: prompt }]
}
}
}
];
HTTP node Call all models:
- URL:
{{ $json.url }}
- Send Headers on, Specify Headers → Using JSON:
{{ JSON.stringify($json.headers) }}
- Send Body on, Specify Body → Using JSON:
{{ JSON.stringify($json.body) }}
- Options → Batching → Items per Batch
3
That’s the bit that does the work. All three go out together rather than one after another.
Code node 3 Flatten responses:
const requests = $(‘Build requests’).all();
return $input.all().map((item, i) => {
const provider = requests[i].json.provider;
const r = item.json;
const text = provider === ‘gemini’
? r?.candidates?.[0]?.content?.parts?.[0]?.text
: r?.choices?.[0]?.message?.content;
return { json: { provider, text: text ?? null } };
});
Does it help?