"APM Server transport error (ECONNREFUSED): connect ECONNREFUSED ::1:8200" when trying to configure APM agent in application code

I am trying to configure an APM agent in my nodejs application code. I have installed the APM agent using the following command:

npm install elastic-apm-node --save

I then updated my application code using the following template:

// Add this to the VERY top of the first file loaded in your app
var apm = require('elastic-apm-node').start({

  // Override the service name from package.json
  // Allowed characters: a-z, A-Z, 0-9, -, _, and space
  serviceName: 'my-service-name',

  // Use if APM Server requires a secret token
  secretToken: '',

  // Set the custom APM Server URL (default: http://localhost:8200)
  serverUrl: 'http://localhost:8200',

  // Set the service environment
  environment: 'my-environment'

But when I start up my application, the following is logged to the console:

{"log.level":"error","@timestamp":"2023-02-22T23:47:33.876Z","log":{"logger":"elastic-apm-node"},"ecs":{"version":"1.6.0"},"message":"APM Server transport error: error fetching APM Server version: connect ECONNREFUSED ::1:8200"}
{"log.level":"error","@timestamp":"2023-02-22T23:47:33.877Z","log":{"logger":"elastic-apm-node"},"ecs":{"version":"1.6.0"},"message":"APM Server transport error (ECONNREFUSED): connect ECONNREFUSED ::1:8200"}
{"log.level":"error","@timestamp":"2023-02-22T23:47:33.977Z","log":{"logger":"elastic-apm-node"},"ecs":{"version":"1.6.0"},"message":"APM Server transport error (ECONNREFUSED): connect ECONNREFUSED ::1:8200"}
{"log.level":"error","@timestamp":"2023-02-22T23:48:03.885Z","log":{"logger":"elastic-apm-node"},"ecs":{"version":"1.6.0"},"message":"APM Server transport error (ECONNREFUSED): connect ECONNREFUSED ::1:8200"}

I'm not sure why I'm receiving these error messages or what to do about them.

Hi @Matt_Johnston,

That error message is the APM agent saying that it cannot find an APM server running at "http://localhost:8200" to which to send tracing data. That "http://localhost:8200" default only works if there actually is a locally running APM server.

Do you have an Elastic Cloud deployment? If so, if you visit the APM section (click "Services" in the sidebar), then you should get a screen like this:

That "Add data" button will take you to a tutorial page that walks through setup steps. Part of that setup will be a "Configure the agent" section that will show the serverUrl and secretToken for your deployment. For example:

Hi. Thank you for your response.

As to your question, no I don't have an elastic cloud deployment. I'm running everything on my local machine.

In Kibana, I checked my APM server status and it said "You have correctly setup APM Server", as you can see from the following screenshot:

Also in Kibana and similar to the "Configure the agent" screenshot you provide, I have the following:

So, what should I do in this case if I'm not running an Elastic Cloud deployment and getting the error I'm getting?

Hi @Matt_Johnston

What Version are you running? I did not see it?

And did you install the APM server via the Agent or Legacy Stand Alone APM Server?

Have you tried to curl the Server Status API from the

curl -v -X POST http://localhost:8200/ -H "Authorization: Bearer secret_token"

Also, is the node agent running on the same host as the APM Server?

If not you will need to use the IP of the APM server not localhost in that case from the server that you have your node app on

curl -v -X POST http://<ipofapmserverhost>:8200/ -H "Authorization: Bearer secret_token"

I think I'm running version 8.6.2, but I'm not sure how to verify that.

As to your second question, I think I installed it via the Agent, but I'm not sure I fully know the difference between those two options.

The node agent is running on the same host as the APM Server. They are both running on my local machine.

I tried curl -v -X POST http://localhost:8200/ -H "Authorization: Bearer secret_token" and this is what was printed to my console:

*   Trying
* Connected to localhost ( port 8200 (#0)
> POST / HTTP/1.1
> Host: localhost:8200
> User-Agent: curl/7.79.1
> Accept: */*
> Authorization: Bearer secret_token
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json
< X-Content-Type-Options: nosniff
< Date: Thu, 23 Feb 2023 13:26:21 GMT
< Content-Length: 149
  "build_date": "2023-02-13T13:01:54Z",
  "build_sha": "8638b035d700e5e85e376252402b5375e4d4190b",
  "publish_ready": true,
  "version": "8.6.2"

But, I'm not sure how to interpret the meaning of the output.

It looks like curl reached the apm server and it is connected which is good

When you show secret_token you need to replace with your secret token I guess that API did not need that but when you connect from your node app you will need that BUT it looks like yours does not require one according to your image above... which is interesting not sure I have seen that before.

When you set up your server did you get a secret_token if so you need to use it in the node app.

Also can you try using in your node app instead of localhost


No, I did not receive a secret_token when I set things up.

So, I tried instead of localhost and that seems to have fixed it! Thank you!

Cool and Interesting that localhost was not resolved correctly ... put that in your "List" :slight_smile:

You do not need a secret_token then, IF / When you want to move out of POC mode to production mode you should secure the APM with HTTPS and a secret_token

Okay, got it. Thanks again!

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