postman-to-swagger icon indicating copy to clipboard operation
postman-to-swagger copied to clipboard

Convert Postman 2.1 Collections to Swagger 2.0 or OpenAPI 3.0

Postman to Swagger

This project intends to output a valid swagger.yaml or openapi.yaml file from a Postman collection input.

You can convert:

INPUT OUTPUT
Postman 2.1 (PostmanCollection.json) Swagger 2.0 (swagger.yaml)

OpenAPI 3.0 (openapi.yaml)

Please Note:

Postman schema doesn't quite match up 1-to-1 against Swagger/OpenAPI schema. So some target spec defaults are automatically implemented when absent in the source spec.

Installation

npm install tecfu/postman-to-swagger

Options

Options: All Targets Specs object

Param Type Description
source_spec string default: "postman2.1".
options: "postman2.1"
target_spec string default: "openapi3.0".
options: "swagger2.0", "openapi3.0"
require_all array default: ["headers", "body", "query", "path"]
omit object default: {
headers: ["Content-Type", "X-Requested-With"]
}
info object default: {}
responses object default: {
200: {
description: "OK"
}
}

Options: Swagger 2.x object

Param Type Description
host string default: ''
Note: Only applies to Swagger 2.0 output
basepath string default: ''
Note: Only applies to Swagger 2.0 output
schemes string default: ''
Note: Only applies to Swagger 2.0 output

Options: OpenAPI 3.x object

Param Type Description
servers array default: []
Note: Only applies to OpenAPI 3.0 output

Example

const p2s = require('postman-to-swagger')
const yaml = require('js-yaml')
const fs = require('fs')
const postmanJson = require('./postman_collection.json')
const swaggerJson = p2s(postmanJson, {
  target_spec: "swagger2.0",
  info: {
    version: 'v1'
  }
})

//let output = JSON.stringify(swaggerJson, null, 2)
let output = yaml.safeDump(swaggerJson)

// Save to file
fs.writeFileSync(
  'swagger.yaml',
  output,
  'utf8'
)

License

MIT