Skip to content

Instantly share code, notes, and snippets.

@WilliamStam
Created October 7, 2022 07:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WilliamStam/51d62005ce096100acfa073586bdb777 to your computer and use it in GitHub Desktop.
Save WilliamStam/51d62005ce096100acfa073586bdb777 to your computer and use it in GitHub Desktop.
Update postman collection from source (openapi3 to postman)
  1. start by running npm install
  2. then to update the collection in postman... run npm run generate

the npm run generate script is in the packages.json file

postman should now have updated... on my side the changes are almost instant from pushing to the cloud api for postman updating.

/*
postman makes finding the collection id and uid rather.... "interesting".
step 1: import your collections from files (collection not api) -
on the left menu in postman go to "collections", import, select your file
if you import the file under "api" you will end up with 2 that look the same for step 2.
step 2: setup a GET request in postman to https://api.getpostman.com/collections
add a "header" and call it "X-Api-Key" and the value is the api key setup in secret.js. click send
step 3: get your id and uid from the result. pro tip if you end up having multiple and not knowing which is which,
go to your collections in postman and rename them. then hit the endpoint again
{
"collections": [
{
"id": "1234-1234-1234-1234",
"name": "API",
"owner": "1234567",
"createdAt": "2022-10-06T17:25:57.000Z",
"updatedAt": "2022-10-06T17:37:23.000Z",
"uid": "1234abc-asdfg12345-aaagn5-1234565",
"isPublic": false
},
*/
// i'll probably do some auto discover stuff in future. but for now...
var secret = require('./secret.js');
// change file names accordingly
// the "to" here is where you going to store the temp file before pushing it to postman's api.
// in future i'll try set it up to not need the file written to disk but this was quick n dirty
module.exports = {
"collections": [
{
"from": "location/of/openapi/api.yaml",
"to": "./temp/api.json",
"name": "API user",
"collection_id": "1234-1234-1234-1234",
"collection_uid": "1234abc-asdfg12345-aaagn5-1234565"
},
// add in more blocks as needed
],
"key": secret.key
};
// mostly copy pasted from https://blog.postman.com/sync-your-specs/ but changed for openapi.yaml files vs json files
const fs = require("fs");
const Converter = require("openapi-to-postmanv2");
const request = require('request');
const config = require('./config.js');
// ###############################################
// Get collection ids from here https://api.getpostman.com/collections
// ###############################################
// define functions to handle conversion and update collection
function handleConversion(originalFileName, newFileName) {
var openapiData = fs.readFileSync(originalFileName, {encoding: "UTF8"});
Converter.convert({
type: "string",
data: openapiData
}, {}, (err, conversionResult) => {
if (!conversionResult.result) {
console.log("Could not convert", conversionResult.reason);
} else {
fs.writeFileSync(newFileName, JSON.stringify(conversionResult.output[0].data, null, 2), function (err) {
if (err) {
return console.error(err);
}
console.log('Converted ' + originalFileName + ' to ' + newFileName);
});
}
});
}
function updatePostman(newFileName, collection_uid) {
// read the updated local file and update the Postman collection using Postman API
var data = fs.readFileSync('./' + newFileName, 'utf8');
// Postman users can get a Postman API key here: https://app.getpostman.com/dashboard/integrations
var postmanAPIKey = config.key;
// compile PUT request to update the Postman collection
var putOptions = {
method: 'PUT',
url: 'https://api.getpostman.com/collections/' + collection_uid,
qs: {
format: '2.1.0'
},
headers: {
'Postman-Token': 'fake-for-now',
'Cache-Control': 'no-cache',
'X-Api-Key': postmanAPIKey,
'Content-Type': 'application/json'
},
body: JSON.parse(data),
json: true
};
// submit PUT request to update the Postman collection
request(putOptions, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
}
function updateLocalCollection(newFileName, newFile) {
// update the local Postman collection file
fs.writeFileSync('./' + newFileName, JSON.stringify(newFile, null, 2));
console.log('writing to ' + newFileName);
}
function updateCollection(newFileName, collection) {
// update the Postman collection locally and in the cloud
var data = fs.readFileSync('./' + newFileName, 'utf8');
var file = JSON.parse(data);
file.info.id = collection.collection_id; // update id according to config file
file.info._postman_id = file.info.id; // add new property that is identical to id
var newFile = {};
newFile.collection = file; // wrap JSON object in new "collection" property
updateLocalCollection(newFileName, newFile);
updatePostman(newFileName, collection.collection_uid);
}
// ###############################################
// loop through collections in config file to call functions to handle conversion and update collection
config.collections.forEach( function (collection) {
var originalFileName = collection.from;
var newFileName = collection.to ;
handleConversion(originalFileName, newFileName);
updateCollection(newFileName,collection);
});
{
"name": "update-postman",
"version": "0.0.1",
"scripts": {
"generate": "node generate.js"
},
"dependencies": {
},
"devDependencies": {
"request": "^2",
"openapi-to-postmanv2": "^4"
}
}
// go to https://web.postman.co/settings/me/api-keys
// or
// (or go to the online postman thing where you see your collections, then click your profile icon top right
// then "Settings" in the dropdown
// then API keys on the left
// then "generate key"
// copy that key and paste it here
module.exports = {
"key": "api_key_here_mine_started_with_PMAK"
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment