# Is it possible to add a Bearer Token auth type in the pm.sendRequest function?

**URL:** <https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274>\
**Category:** Help Hub\
**Tags:** authentication\
**Created:** [March 11, 2021, 2:20pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274 "2021-03-11T14:20:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zachary.baker123](https://avatars.discourse-cdn.com/v4/letter/z/977dab/32.png) [@zachary.baker123](https://community.postman.com/u/zachary.baker123)\
**Post date:** [March 11, 2021, 2:20pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/1 "2021-03-11T14:20:04Z")

</div>

In the Pre-request script, is it possible to add a Bearer Style authentication in the pm.sendRequest function? If so, what does the syntax look like?

```
pm.sendRequest({
            url: "URL of some type", 
            method: 'POST',
            auth: {
                 type:'bearer',
                 token: 'Bearer Token'
            }
            header: {
                'Content-Type': 'application/xml'
            },
            body: {
                mode: 'raw',
                raw: `Body Data of some type`
            }
        }, function (err, res) {
            if(err) {
                throw new Error(err);
            } else {
                console.log("auth was successful")
            }
});
```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2021, 2:37pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/2 "2021-03-11T14:37:11Z")

</div>

You add a number of different things into `.sendRequest()`.

I’m not sure of the full context of the actual request your making but there are some different examples in this gist which I always find useful.

> <https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990>

---

<div class="post-metadata">

**Author:** ![zachary.baker123](https://avatars.discourse-cdn.com/v4/letter/z/977dab/32.png) [@zachary.baker123](https://community.postman.com/u/zachary.baker123)\
**Post date:** [March 11, 2021, 2:42pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/3 "2021-03-11T14:42:16Z")

</div>

It’s a pre-request script that requires Bearer Token authorization for the requests in it. I already have the token, I just need to pass it into a request like what I listed as an example.  
I already know how to do a basic auth with similar syntax. My issue is around what the syntax for a bearer style authentication.

```
 // Authentication
pm.sendRequest({
        url: 'https://test.com',
        method: 'POST',
        auth: {
                 type: "basic",
                 basic: [
                      {key: "username", value:"user"},
                      {key: "password", value:"password"}
            ]
        },
        body: {
            mode: 'urlencoded',
            urlencoded: [
                {key: "Content-Type", value: "application/x-www-form-urlencoded", disabled: false},
                {key: "grant_type", value: "client_credentials", disabled: false}
            ]
        }
}, function (err, res) {
        if(err) {            
throw new Error("An error occurred during authentication, please review:\n" + JSON.stringify(err));
        } else {
            console.log(res.json());

            // Confirm the request was successful
            pm.expect(res).to.have.property('code', 200);
            let data = res.json();
            pm.environment.set("token", "Bearer " + data.access_token);
        }
});
```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2021, 2:47pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/4 "2021-03-11T14:47:15Z")

</div>

My mistake, I thought you were trying to _get_ one with that request 🤦‍♂️

It looks like you already added the word `Bearer` when setting the variable so you would just need to add a new `Authorization` header with the value in the example.

```auto
pm.sendRequest({
    url: "URL of some type",
    method: 'POST',
    header: {
        'Authorization': `${pm.environment.get('token')}`,
        'Content-Type': 'application/json'
    },
    body: {
        mode: 'raw',
        raw: `Body Data of some type`
    }
}, function (err, res) {
    if (err) {
        throw new Error(err);
    } else {
        console.log("auth was successful")
    }
});

```

More information on Javascript template literals:

[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template\_literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)

---

<div class="post-metadata">

**Author:** ![zachary.baker123](https://avatars.discourse-cdn.com/v4/letter/z/977dab/32.png) [@zachary.baker123](https://community.postman.com/u/zachary.baker123)\
**Post date:** [March 11, 2021, 4:03pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/5 "2021-03-11T16:03:18Z")

</div>

I attempted this with my request and it’s still failing validation. It requires that I have the Bearer Authorization setup separately.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2021, 4:13pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/6 "2021-03-11T16:13:34Z")

