Postman Environments — Managing Configuration Across Dev, Staging, and Production
In this tutorial, you will learn about Postman Environments. We cover key concepts, practical examples, and best practices to help you master this topic.
Postman environments organize variables into named sets for different deployment stages, allowing the same collection to run against dev, staging, and production without modifying requests.
Code Example: Multi-Environment Configuration
// Dev environment template
{
"name": "Development",
"values": [
{ "key": "baseUrl", "value": "https://dev-api.durga-antivirus.com" },
{ "key": "authUrl", "value": "https://dev-auth.durga-antivirus.com" },
{ "key": "testUsername", "value": "dev-tester" },
{ "key": "testPassword", "value": "dev-pass-123" },
{ "key": "apiKey", "value": "dk_dev_test_key_abc123" },
{ "key": "timeout", "value": "10000" },
{ "key": "logLevel", "value": "debug" },
{ "key": "tokenExpiry", "value": "" }
]
}
// Pre-request script — environment validation
const requiredVars = ["baseUrl", "authUrl", "testUsername", "testPassword"];
const missing = requiredVars.filter(v => !pm.environment.get(v));
if (missing.length > 0) {
throw new Error(
`Missing required environment variables: ${missing.join(", ")}`
);
}
// Set environment-specific timeout
const timeout = parseInt(pm.environment.get("timeout")) || 5000;
pm.request.timeout = timeout;
console.log(`Running against: ${pm.environment.get("baseUrl")}`);
Code Example: Dynamic Variable Resolution
// Pre-request script — resolve variables at runtime
function resolveVariables(template) {
return template.replace(/\{\{(\w+)\}\}/g, (match, key) => {
return pm.environment.get(key) ||
pm.collectionVariables.get(key) ||
pm.variables.get(key) ||
match;
});
}
const urlTemplate = "{{baseUrl}}/api/v1/threats";
pm.request.url = resolveVariables(urlTemplate);
// Secret handling — never log secrets
const sensitiveKeys = ["testPassword", "apiKey", "jwtSecret"];
sensitiveKeys.forEach(key => {
if (pm.environment.get(key)) {
pm.environment.set(key + "_present", "true");
}
});
Common Mistakes
1. Committing Secrets to Version Control
Postman environment files contain API keys and passwords. Never commit them. Use environment-specific variables from CI/CD secret stores.
2. Environment Name Mismatches
If the environment name in Postman differs from the CI/CD environment, the wrong variables load. Standardize environment names.
3. Not Validating Variables Before Run
Missing variables cause cryptic errors. Validate all required variables in pre-request scripts before making API calls.
Practice, FAQ, Mini Project follow standard format.
What's Next
Now learn about Postman Workflows with setNextRequest for controlling collection execution flow.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro