# Trouble connecting to API

**URL:** <https://support.routexl.com/t/trouble-connecting-to-api/1801>\
**Category:** API\
**Created:** [January 21, 2021, 6:51pm UTC](https://support.routexl.com/t/trouble-connecting-to-api/1801 "2021-01-21T18:51:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![polar](https://support.routexl.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@polar](https://support.routexl.com/u/polar)\
**Post date:** [January 21, 2021, 6:51pm UTC](https://support.routexl.com/t/trouble-connecting-to-api/1801/1 "2021-01-21T18:51:11Z")

</div>

I am trying to connect to the API for the first time with NodeJS, but I keep getting the error: “connect ECONNREFUSED”. In the options “headers” parameter, for the username:password I have my actual username and password. I also tried it with `headers: ("Authorization", "Basic " + btoa("username:password"));` but that threw an error "btoa is not defined”.

Thoughts on what I’m doing wrong?

Here’s my javascript code:

```
const https = require('https');

const options = {
              baseURL: 'https://api.routexl.com/status', 
              method: 'GET',
              headers: ("Authorization", "Basic username:password")
            }
console.log('options: ' + JSON.stringify(options));
const myResult = httpGet(options); //, res => { 

```

The output of my console.log is:

{“method”: “POST”, “baseURL”: “[https://api.routexl.com/status](https://api.routexl.com/status)”, “headers”: “Basic username:password”}

---

<div class="post-metadata">

**Author:** ![RouteXL](https://support.routexl.com/user_avatar/support.routexl.com/routexl/32/347_2.png) [@RouteXL](https://support.routexl.com/u/RouteXL)\
**Post date:** [January 21, 2021, 9:29pm UTC](https://support.routexl.com/t/trouble-connecting-to-api/1801/2 "2021-01-21T21:29:54Z")

</div>

We’re no NodeJS experts, but the “btoa is not defined” [seems](https://www.google.com/search?q=nodejs+btoa) to be an NodeJS specific issue that we would try to solve first. Here is an topic on StackOverflow with some suggestions:

> <https://stackoverflow.com/questions/23097928/node-js-throws-btoa-is-not-defined-error>

---

<div class="post-metadata">

**Author:** ![polar](https://support.routexl.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@polar](https://support.routexl.com/u/polar)\
**Post date:** [January 22, 2021, 2:19pm UTC](https://support.routexl.com/t/trouble-connecting-to-api/1801/3 "2021-01-22T14:19:19Z")

</div>

Yes, I looked at that post yesterday prior to submitting my post, and as suggested I also tried using the command `var ulog = Buffer.from('username:password').toString('base64');` but with my actual username:password and was unsucessful. Here’s the code:

```
var ulog = Buffer.from('username:password').toString('base64');

const options = {
              //url: 'https://api.routexl.com/', //Enter your base api url from RouteXL
              url: 'https://api.routexl.com/status/Hello+world',
              method: 'GET',
              headers: ("Authorization", "Basic " + ulog)
            }
const myResult = await httpGet('https://api.routexl.com/status/Hello+world', options); //, res => {

```

Here’s what I’m sending:  
options: {“url”:“[https://api.routexl.com/status/Hello+world",“method”:“GET”,“headers”:"Basic](https://api.routexl.com/status/Hello+world%22,%22method%22:%22GET%22,%22headers%22:%22Basic) username:password”}

I’m not getting a JSON back. Am I missing something else?

---

<div class="post-metadata">

**Author:** ![RouteXL](https://support.routexl.com/user_avatar/support.routexl.com/routexl/32/347_2.png) [@RouteXL](https://support.routexl.com/u/RouteXL)\
**Post date:** [January 22, 2021, 3:33pm UTC](https://support.routexl.com/t/trouble-connecting-to-api/1801/4 "2021-01-22T15:33:14Z")

</div>

There is no error on our log for your account, so it seems to return a valid result. When was the last request you’ve sent? What are you getting back exactly? Can you share the result?

---

<div class="post-metadata">

**Author:** ![polar](https://support.routexl.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@polar](https://support.routexl.com/u/polar)\
**Post date:** [January 22, 2021, 3:40pm UTC](https://support.routexl.com/t/trouble-connecting-to-api/1801/5 "2021-01-22T15:40:45Z")

</div>

I’ve tried it multiple times within the last two hours and again just moments ago.

```
 console.log('----------------------------------');	
 console.log(" sent: " + JSON.stringify(options));
 console.log("received: " + JSON.stringify(myResult));
 console.log('----------------------------------');

```

Here’s what I get back on output (but with encoded username:password):

`----------------------------------`  
`sent: {"url":"https://api.routexl.com/status/Hello+world","method":"GET","headers":"Basic myEncodedUsername:Password"}`  
`received: ""`  
`----------------------------------`

---

<div class="post-metadata">

**Author:** ![RouteXL](https://support.routexl.com/user_avatar/support.routexl.com/routexl/32/347_2.png) [@RouteXL](https://support.routexl.com/u/RouteXL)\
**Post date:** [January 22, 2021, 4:12pm UTC](https://support.routexl.com/t/trouble-connecting-to-api/1801/6 "2021-01-22T16:12:19Z")

</div>

We’ve digged a little deeper in the lower level server logs and found 401 errors. Your `httpGet` function should give that HTTP code too. That means the connection worked but authentication failed. So you’ll need to double check that part.
