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.