Postman Collections — Complete Guide
In this tutorial, you will learn about Postman Collections. We cover key concepts, practical examples, and best practices to help you master this topic.
Postman collections group related API requests for organized testing. Collections support nested folders, collection-level variables, shared authorization, and inherited scripts. They form the foundation for automated API testing.
What You'll Learn
- Creating and structuring collections
- Adding folders for logical grouping
- Collection-level variables and authorization
- Inherited pre-request and test scripts
- Exporting and sharing collections
Why It Matters
Well-organized collections are reusable test suites. Teams share collections for consistent API testing. Collections can be run in CI/CD with Newman for automated Regression Testing.
Real-World Use
Twilio provides a comprehensive Postman collection with folders for each API resource. Salesforce organizes their API by object types in folders. GitHub's collection has folders for repos, issues, and users.
flowchart TD
Collection[API Collection] --> Auth[Collection Auth]
Collection --> Variables[Collection Variables]
Collection --> Scripts[Collection Scripts]
Collection --> Folder1[Folder: Users]
Collection --> Folder2[Folder: Products]
Collection --> Folder3[Folder: Orders]
Folder1 --> Req1[GET Users]
Folder1 --> Req2[POST User]
Folder1 --> Req3[DELETE User]
Teacher Mindset
Structure collections like a filesystem. Use folders for resources (Users, Products, Orders). Set authorization at the collection level so all requests inherit it. Use collection variables for shared base URL and API key.
Code Examples
// Example 1: Collection-level pre-request script
// Set timestamp for all requests
const timestamp = new Date().toISOString();
pm.variables.set('timestamp', timestamp);
// Generate unique IDs
pm.variables.set('request_id', require('uuid').v4());
// Example 2: Collection-level test script
// Log response time for all requests
pm.test('Response time is acceptable', () => {
pm.expect(pm.response.responseTime).to.be.below(2000);
});
// Check for error structure
const body = pm.response.json();
if (body.errors) {
console.warn('Response contains errors:', body.errors);
}
// Example 3: Folder-level tests
// In the "Users" folder test script
pm.test('User response has required fields', () => {
const user = pm.response.json();
pm.expect(user).to.have.all.keys('id', 'name', 'email', 'createdAt');
});
Common Mistakes
- Storing sensitive data (API keys) directly in collections instead of using environments
- Not using folders for resource organization
- Duplicating authorization setup across requests instead of using collection-level auth
- Forgetting to inherit collection scripts at the request level
- Exporting collections without removing sensitive variables
Practice
- Create a collection with three folders: Users, Products, Orders.
- Add collection-level variables for base URL and API key.
- Set collection-level authorization to Bearer Token.
- Add a pre-request script that sets a timestamp variable.
- Challenge: Create a folder-level test that runs for all requests in the Users folder.
FAQ
Mini Project
Create a Postman collection for an e-commerce API. Structure it with folders for Auth, Products, Cart, and Orders. Set collection-level auth and base URL. Add pre-request scripts for timestamps and folder-level tests for response validation.
What's Next
Next, you will learn about Postman scripts for pre-request and test automation.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro