Home

Awesome

<img src="https://voyager.postman.com/logo/postman-logo-orange.svg" width="320" alt="The Postman Logo">

Supercharge your API workflow. Modern software is built on APIs. Postman helps you develop APIs faster.

OpenAPI 3.0, 3.1 and Swagger 2.0 to Postman Collection

Build Status

<a href="https://www.npmjs.com/package/openapi-to-postmanv2" alt="Latest Stable Version">npm</a> <a href="https://www.npmjs.com/package/openapi-to-postmanv2" alt="Total Downloads">npm</a>

Contents

  1. Getting Started
  2. Command Line Interface
    1. Options
    2. Usage
  3. Using the converter as a NodeJS module
    1. Convert Function
    2. Options
    3. ConversionResult
    4. Sample usage
    5. Validate function
  4. Conversion Schema


🚀 We now also support OpenAPI 3.1 and Swagger 2.0 along with OpenAPI 3.0.



💭 Getting Started

To use the converter as a Node module, you need to have a copy of the NodeJS runtime. The easiest way to do this is through npm. If you have NodeJS installed you have npm installed as well.

$ npm install openapi-to-postmanv2

If you want to use the converter in the CLI, install it globally with NPM:

$ npm i -g openapi-to-postmanv2

📖 Command Line Interface

The converter can be used as a CLI tool as well. The following command line options are available.

openapi2postmanv2 [options]

Options

Usage

$ openapi2postmanv2 -s spec.yaml -o collection.json -p -O folderStrategy=Tags,includeAuthInfoInExample=false
$ openapi2postmanv2 -s spec.yaml -o collection.json -p  -c ./examples/cli-options-config.json
$ openapi2postmanv2 -s spec.yaml -o collection.json -p -O folderStrategy=Tags,requestParametersResolution=Example,optimizeConversion=false,stackLimit=50
$ openapi2postmanv2 --test

🛠 Using the converter as a NodeJS module

In order to use the convert in your node application, you need to import the package using require.

var Converter = require('openapi-to-postmanv2')

The converter provides the following functions:

Convert

The convert function takes in your OpenAPI 3.0, 3.1 and Swagger 2.0 specification ( YAML / JSON ) and converts it to a Postman collection.

Signature: convert (data, options, callback);

data:

{ type: 'file', data: 'filepath' }
OR
{ type: 'string', data: '<entire OpenAPI string - JSON or YAML>' }
OR
{ type: 'json', data: OpenAPI-JS-object }

options:

{
  schemaFaker: true,
  requestNameSource: 'fallback',
  indentCharacter: ' '
}
/*
All three properties are optional. Check the options section below for possible values for each option.
*/

Note: All possible values of options and their usage can be found over here: OPTIONS.md

callback:

function (err, result) {
  /*
  result = {
    result: true,
    output: [
      {
        type: 'collection',
        data: {..collection object..}
      }
    ]
  }
  */
}

Options

Check out complete list of options and their usage at OPTIONS.md

ConversionResult

Sample Usage

const fs = require('fs'),
  Converter = require('openapi-to-postmanv2'),
  openapiData = fs.readFileSync('sample-spec.yaml', {encoding: 'UTF8'});

Converter.convert({ type: 'string', data: openapiData },
  {}, (err, conversionResult) => {
    if (!conversionResult.result) {
      console.log('Could not convert', conversionResult.reason);
    }
    else {
      console.log('The collection object is: ', conversionResult.output[0].data);
    }
  }
);

Validate Function

The validate function is meant to ensure that the data that is being passed to the convert function is a valid JSON object or a valid (YAML/JSON) string.

The validate function is synchronous and returns a status object which conforms to the following schema

Validation object schema

{
  type: 'object',
  properties: {
    result: { type: 'boolean'},
    reason: { type: 'string' }
  },
  required: ['result']
}
Validation object explanation

🧭 Conversion Schema

postmanopenapirelated options
collectionNameinfo.title-
descriptioninfo.description + info.contact-
collectionVariablesserver.variables + pathVariables-
folderNamepaths.path / tags.namefolderStrategy
requestNameoperationItem(method).summary / operationItem(method).operationId / urlrequestNameSource
request.methodpath.method-
request.headersparameter (in = header)-
request.bodyoperationItem(method).requestBodyrequestParametersResolution, exampleParametersResolution
request.url.rawserver.url (path level server >> openapi server) + path-
request.url.variablesparameter (in = path)-
request.url.paramsparameter (in = query)-
api_key in (query or header)components.securitySchemes.api_keyincludeAuthInfoInExample