# How to add variables in pre request script section

**URL:** <https://community.postman.com/t/how-to-add-variables-in-pre-request-script-section/19929>\
**Category:** Help Hub\
**Created:** [February 3, 2021, 11:00am UTC](https://community.postman.com/t/how-to-add-variables-in-pre-request-script-section/19929 "2021-02-03T11:00:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [February 3, 2021, 11:00am UTC](https://community.postman.com/t/how-to-add-variables-in-pre-request-script-section/19929/1 "2021-02-03T11:00:08Z")

</div>

How to add variables in pre request script section ?

I tired to add variables through get steps but does not worked out.

P.S. Tried with environment variables.

“username”: “{{username}}”,  
“password”: “{{password}}”,

username & password already added in env variables.

Let me know if need full code

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [February 3, 2021, 11:10am UTC](https://community.postman.com/t/how-to-add-variables-in-pre-request-script-section/19929/2 "2021-02-03T11:10:04Z")

</div>

Hi @aakashjain8693, why do you need to declare the variables under pre-request section? Are you trying to add the variables from the body of the request or the previous request or something? You should be able to add the variables,

like

```auto
pm.environment.set("username", pm.variables.replaceIn('{{$randomFirstName}}'));

```

```auto

```

---

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [February 3, 2021, 11:25am UTC](https://community.postman.com/t/how-to-add-variables-in-pre-request-script-section/19929/3 "2021-02-03T11:25:40Z")

</div>

Trying to add data which is already I added in env variable.

Like I added username and password in env variable and now I want to access in pre request.

var username=pm.environment.set(“username”);

and now I am using {{username}} in my next steps but its getting failed.

---

<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:** [February 3, 2021, 11:43am UTC](https://community.postman.com/t/how-to-add-variables-in-pre-request-script-section/19929/4 "2021-02-03T11:43:19Z")

</div>

> [@aakashjain8693](#):
>
> I am using {{username}} in my next steps but its getting failed.

It would be easier for people to help you solve this problem, if you added some example images of the UI that you can see in front of you 😃

I’m not sure where or how you have added the `{{username}}` syntax - It can be used in a number of different areas of the app.

If you’re trying to use this syntax in the scripts, it won’t work like that and you will either need to use `pm.environment.get('username')` or `pm.environment.replaceIn('{{username}}')` to access the value.

---

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [February 3, 2021, 12:28pm UTC](https://community.postman.com/t/how-to-add-variables-in-pre-request-script-section/19929/5 "2021-02-03T12:28:11Z")

</div>

Agree and sorry for inconvenience.

Truly there were few confidential data but will try to provide the exact scenario.

So basically I am trying to send request from pre-request to get my token for which I am using the “send request” snippet.

Here is below looks like my request:

```auto
pm.test('post method', function()
{
pm.sendRequest({
    url: 'dsdsd/auth',
    method: 'POST',
    header: {
        "Content-Type":"application/json"
    },
    body:
    {
        mode: 'raw',
        raw: JSON.stringify(
            {
  "clientId": "",
  "clientSecret": "",
  "grantType": "password",
  "username": "fhdkjfhdkjfh",
  "password": "dhskjhdskjhd*",
  "scope": "openid email offline_access"
            })
    }
},
function(err, res){
    let response=res.json();
     pm.expect(res).to.have.property('code', 200);
    pm.environment.set("tokencoach", response.id_token);
}
)
});

```

So here I need to give username and password.

I tried like `var username =pm.environment.get(“username”);` and then in script {{username}} but no luck.

Let me know still needed any info.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [February 3, 2021, 12:33pm UTC](https://community.postman.com/t/how-to-add-variables-in-pre-request-script-section/19929/6 "2021-02-03T12:33:38Z")

</div>

You need to take this response down. It has your client id and secret in it.

---

<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:** [February 3, 2021, 12:38pm UTC](https://community.postman.com/t/how-to-add-variables-in-pre-request-script-section/19929/7 "2021-02-03T12:38:51Z")

</div>

Good point - removed. 🏆

---

<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:** [February 3, 2021, 12:45pm UTC](https://community.postman.com/t/how-to-add-variables-in-pre-request-script-section/19929/8 "2021-02-03T12:45:35Z")

</div>

If you were to lead with this code snippet in the original question, it would have been slightly clearer 😃

As that’s a string in the script you can resolve this using like this:

```auto
`${variable_name}`

```

More information about how that works can be found here:

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

* * *

I’m not sure why your script is in the `pm.test()` as it’s not really testing anything and it would never _fail_, the `sendRequest()` function will work without that.

```auto
pm.sendRequest({
    url: 'dsdsd/auth',
    method: 'POST',
    header: {
        "Content-Type": "application/json"
    },
    body:
    {
        mode: 'raw',
        raw: JSON.stringify(
            {
                "clientId": "",
                "clientSecret": "",
                "grantType": "password",
                "username": `${pm.environment.get('username')}`,
                "password": `${pm.environment.get('password')}`,
                "scope": "openid email offline_access"
            })
    }
},
    function (err, res) {
        let response = res.json();
        pm.expect(res).to.have.property('code', 200);
        pm.environment.set("tokencoach", response.id_token);
    }
)

```
