Generate Random US Addresses in Postman
- Elisha Antunes
- Jun 6
- 2 min read
Updated: Jun 23
When testing APIs that involve user profiles, policyholders, or event registrations, it’s tempting to hardcode placeholder addresses like "123 Main St". But realistic, randomized input makes your tests stronger—and your results more believable.
As a Business Analyst who builds automated test suites, I wanted a way to dynamically generate valid US addresses in Postman without relying on external data files. Here’s how I did it using built-in Postman variables.
Tools Used
Postman workspace: for pre-request scripting
Built-in variables like {{$randomStreetAddress}} and {{$randomCity}}
Custom functions to generate zip codes and persist data with pm.collectionVariables
Goal: A realistic address object like this:
{
"street": "852 Elm Hill Dr",
"city": "Minneapolis",
"state": "NY",
"zipCode": "10018",
"country": "US"
}We want to generate this dynamically before each request, so that every test uses fresh data while remaining within valid US formats.
Step 1: Generate a Random Street Address
function generateRandomStreetAddress() {
return pm.variables.replaceIn('{{$randomStreetAddress}}');
}This taps into Postman’s Faker.js-based utility, giving us lifelike addresses like “1934 Sunset Drive”.
Step 2: Add a Random US City
function generateRandomCity() {
return pm.variables.replaceIn('{{$randomCity}}');
}Together with the street address, this builds a more complete picture.
Step 3: Create a 5-digit US Zip Code
function generateRandomZipCode() {
return String(Math.floor(10000 + Math.random() * 90000));
}Why not use {{$randomZipCode}}? Because Postman doesn’t provide one—so we create our own, ensuring it always generates valid 5-digit numbers.
Step 4: Wrap It All in an Address Object
const clientAddress = {
street: generateRandomStreetAddress(),
city: generateRandomCity(),
state: "NY", // Optional: make this dynamic too
zipCode: generateRandomZipCode(),
country: "US"
};
pm.collectionVariables.set("clientAddress", JSON.stringify(clientAddress));
This makes the address reusable across any part of your test suite (e.g. client, venue, insured).
Using It in Your Request Body
In your Postman request body, just plug it in like this:
"client": {{clientAddress}},At runtime, Postman will inject the full JSON string from your collection variable.
Why This Matters
Reduces duplication: You avoid copy-pasting the same addresses.
Improves realism: Your payloads feel lifelike, great for demos or testing front-ends.
Boosts flexibility: You can simulate new locations or add constraints later (e.g., limit states).
