# API call - statusCode: 401

**URL:** <https://discourse.nodered.org/t/api-call-statuscode-401/87880>\
**Category:** General\
**Created:** [11 May 2024 21:32 UTC](https://discourse.nodered.org/t/api-call-statuscode-401/87880 "2024-05-11T21:32:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)\
**Post date:** [11 May 2024 21:32 UTC](https://discourse.nodered.org/t/api-call-statuscode-401/87880/1 "2024-05-11T21:32:05Z")

</div>

I am trying make a API call from Node-Red and it returns 'statusCode: 401'

The same API call from my browser and it is successful.

Why?

```auto
[
    {
        "id": "38ae1970b0adf818",
        "type": "inject",
        "z": "81a03fc837d2d50e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 140,
        "y": 140,
        "wires": [
            [
                "e943e0c4e5782741"
            ]
        ]
    },
    {
        "id": "e943e0c4e5782741",
        "type": "function",
        "z": "81a03fc837d2d50e",
        "name": "function 42",
        "func": "let gateway = 'lemkelabs-5031395368184750'\n\n\nmsg.url = \"https://eu1.cloud.thethings.network/api/v3/gs/gateways/\" + gateway + \"/connection/stats\"\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 310,
        "y": 140,
        "wires": [
            [
                "278737cf884c5a83"
            ]
        ]
    },
    {
        "id": "278737cf884c5a83",
        "type": "http request",
        "z": "81a03fc837d2d50e",
        "name": "",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 510,
        "y": 140,
        "wires": [
            [
                "73eb1bc0464ce853"
            ]
        ]
    },
    {
        "id": "73eb1bc0464ce853",
        "type": "debug",
        "z": "81a03fc837d2d50e",
        "name": "debug 324",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 140,
        "wires": []
    }
]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 May 2024 22:05 UTC](https://discourse.nodered.org/t/api-call-statuscode-401/87880/2 "2024-05-11T22:05:36Z")

</div>

> [@ScheepersJohan](#):
>
> statusCode: 401

It is telling you that the call is unauthorised. You probably need to pass some authentication header.

---

<div class="post-metadata">

**Author:** ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)\
**Post date:** [12 May 2024 10:53 UTC](https://discourse.nodered.org/t/api-call-statuscode-401/87880/3 "2024-05-12T10:53:54Z")

</div>

> [@TotallyInformation](#):
>
> call is unauthorised

> [@ScheepersJohan](#):
>
> The same API call from my browser and it is successful.

I do not add a username or password when I call it from the browser.

[The API call](https://eu1.cloud.thethings.network/api/v3/gs/gateways/lemkelabs-5031395368184750/connection/stats)

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [12 May 2024 11:26 UTC](https://discourse.nodered.org/t/api-call-statuscode-401/87880/4 "2024-05-12T11:26:46Z")

</div>

Same error here in firefox browser;

```auto
message	"error:pkg/rpcmetadata:unauthenticated (the context is not authenticated)"
details	
0	
@type	"type.googleapis.com/ttn.lorawan.v3.ErrorDetails"
namespace	"pkg/rpcmetadata"
name	"unauthenticated"
message_format	"the context is not authenticated"
correlation_id	"6e33bf34deda43bc8f344f463259b8ca"
code	16

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 May 2024 11:27 UTC](https://discourse.nodered.org/t/api-call-statuscode-401/87880/5 "2024-05-12T11:27:00Z")

</div>

That URL does fail for me in a browser. Possibly you have already logged on the site in some way in the browser.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [12 May 2024 12:02 UTC](https://discourse.nodered.org/t/api-call-statuscode-401/87880/6 "2024-05-12T12:02:54Z")

</div>

> [@ScheepersJohan](#):
>
> I do not add a username or password when I call it from the browser.

Your normal browser already has an authentication token in the cache. Possibly a cookie. As Colin says, you probably logged in manually at some point and the cookie has been retained.

You will need to read the docs for the API to see how to do an API-based login.

---

<div class="post-metadata">

**Author:** ![kitori](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@kitori](https://discourse.nodered.org/u/kitori)\
**Post date:** [13 May 2024 07:29 UTC](https://discourse.nodered.org/t/api-call-statuscode-401/87880/7 "2024-05-13T07:29:19Z")

</div>

You need an api key:

[https://www.thethingsindustries.com/docs/api/concepts/auth/](https://www.thethingsindustries.com/docs/api/concepts/auth/)

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f7579f2d25aade91d82ff973f32c74bc973fe52.png)

```auto
[{"id":"5a85d4e6d42cd8a6","type":"http request","z":"420aeb9f577edec9","name":"ttn","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://eu1.cloud.thethings.network/api/v3/gs/gateways/lemkelabs-5031395368184750/connection/stats","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"Authorization","keyValue":"","valueType":"other","valueValue":"Bearer NNSXS.U4H3ZFFCMSR42BUAZPW2UWGFBV4WCNI5EXDJXDY.SHIF3PP5PBMJNZESN5XLR5TZJTJUIGKVUTM2I22IVBUVCD6VIQIA"}],"x":790,"y":1020,"wires":[["abff19e5d9e4924c"]]},{"id":"14ddae026c87189f","type":"inject","z":"420aeb9f577edec9","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":620,"y":1020,"wires":[["5a85d4e6d42cd8a6"]]},{"id":"abff19e5d9e4924c","type":"debug","z":"420aeb9f577edec9","name":"debug 177","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":990,"y":1020,"wires":[]}]

```

edit: wrong api version 😅

---

<div class="post-metadata">

**Author:** ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)\
**Post date:** [13 May 2024 12:44 UTC](https://discourse.nodered.org/t/api-call-statuscode-401/87880/8 "2024-05-13T12:44:43Z")

</div>

> [@kitori](#):
>
> `Authorization`

Thank you

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 May 2024 12:45 UTC](https://discourse.nodered.org/t/api-call-statuscode-401/87880/9 "2024-05-27T12:45:14Z")

</div>

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