</div>

I would have thought that if you have the pre-request script that’s getting the Bearer Token, wouldn’t you just use that token value in an Authorization Header of your normal requests.

I don’t really understand why you’re using this in another pre-request. I don’t know your context and what you have in front of you so that only think that I can offer is a ‘guess’.

---

<div class="post-metadata">

**Author:** ![zachary.baker123](https://avatars.discourse-cdn.com/v4/letter/z/977dab/32.png) [@zachary.baker123](https://community.postman.com/u/zachary.baker123)\
**Post date:** [March 11, 2021, 4:31pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/7 "2021-03-11T16:31:02Z")

</div>

It’s due to some constraints that are being set from the BE due to code note present in the FE of the project. I believe once those constraints are removed, your solution should work with no issue

---

<div class="post-metadata">

**Author:** ![zachary.baker123](https://avatars.discourse-cdn.com/v4/letter/z/977dab/32.png) [@zachary.baker123](https://community.postman.com/u/zachary.baker123)\
**Post date:** [March 11, 2021, 6:35pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/8 "2021-03-11T18:35:52Z")

</div>

I found out how to do this type of auth in the pre-request script:

```
       pm.sendRequest({
        url: "some URL", 
        method: 'POST',
        auth: {
			type: "bearer",
			bearer: [
				{
					key: "token",
					value: "Bearer " + bearerToken,
					type: "string"
				}
			]
		},
        header: {
            'Content-Type': 'application/xml'
        },
        body: {
            mode: 'raw',
            raw: `type of body`
        }
    }, function (err, res) {
        if(err) {
            throw new Error(err);
        } else {
            console.log("auth was successful")
        }
    });
```

---

<div class="post-metadata">

**Author:** ![zachary.baker123](https://avatars.discourse-cdn.com/v4/letter/z/977dab/32.png) [@zachary.baker123](https://community.postman.com/u/zachary.baker123)\
**Post date:** [March 11, 2021, 6:36pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/9 "2021-03-11T18:36:56Z")

</div>

I appreciate your help through this endeavour @danny-dainton, you gave me some really good references to read through that helped me out. You rock!

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2021, 7:09pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/10 "2021-03-11T19:09:40Z")

</div>

Ha, I actually had it this way (minus the type property) in one of my initial responses but I edited the code after seeing your example 🤦‍♂️

Totally up to you and down to personal preference but I’m a fan of this syntax.

```auto
`Bearer ${bearerToken}`

```

To me, it feels more readable than:

```auto
"Bearer " + bearerToken 

```

---

<div class="post-metadata">

**Author:** ![wojanik](https://avatars.discourse-cdn.com/v4/letter/w/ecd19e/32.png) [@wojanik](https://community.postman.com/u/wojanik)\
**Post date:** [April 1, 2022, 11:30am UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/11 "2022-04-01T11:30:09Z")

</div>

@Zachary: Great post!  
It helped me to solve my problem.  
However, when I first tried this I had an issue with the token.  
Analysis of the ressonse headers revealed that the Bearer token was like this:  
‘Bearer Bearer llkjh876976jjhgjhg874653hgIj…’  
The word ‘Bearer’ was used twice, hence the authentication was KO.  
So I deleted the “Bearer” part of the ‘value:’ assignment

bearer: [  
{  
key: “token”,  
value: bearerToken,  
type: “string”  
}  
]

and it did work immediately for me.  
Thanks

---

<div class="post-metadata">

**Author:** ![phenom00](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@phenom00](https://community.postman.com/u/phenom00)\
**Post date:** [August 7, 2023, 12:37pm UTC](https://community.postman.com/t/is-it-possible-to-add-a-bearer-token-auth-type-in-the-pm-sendrequest-function/21274/12 "2023-08-07T12:37:13Z")

</div>

God blessed you with this response, after 2 hours of solving the problem its finally over thanks to your help. God blessed, alhamdulillah
