Postman collection to OpenAPI specs in Nodejs

Install

To use local in your Node.js project as a library

npm i postman-to-openapi --save

To use as a cli

npm i postman-to-openapi -g

Usage

As library

Use the library is as easy as use a single method async postmanToOpenApi(input, outputPath, options), the parameters are:

ParamDescription
inputString. Path of the Postman collection file or value of the postman collection as String.
outputPathString. Path of the output file where the OpenAPi will be stored. This param is optional if not provided (undefined or null) no file will be saved.
optionsObject. Optional configuration, see options section for a detailed description.

The method return a promise string that contain the yml OpenAPI specification, only is saved to a file if the outputPath parameter is provided.

An example of usage:

const postmanToOpenApi = require('postman-to-openapi');

const postmanCollection = './path/to/postman/collection.json';
const outputFile = './api/collection.yaml';

// Async/await
try {
    // Without save the result in a file
    const result = await postmanToOpenApi(postmanCollection, null, { defaultTag: 'General' })
    console.log(`OpenAPI specs: ${result}`)
} catch (err) {
    console.log(err)
}

// Promise callback style
// With save the result in a file
postmanToOpenApi(postmanCollection, outputFile, { defaultTag: 'General' })
    .then(result => {
        console.log(`OpenAPI specs: ${result}`)
    })
    .catch(err => {
        console.log(err)
    })

As cli

After install just need to

p2o ./path/to/PostmantoCollection.json -f ./path/to/result.yml -o ./path/to/options.json

All the field described in options can be provided and used in the cli, for more info an all the available options just check the cli help

p2o -h

See demo in next gif:

Cli demo gif

Options

The third parameter used in the library method is an options object containing the optional parameters for the transformation, the allowed parameters are:

ParamDescription
infoBasic API information
defaultTagValues of the default tag object.
pathDepthNumber of subpaths that should be part of the operation path.
authGlobal authorization definition object.
serversServer list for the OpenApi specs.
externalDocsInfo about the API external documentation.
foldersConfig object for folders and nested folders in postman collection.
responseHeadersIndicate if should parse the response headers from the collection examples.
replaceVarsBoolean value to indicate if postman variables should be replaced.
additionalVarsObject to provide additional values for variables replacement.

Author

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!