What You'll Learn
- How to organize requests into collections and folders
- How to use environments to switch between dev/staging/prod
- How to share collections with your team
- Best practices for naming and structuring your workspace
Why This Matters
A pile of saved requests is unmanageable past 20 entries. Collections and folders give you structure. Environments let you switch between dev and prod without editing every request. Together, they turn Postman from a one-off tool into a real testing workspace that scales with your project.
Collections — Grouping Related Requests
A collection is a folder that holds related requests. Typical organization:
JSONPlaceholder API (collection)
├── Posts (folder)
│ ├── Get all posts
│ ├── Get post by ID
│ ├── Create post
│ ├── Update post
│ └── Delete post
├── Users (folder)
│ ├── Get all users
│ ├── Get user by ID
│ └── Create user
└── Comments (folder)
└── Get comments for post
Creating a collection
- Click + New in the left sidebar.
- Choose Collection.
- Name it (e.g., "JSONPlaceholder API").
- Add a description (what this collection tests).
Adding requests to a collection
- Open any request tab.
- Click Save.
- Choose the collection (and optionally a folder within it).
- Give the request a clear name.
Folders — Sub-Grouping Within Collections
Folders let you group requests within a collection. Common patterns:
- By resource:
Users,Posts,Comments - By operation:
Read,Create,Update,Delete - By test type:
Positive,Negative,Boundary - By feature:
Auth,Profile,Billing
Pick one pattern and apply it consistently. Mixing patterns makes the collection hard to navigate.
Variables — Reusable Values
Variables let you write a request once and reuse it across environments. Define a variable like {{base_url}}, and Postman substitutes the value from the active environment.
Variable scopes (from most to least specific)
- Local — current request only, cleared when Postman closes.
- Data — from a data file during Collection Runner.
- Environment — from the active environment.
- Collection — from the collection's variables.
- Global — available everywhere.
More specific scopes override less specific ones. Use the least privileged scope that works.
Using variables in requests
URL: {{base_url}}/users/{{user_id}}
Header: Authorization: Bearer {{token}}
Body: {"name": "{{user_name}}"}
Setting variables in scripts
// In Tests or Pre-request Script
pm.collectionVariables.set("user_id", response.body.id);
pm.environment.set("token", response.body.access_token);
Environments — Switching Between Configurations
An environment is a named set of variables. Typical environments:
| Variable | Dev | Staging | Production |
|---|---|---|---|
base_url | http://localhost:3000 | https://staging.example.com | https://api.example.com |
token | dev_token_xxx | staging_token_yyy | prod_token_zzz |
test_user | dev_user | staging_user | real_user |
Creating an environment
- Click the environment dropdown (top right).
- Click Add Environment.
- Name it (e.g., "Dev").
- Add variables (Key, Value, optionally marked as secret).
- Save.
Switching environments
Use the dropdown at the top right. All {{variable}} references in your requests now use values from the selected environment. Switching takes effect immediately — no need to reload requests.
Best Practices for Naming
- Request names describe what they do: "Get User by ID", "Create Order", not "Request 1", "Request 2".
- Folder names describe a group: "Users", "Auth", "Positive Tests".
- Collection names describe an API: "Stripe API", "GitHub API", "Internal User Service".
- Environment names describe a config: "Dev", "Staging", "Prod", "Local".
- Variable names are clear:
base_url,user_token, notbu,t.
Sharing Collections
- Export to file: Right-click collection → Export → save as JSON. Send the file to a teammate.
- Share via workspace: Create a team workspace, add the collection, invite teammates. Everyone sees the same version.
- Postman API: Use the Postman API to programmatically update collections in CI.
Collection-Level Settings
Collections have settings that apply to all requests in them:
- Authorization — set once, applies to every request (can be overridden per request).
- Pre-request scripts — run before every request.
- Tests — run after every request.
- Variables — collection-scoped variables.
Use these for common setup like authentication. Set the auth token at the collection level; every request inherits it.
Common Mistakes
- Hard-coding URLs and tokens. Use variables. Otherwise, switching environments means editing every request.
- Generic request names. "Request 1" tells you nothing. Name requests by what they do.
- Flat collections. 50 requests in one list is unmanageable. Use folders.
- One giant environment with everything. Split into Dev/Staging/Prod. Each has its own values.
- Sharing tokens in exported collections. Use environment variables marked as secret. Never commit tokens inside saved requests.
- Not using collection-level auth. If every request uses Bearer token auth, set it once at the collection level.
Practical Exercise (20 minutes)
- Create a collection called "JSONPlaceholder".
- Create three folders inside it: "Posts", "Users", "Comments".
- Save 5 requests into the Posts folder: GET list, GET one, POST create, PUT update, DELETE.
- Create two environments: "Dev" (base_url = https://jsonplaceholder.typicode.com) and "Open-Meteo" (base_url = https://api.open-meteo.com).
- Update all your saved requests to use
{{base_url}}instead of the hardcoded URL. - Switch between environments and confirm the same requests work for both APIs.
Mini Challenge
Export your collection as a JSON file and send it to a friend (or to a second Postman account you control). Have them import it and run the requests. Did it work? Did the environments come through? (Hint: environments are exported separately from collections.)
Key Takeaways
- Collections group related requests. Folders sub-group them by resource, operation, or test type.
- Variables (
{{base_url}},{{token}}) make requests reusable across environments. - Environments switch between Dev/Staging/Prod without editing every request.
- Variable scopes: local < data < environment < collection < global. Use the most specific scope that works.
- Set collection-level auth and scripts to apply them to every request.
Previously: Lesson 24 introduced Postman basics.
Today: You learned to organize Postman work into collections and environments.
Next: Lesson 37b covers variables, scripts, and the Collection Runner — turning saved requests into automated tests.
FAQ
What is the difference between global and environment variables?
Global variables are available in every environment. Environment variables are specific to the active environment. Use globals for values that never change (like a constant API key shared across environments). Use environment variables for values that differ between Dev/Staging/Prod.
Can I export environments separately from collections?
Yes. Collections and environments are exported separately. To share a complete setup, export both the collection JSON and the environment JSON. The recipient imports each.
Comments
Comments
Post a Comment