Can I make http request inside of code node?

Hey,

I check the documents but couldnt find it. Im asking just to make sure,

Can I make get/post request as an API call inside of code node?

image

No, I don’t believe it’s possible to do. You will want to use the http request node instead.

But in my case, it is much more easy to make the API calls in code node.

I wonder if it is possible.

1 Like

It should be possible, there are some more topics on people trying to do this, so you will have to do a bit of searching on the forum. Maybe also search on function node as this is the old name for this node.
It should be basic javascript though, you might need some external/internal packages to be allowed in your n8n setup though. Not sure.

Not sure why you would really want to do this though as using the HTTP request node is a lot simpler.

1 Like

Yes, I tried using axios and fetch and neither worked. Assuming you will need to manually add those.

I think @onurbolaca 's issue maybe worrying about authentication, and how that maybe easier in the code function. If that is the case, once you understand how easy n8n makes authentication, it becomes easier to just use the http request node.

For example, Oauth2 is a piece of cake with n8n authentication, because it handles the token fetching portion for you, no need to store or get keys, and you have the benefit of the credentials themselves stored securely, not in plain text in the code node.

Additionally, the Import CURL command in the http request node is almost as easy as copy and pasting code for the api call.

2 Likes

You’ll need to make the/a module available. n8n currently uses axios internally so probably adding just that to the env var will allow you to use it in the code node NODE_FUNCTION_ALLOW_EXTERNAL=axios

2 Likes

Additionally,. are also all the helpers available that can be used in custom nodes.

Meaning methods like:

  • this.helpers.httpRequest
  • this.helpers.httpRequestWithAuthentication

Those methods use underneath the hood, axios and no environment variable has to be set.

7 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.