I am new with Postman > Pre-request Script, Can you help me create a random email value for my API Request. Thank you in advance.


Contrary to Pre-request Script, I would suggest using another efficient way to generate a random email: like qa{{$timestamp}}@gmail.com. Here, {{$timestamp}} will always give you a unique number that never occurred in the past.

enter image description here

Moreover, if you want to stick with the Pre-request Script, then here is the way to do so:

Set the random email in the environment so that you can use it in the request.

Now, access the environment variable in the request as:

You might face 'Unexpected token error in JSON, that can be fixed by putting environment variable in double-quotes.

  • 1
    Hi the pre-requisite code doesn't work. pm.environment.set('a',_.$randomEmail) , now try accessing a – PDHide Jan 8 '20 at 13:39
  • I add in Pre-request Script as: pm.environment.set("randomAge", parseInt(_.random(20, 80))); and in body request, get randomAge as: { "firstName": "{{$randomFirstName}}", "lastName": "{{$randomLastName}}", "age": {{randomAge}} } – Ghulam Abbas Jan 9 '20 at 9:36
  • And above script worked for me. – Ghulam Abbas Jan 9 '20 at 9:36
  • Hi Ghulam , the script you mentioned in comment is entirely different from the answer. The approach in the new script is just a normal operation. It is not using the dynamic variable of postman. My question was about your code of using dynamic variable in postman scripts. – PDHide Jan 9 '20 at 10:54

You can simply generate a UUID and append it to a domain.

const uuid = Math.random().toString(36).substring(2, 15) +
        Math.random().toString(36).substring(2, 15);
const email = "user_" + uuid + "@myDomain.com";
pm.environment.set("currentEmail", email);

Reference: https://stackoverflow.com/a/13403498/2252076

Then, you can reference the {{currentEmail}} variable anywhere on the request or on the tests.


You can use below code in your pre-requist script

 const uuid = require('uuid')
 let email=uuid()+'@domain.com'

and access the same using

  1. {{email}} , in non script locations
  2. pm.environment.get('email') in test scripts

You can also use other unique identifier libraries given below, instead of uuid

 const moment = require('moment')
 let email=moment().valueOf()+'@domain.com'


 let email=_.random(1000)+'@domain.com'

I am currently on Postman v7.29.1. It appears that $randomEmail (one of many $random vars) is a built-in variable. So you are free to skip the pre-request script.

// req body - raw/json

  "firstName": "{{$randomFirstName}}",
  "lastName": "{{$randomLastName}}",
  "email": "{{$randomEmail}}",
  "phone": "{{$randomPhoneNumber}}",

Note: The double-quotations " around the curly braces {{ }} are important, as are the prefixed dollar signs $ in the variable names.


use postman Dynamic variables as "{{$postmanDynamicVariables}}" in pre-request Script


const email = "{{$randomEmail}}";

pm.environment.set("email", email);

// or to generate and set random full name


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.