# Adding authentication for REST API

**URL:** https://discourse.nodered.org/t/adding-authentication-for-rest-api/65293
**Category:** Industrial
**Created:** [19 July 2022 03:08 UTC](https://discourse.nodered.org/t/adding-authentication-for-rest-api/65293 "2022-07-19T03:08:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@dinesh](https://discourse.nodered.org/u/dinesh)
#### Post date: [19 July 2022 03:08 UTC](https://discourse.nodered.org/t/adding-authentication-for-rest-api/65293/1 "2022-07-19T03:08:39Z")

</div>

Hi

I am trying to set up REST api in my node red. I want to set authentication but Im not sure how to do this. In the 'http request' node, Im able to put in authentication to GET/POST from external server.

However, if a client wants to access my API(http in node), there is no provision to set basic authentication. I'm sure there has to be a way to restrict access to my API. How can I set the username and password.?

Currently, my node red is installed in linux debian based IOT device. Do I need to edit any configuration files in NODE RED?

thank you  
 ![rest1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/4/b4a86908b4b1416efd276fed6d8358f6d6955f31.png)  
 ![rest2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db88875c831ab07e7f14a8e991793a39cfb03e68.png)

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [19 July 2022 04:25 UTC](https://discourse.nodered.org/t/adding-authentication-for-rest-api/65293/2 "2022-07-19T04:25:38Z")

</div>

You can capture the incoming headers in the http-in node (`msg.req.headers.authorization`) and handle the authentication yourself from there.

---

<div class="post-metadata">

### Author: ![dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@dinesh](https://discourse.nodered.org/u/dinesh)
#### Post date: [19 July 2022 06:18 UTC](https://discourse.nodered.org/t/adding-authentication-for-rest-api/65293/3 "2022-07-19T06:18:36Z")

</div>

Hi,  
This is a good idea also. I'm able to catch the authentication.Thanks.

But I'm actually not that familiar with REST API. I know that if incorrect username/password it has to give something like "401-Unauthorized". I could return this message in the payload but I'm not that sure what is the full format for such response. But I can work with this.

However, is there other ways where this can all be automatically taken care off?

Thank you

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [19 July 2022 06:25 UTC](https://discourse.nodered.org/t/adding-authentication-for-rest-api/65293/4 "2022-07-19T06:25:58Z")

</div>

The http-response node supports `msg.statusCode`, so you would return a 401. (and you can route that to a custom html response as well).

---

<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: [19 July 2022 06:26 UTC](https://discourse.nodered.org/t/adding-authentication-for-rest-api/65293/5 "2022-07-19T06:26:25Z")

</div>

> [@dinesh](#):
>
> However, is there other ways where this can all be automatically taken care off?

Yes, by far the easiest approach is to use an external reverse proxy such as NGINX or Caddy and do the authentication there.

---

<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: [17 September 2022 06:26 UTC](https://discourse.nodered.org/t/adding-authentication-for-rest-api/65293/6 "2022-09-17T06:26:25Z")

</div>

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