Publishing documentation in Postman - Build an Automation Script
import fetch from 'node-fetch'; import assert from 'assert'; // Environment variables for security const POSTMAN_API_KEY = process.env.POSTMAN_API_KEY; const COLLECTION_UID = process.env.COLLECTION_UID; async function publishDocumentation() { // Step 1: Verify collection exists const collectionResponse = await fetch(`https://api.getpostman.com/collections/${COLLECTION_UID}`, { headers: { 'X-Api-Key': POSTMAN_API_KEY } }); assert.strictEqual(collectionResponse.status, 200, 'Collection not found'); // Step 2: Publish documentation via Postman API const publishResponse = await fetch(`https://api.getpostman.com/documentation/collections/${COLLECTION_UID}/publish`, { method: 'POST', headers: { 'X-Api-Key': POSTMAN_API_KEY, 'Content-Type': 'application/json' }, body: JSON.stringify({}) // No body needed for publish }); assert.strictEqual(publishResponse.status, 200, 'Failed to publish documentation'); const publishData = await publishResponse.json(); const docUrl = publishData.documentation?.url; assert.ok(docUrl, 'Documentation URL missing'); // Step 3: Verify documentation URL is accessible const docPageResponse = await fetch(docUrl); assert.strictEqual(docPageResponse.status, 200, 'Documentation URL not accessible'); console.log(`Documentation published successfully: ${docUrl}`); } publishDocumentation().catch(err => { console.error('Test failed:', err.message); process.exit(1); });
This script automates publishing Postman documentation using the Postman API.
First, it checks if the collection exists by calling the collections endpoint with the collection UID.
Then, it sends a POST request to the documentation publish endpoint to publish the docs.
It asserts the response status codes to ensure success at each step.
Next, it extracts the documentation URL from the response and verifies that the URL is accessible by checking for HTTP 200 status.
Environment variables are used to keep the API key and collection UID secure and configurable.
This approach uses explicit assertions and clear error messages to help identify failures.
Now add data-driven testing to publish documentation for 3 different collections using their UIDs
Practice
Solution
Step 1: Understand the purpose of documentation
Documentation explains how an API works and helps others use it.Step 2: Identify the role of publishing in Postman
Publishing shares this documentation so others can access it easily.Final Answer:
To share API details clearly with others -> Option CQuick Check:
Publishing documentation = sharing API info [OK]
- Confusing publishing with testing APIs
- Thinking publishing creates API endpoints
- Assuming publishing is for debugging
Solution
Step 1: Locate where collections are managed
Collections hold API requests and their details in Postman.Step 2: Identify the menu for publishing docs
The Collection menu provides options to publish documentation for that collection.Final Answer:
Collection menu -> Option DQuick Check:
Publish docs from Collection menu [OK]
- Choosing Environment menu which manages variables
- Selecting History menu which shows past requests
- Using Runner menu meant for running tests
Solution
Step 1: Understand the link between collection and docs
Published docs are connected to the collection's current state.Step 2: Identify behavior on collection update
When the collection changes, the published docs update automatically to stay current.Final Answer:
They update automatically to reflect changes -> Option AQuick Check:
Docs auto-update with collection changes [OK]
- Thinking docs stay static after publishing
- Believing docs get deleted on update
- Assuming docs become read-only
Solution
Step 1: Check requirements for publishing docs
Publishing requires a collection with at least one request to document.Step 2: Analyze why option is disabled
If the collection is empty (no requests), the publish option is disabled (greyed out).Final Answer:
Your collection has no requests -> Option BQuick Check:
Empty collection disables publish option [OK]
- Assuming internet or login issues cause greyed option
- Not checking if collection is empty
- Confusing selection with collection content
Solution
Step 1: Understand visibility options in Postman docs
Postman allows setting documentation visibility to Public, Team, or Private.Step 2: Choose correct visibility for sharing with team only
Setting visibility to 'Team' restricts access to team members only, not public.Final Answer:
Set documentation visibility to 'Team' instead of 'Public' -> Option AQuick Check:
Use 'Team' visibility to share docs with team only [OK]
- Publishing docs as Public when private sharing needed
- Relying on environment variables for access control
- Exporting collections instead of publishing docs
Ready to prove it?
Pass the test and this pattern moves unknown
