import requests
# Set variables
postman_api_key = 'YOUR_POSTMAN_API_KEY'
collection_uid = 'YOUR_COLLECTION_UID'
team_member_email = 'teammember@example.com'
headers = {
'X-Api-Key': postman_api_key,
'Content-Type': 'application/json'
}
# Step 1: Get current collection details
collection_url = f'https://api.getpostman.com/collections/{collection_uid}'
response = requests.get(collection_url, headers=headers)
response.raise_for_status()
collection_data = response.json()
# Step 2: Share collection with team member
share_url = f'https://api.getpostman.com/collections/{collection_uid}/share'
payload = {
"sharedWith": [
{
"email": team_member_email,
"permission": "edit"
}
]
}
share_response = requests.post(share_url, headers=headers, json=payload)
share_response.raise_for_status()
# Step 3: Verify sharing
verify_response = requests.get(collection_url, headers=headers)
verify_response.raise_for_status()
shared_with = verify_response.json().get('collection', {}).get('sharedWith', [])
assert any(user.get('email') == team_member_email and user.get('permission') == 'edit' for user in shared_with), "Collection not shared correctly"
print('Test Passed: Collection shared with team member with edit permission.')This script uses the Postman API to automate sharing a collection.
First, it fetches the collection details to confirm the collection exists.
Then, it sends a POST request to share the collection with the specified team member email and sets permission to 'edit'.
Finally, it verifies by fetching the collection details again and checking if the team member is listed with the correct permission.
Assertions ensure the sharing was successful. Using environment variables for keys and IDs keeps the script flexible and secure.