Chapter 4: Environment Variables and Data-Driven Tests
This is the question that confuses every beginner. "Should I put baseUrl in collection variables or environment variables?" The answer depends on one thing: does the value CHANGE between environments?
Key Point: If the value changes between dev/staging/prod, it belongs in an ENVIRONMENT variable. If it stays the same regardless of environment, it belongs in a COLLECTION variable.
| Variable | Changes Per Environment? | Use |
|---|---|---|
| baseUrl | Yes — different server per env | Environment |
| authToken | Yes — different credentials per env | Environment |
| apiVersion | No — always "v2" | Collection |
| contentType | No — always "application/json" | Collection |
| timeout | Sometimes — different per env | Environment |
| testEmail | No — same test data everywhere | Collection |
| adminEmail | Yes — different admin per env | Environment |
Here is how a well-organized collection looks. Collection variables hold the constants. Environment variables hold what changes.
// Collection Variables (set once, same everywhere)
// apiVersion = "v2"
// contentType = "application/json"
// maxRetries = "3"
// defaultPageSize = "25"
// Environment Variables (different per env)
// baseUrl = "https://www.testerrank.com/api" (Dev)
// = "https://staging.myapp.com/api" (Staging)
// authToken = "" (set by login script)
// dbHost = "localhost" (Dev) / "staging-db.myapp.com" (Staging)
// In your request URL:
// {{baseUrl}}/{{apiVersion}}/users?limit={{defaultPageSize}}
// Resolves to: https://www.testerrank.com/api/v2/users?limit=25When you export a collection, collection variables go with it. Environment variables do NOT. This means teammates who import your collection get the defaults automatically but need to create their own environment. This is actually a good thing — you do not want your dev credentials leaking into someone else's Postman.
Q: When should you use collection variables vs environment variables in Postman?
A: Use collection variables for values that stay constant regardless of the environment — like API version, content type, or default page size. These travel with the collection when exported. Use environment variables for values that change between dev, staging, and production — like base URL, auth tokens, and database names. The key question is: "Does this value need to change when I switch environments?" If yes, environment variable. If no, collection variable.
Key Point: Value changes per environment? Environment variable. Value stays constant? Collection variable. This one rule will keep your setup clean.