Skip to content

Instantly share code, notes, and snippets.

@lionrajkumar
Created July 10, 2025 12:09
Show Gist options
  • Select an option

  • Save lionrajkumar/f26697302faceca67ed9cf6e8af7d66d to your computer and use it in GitHub Desktop.

Select an option

Save lionrajkumar/f26697302faceca67ed9cf6e8af7d66d to your computer and use it in GitHub Desktop.
Postman to Swagger (OAS 2) Documentation

Postman to Swagger (OAS 2) Documentation

Getting started

The easiest way to get started with this Collection is to click the fork button to fork this Collection to your own Workspace and use Postman to send requests.

Fetch the ID of the Collection you wish to convert. This can be found by selecting the Collection in Postman, navigating to the right-hand sidebar, clicking the Info tab (symbolized by the i icon), and copying the ID.

Generate a Swagger Schema

Method: GET

URL: https://api.getpostman.com/collections/31XXX524-0XX23617-XXXX-XXXX-XXXX-XXXXX22b1

Headers: X-API-Key:PMAK-XXXXXXXX-XXXXXXXXXXXXXXX11e613

Scripts [ Post-response ]

const res = pm.response.json();
const collection = JSON.stringify(res.collection);


const makeVisualizerTemplate = (swagger, errors, warnings) => `Swagger:<br> <textarea style="width: 100vw; height: 100vh; font-family: monospace;">${JSON.stringify(swagger)}</textarea><br>
Errors:<br><textarea style="width: 100vw; height: 100vh; font-family: monospace;">${JSON.stringify(errors)}</textarea><br>
Warnings:<br><textarea style="width: 100vw; height: 100vh; font-family: monospace;">${JSON.stringify(warnings)}</textarea>`;

pm.sendRequest({
    url: `https://demo.postmansolutions.com/postman2swagger`,
    method: 'POST',
    header: 'Content-Type:application/json',
    body: {
        mode: 'raw',
        raw: collection
    }
}, (err, res) => {
    const payload = JSON.parse(res.text());
    const visualizerTemplate = makeVisualizerTemplate(payload.swagger, payload.errors, payload.warnings);

    pm.visualizer.set(visualizerTemplate);
});

This request will generate a Swagger 2.0 Schema from a Postman Collection. Just hit Send, and click Visualize in the response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment