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.