Skip to content

Instantly share code, notes, and snippets.

@a-essawy
Created November 27, 2024 09:57
Show Gist options
  • Save a-essawy/b1efb171fc205557de8b1456347ab6b1 to your computer and use it in GitHub Desktop.
Save a-essawy/b1efb171fc205557de8b1456347ab6b1 to your computer and use it in GitHub Desktop.
Download Postman Collection using Node.js
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