Created
November 27, 2024 09:57
-
-
Save a-essawy/b1efb171fc205557de8b1456347ab6b1 to your computer and use it in GitHub Desktop.
Download Postman Collection using Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const https = require('https'); | |
// Configuration | |
const postmanApiKey = | |
'YOUR-API-KEY'; | |
const postmanCollectionId = 'COLLECTION-ID'; | |
const outputFilePath = path.join(__dirname, 'postman-spec.json'); | |
const options = { | |
hostname: 'api.getpostman.com', | |
path: `/collections/${postmanCollectionId}`, | |
method: 'GET', | |
headers: { | |
'X-Api-Key': postmanApiKey, | |
'Cache-Control': 'no-cache', | |
}, | |
}; | |
const request = https.request(options, (response) => { | |
if (response.statusCode === 200) { | |
const fileStream = fs.createWriteStream(outputFilePath); | |
response.pipe(fileStream); | |
fileStream.on('finish', () => { | |
fileStream.close(); | |
console.log(`Collection downloaded and saved to ${outputFilePath}`); | |
}); | |
} else { | |
console.error( | |
`Failed to fetch collection. Status code: ${response.statusCode}`, | |
); | |
} | |
}); | |
request.on('error', (error) => { | |
console.error(`Error: ${error.message}`); | |
}); | |
request.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment