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

**URL:** <https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236>\
**Category:** APM\
**Tags:** nodejs\
**Created:** [February 22, 2023, 11:50pm UTC](https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236 "2023-02-22T23:50:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Johnston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_johnston/32/117427_2.png) [@Matt\_Johnston](https://discuss.elastic.co/u/Matt_Johnston)\
**Post date:** [February 22, 2023, 11:50pm UTC](https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236/1 "2023-02-22T23:50:20Z")

</div>

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

```auto
npm install elastic-apm-node --save

```

I then updated my application code using the following template:

```auto
// 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:

```auto
{"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.

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [February 23, 2023, 12:16am UTC](https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236/2 "2023-02-23T00:16:51Z")

</div>

Hi @Matt_Johnston,

That error message is the APM agent saying that it cannot find an APM server running at "[http://localhost:8200](http://localhost:8200)" to which to send tracing data. That "[http://localhost:8200](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:

 ![Screenshot 2023-02-22 at 4.10.56 PM](https://us1.discourse-cdn.com/elastic/original/3X/0/2/0247167ad84bf60101bb867285d6d2c128a7ceb6.png)

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:

 ![Screenshot 2023-02-22 at 4.11.36 PM](https://us1.discourse-cdn.com/elastic/original/3X/b/4/b421d8850884a61e76d54f0d468bc29afa299ee0.png)

---

<div class="post-metadata">

**Author:** ![Matt\_Johnston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_johnston/32/117427_2.png) [@Matt\_Johnston](https://discuss.elastic.co/u/Matt_Johnston)\
**Post date:** [February 23, 2023, 12:29am UTC](https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236/3 "2023-02-23T00:29:29Z")

</div>

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:

 ![Screen Shot 2023-02-22 at 8.24.36 PM](https://us1.discourse-cdn.com/elastic/original/3X/1/3/133c4645497ef7750d717e395e13ea78b8300eda.png)

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

 ![Screen Shot 2023-02-22 at 8.28.18 PM](https://us1.discourse-cdn.com/elastic/original/3X/e/c/ec677912216b8c1893c436f2353cf5b72ea35b88.png)

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

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 23, 2023, 2:56am UTC](https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236/4 "2023-02-23T02:56:28Z")

</div>

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](https://www.elastic.co/guide/en/apm/guide/current/api-info.html) 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"`

---

<div class="post-metadata">

**Author:** ![Matt\_Johnston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_johnston/32/117427_2.png) [@Matt\_Johnston](https://discuss.elastic.co/u/Matt_Johnston)\
**Post date:** [February 23, 2023, 1:36pm UTC](https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236/5 "2023-02-23T13:36:27Z")

</div>

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:

```auto
* Trying 127.0.0.1:8200...
* Connected to localhost (127.0.0.1) 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.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 23, 2023, 3:31pm UTC](https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236/6 "2023-02-23T15:31:38Z")

</div>

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

> [@Matt\_Johnston](#):
>
> secret\_token

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 `127.0.0.1` in your node app instead of `localhost`

---

<div class="post-metadata">

**Author:** ![Matt\_Johnston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_johnston/32/117427_2.png) [@Matt\_Johnston](https://discuss.elastic.co/u/Matt_Johnston)\
**Post date:** [February 23, 2023, 3:35pm UTC](https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236/7 "2023-02-23T15:35:45Z")

</div>

Hi.

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

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

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 23, 2023, 3:39pm UTC](https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236/8 "2023-02-23T15:39:11Z")

</div>

Cool and Interesting that `localhost` was not resolved correctly ... put that in your "List" 🙂

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`

---

<div class="post-metadata">

**Author:** ![Matt\_Johnston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_johnston/32/117427_2.png) [@Matt\_Johnston](https://discuss.elastic.co/u/Matt_Johnston)\
**Post date:** [February 23, 2023, 3:43pm UTC](https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236/9 "2023-02-23T15:43:48Z")

</div>

Okay, got it. Thanks again!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 16, 2023, 11:44am UTC](https://discuss.elastic.co/t/apm-server-transport-error-econnrefused-connect-econnrefused-8200-when-trying-to-configure-apm-agent-in-application-code/326236/10 "2023-03-16T11:44:24Z")

</div>

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