Connecting to locally hosted APIs using the HTTP Request node

The issue

I have an API hosted on my server that can only be accessed locally, and it is purposefully blocked by the firewall. I would like to be able to access and use this API in n8n, which is available publicly.

I can use curl in the server’s terminal, e.g.

curl http://localhost:10001/v1/about

And I get an expected response of

{"versions":["v1","v2"],"build":2,"mode":"normal","version":"0.65","capabilities":{"v2/send":["quotes","mentions"]}}

But if I try to use the HTTP Request node for any of:

I get this in response:

{"status":"rejected","reason":{"message":"connect ECONNREFUSED 127.0.0.1:10001","name":"Error","stack":"Error: connect ECONNREFUSED 127.0.0.1:10001\n    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16)","code":"ECONNREFUSED"}}

The workflow

Information on my n8n setup

  • n8n version: 0.210.1
  • Using the default database of SQLite
  • Running n8n with the execution process own
  • Running n8n via Docker with docker-compose and caddy

Has anyone ever set this up, or have any pointers? I feel like there should be an environment variable I have missed setting up

Welcome to the community @joe-irving!

I assume the problem is that n8n runs in Docker. So when try to reach localhost within n8n it will be the the Docker host instead of your host.

Does that other API also run within Docker? In this case you have to make sure they are in the same network. If the API is running directly on your server, without Docker, you have to add the following to your docker-compose file:

network_mode: host

Here the docs.

1 Like

Hi @jan, thanks for this (and happy to be here!)

That was a good pointer, great to know it is possible! I have now solved it by:

  • Making sure both services are on the same network
  • Using the service name as the host name in the request
  • Using the internal port
    • i.e. where in the docker-compose file it said ports: - "10001:8080" I was using 10001 but should have been using 8080

I have removed the port mapping because I realise it isn’t actually helpful.

Thanks so much again for your speedy response!

1 Like

Great to hear that it was helpful!

Also thanks a lot for sharing how you could solve it to help others. Is very appreciated!

Have fun!

1 Like

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