Creating collections in Postman - Automation Script Walkthrough
import requests import json # Set your Postman API key here API_KEY = 'YOUR_POSTMAN_API_KEY' # Postman API endpoint for collections url = 'https://api.getpostman.com/collections' # Headers with API key headers = { 'X-Api-Key': API_KEY, 'Content-Type': 'application/json' } # Collection data to create collection_data = { "collection": { "info": { "name": "My API Tests", "description": "Collection for API testing exercises", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [] } } # Send POST request to create collection response = requests.post(url, headers=headers, data=json.dumps(collection_data)) # Assert response status code is 200 (success) assert response.status_code == 200, f"Expected status 200 but got {response.status_code}" # Parse response JSON response_json = response.json() # Assert collection name and description in response created_collection = response_json.get('collection', {}) assert created_collection.get('info', {}).get('name') == 'My API Tests', "Collection name mismatch" assert created_collection.get('info', {}).get('description') == 'Collection for API testing exercises', "Collection description mismatch" print('Collection created successfully with correct name and description.')
This script uses the Postman API to create a new collection programmatically.
First, it sets the API key for authentication in the headers.
The collection data includes the name and description as required.
It sends a POST request to the Postman collections endpoint.
After receiving the response, it checks the status code to ensure the request succeeded.
Then it verifies the collection name and description in the response match what was sent.
This confirms the collection was created correctly.
Using the API avoids manual UI steps and allows automation.
Now add data-driven testing to create three different collections with different names and descriptions
Practice
Solution
Step 1: Understand what collections do
Collections in Postman are used to group API requests together.Step 2: Identify the main benefit
This grouping helps organize requests and makes testing easier.Final Answer:
To group API requests for better organization -> Option AQuick Check:
Collections = Group API requests [OK]
- Thinking collections are for coding
- Confusing collections with server settings
- Assuming collections create user accounts
Solution
Step 1: Locate how to create collections
In Postman, you create collections by clicking the 'New' button.Step 2: Select the correct option
After clicking 'New', you choose 'Collection' to create one.Final Answer:
Click the 'New' button and select 'Collection' -> Option CQuick Check:
Create collection = New button > Collection [OK]
- Trying to delete requests to create collections
- Changing environment instead of creating collections
- Typing commands in console to create collections
Solution
Step 1: Understand collection structure
A collection acts like a folder holding multiple requests.Step 2: Visualize sidebar display
The sidebar shows the collection name with its requests nested inside.Final Answer:
A folder named 'User APIs' containing the three requests -> Option AQuick Check:
Collection = Folder with requests inside [OK]
- Thinking each request becomes its own collection
- Expecting requests to appear without collections
- Assuming errors occur when adding multiple requests
Solution
Step 1: Check the process of adding requests
After adding a request to a collection, you must save it to confirm.Step 2: Identify common user error
Forgetting to save means the request won't appear in the collection.Final Answer:
You forgot to save the request after adding it to the collection -> Option BQuick Check:
Save request after adding = request appears [OK]
- Assuming creating new collection adds request automatically
- Closing app before saving changes
- Mixing environment changes with request additions
Solution
Step 1: Create and organize requests in a collection
First, create a collection and add all relevant API requests to it.Step 2: Save the collection and use sharing features
Save the collection and then use Postman's 'Share' option to generate a link or invite team members.Final Answer:
Create a collection, add requests, save it, then use the 'Share' option to send a link -> Option DQuick Check:
Share collections by saving and using Share link [OK]
- Sharing requests individually instead of collections
- Only exporting environment variables
- Changing workspace without creating collections
Ready to prove it?
Pass the test and this pattern moves unknown
