0
0
Postmantesting~15 mins

Collection sharing in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify sharing a Postman collection with a team member
Preconditions (3)
Step 1: Open Postman and navigate to the 'User API Tests' collection
Step 2: Click on the 'Share' button for the collection
Step 3: Enter 'teammember@example.com' in the invite field
Step 4: Set permission to 'Can Edit'
Step 5: Click 'Send Invite'
Step 6: Verify that the team member receives the invitation
Step 7: Verify that the team member can access and edit the collection
✅ Expected Result: The team member receives the invite and can access and edit the 'User API Tests' collection
Automation Requirements - Postman API with Newman
Assertions Needed:
Verify the collection is shared with the specified email
Verify the permission level is 'Can Edit'
Verify the team member can access the collection via API
Best Practices:
Use Postman API endpoints to automate collection sharing
Use environment variables for emails and collection IDs
Validate API responses with clear assertions
Handle authentication securely with API keys or tokens
Automated Solution
Postman
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.

Common Mistakes - 3 Pitfalls
Hardcoding API keys directly in the script
Not checking API response status before proceeding
Assuming the team member has accepted the invite without verification
Bonus Challenge

Now add data-driven testing to share the collection with 3 different team member emails and permissions

Show Hint