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.
5 Answers
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.
Moreover, if you want to stick with the Pre-request Script, then here is the way to do so:
Now, access the environment variable in the request as:
- 41
- 3
-
1Hi the pre-requisite code doesn't work. pm.environment.set('a',_.$randomEmail) , now try accessing a – PDHide Jan 08 '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 09 '20 at 09:36
-
And above script worked for me. – Ghulam Abbas Jan 09 '20 at 09: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 09 '20 at 10:54
You can use below code in your pre-requist script
const uuid = require('uuid')
let email=uuid()+'@domain.com'
pm.environment.set('email',email)
and access the same using
- {{email}} , in non script locations
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'
pm.environment.set('email',email)
Or
let email=_.random(1000)+'@domain.com'
pm.environment.set('email',email)
- 11,065
- 2
- 14
- 42
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.
- 121
- 1
-
2Interesting. There's a reference to the official documentation: https://learning.postman.com/docs/writing-scripts/script-references/variables-list/#domains-emails-and-usernames – pavelsaman Jul 30 '20 at 20:14
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.
- 10,876
- 2
- 18
- 39
use postman Dynamic variables as "{{$postmanDynamicVariables}}" in pre-request Script
Example
const email = "{{$randomEmail}}";
pm.environment.set("email", email);
// or to generate and set random full name
pm.environment.set("name","{{$randomFullName}}");
- 111
- 1


