Code Node fetch is not defined Error When Calling External API

Hi everyone, I’m running into an issue in the Code node when trying to call an external API. I’m getting an error:
fetch is not defined
My workflow is:
HTTP Request → Code → Next Node
Inside the Code node, I’m trying to make another API call like this:
const res = await fetch(‘https://api.example.com/data’);
const data = await res.json();
return [
{
json: data
}
];
But it throws the error and doesn’t execute.
I thought fetch would work inside the Code node, but maybe I’m missing something about the environment n8n uses.

Describe the problem/error/question

What’s the correct way to make external API calls inside an n8n Code node? Or should I be using a different node for this?

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @Ademola_Daniel

This happens because fetch is not available in n8n Code nodes by default.

n8n doesn’t run in a browser environment, so fetch isn’t defined.

Instead of calling APIs inside the Code node, use the HTTP Request node that’s what it’s designed for.

If you still want to do it in Code use this.helpers.httpRequest:const response = await this.helpers.httpRequest({
method: ‘GET’,
url: ‘https://api.example.com/data’,
json: true
});

return [
{
json: response
}
];

Oh okay thanks @Niffzy I will try this fix right now