17

I've followed the instructions to create a Swagger documentation, and my documentation is now available using Swagger UI. I'd like to also generate the documentation as JSON or YAML so it's easy to import in e.g. Postman, but I can't find any suitable methods in the SwaggerModule, nor does the Swagger UI have any export button.

Kim Kern
  • 54,283
  • 17
  • 197
  • 195
Joakim Bugge
  • 173
  • 1
  • 1
  • 5

6 Answers6

28

According to this github issue you can just stringify the created Swagger document and e.g. write it to the file system like this:

const app = await NestFactory.create(ApplicationModule);
const options = new DocumentBuilder()
    .setTitle("Title")
    .setDescription("description")
    .setVersion("1.0")
    .build();
const document = SwaggerModule.createDocument(app, options);

fs.writeFileSync("./swagger-spec.json", JSON.stringify(document));
SwaggerModule.setup("/api", app, document);

await app.listen(80);
Kim Kern
  • 54,283
  • 17
  • 197
  • 195
  • 4
    This worked very well for me, I added an if condition to save that file only on development environment: `if (process.env.NODE_ENV === 'development')` – Crysfel Aug 15 '19 at 04:49
  • Is there a way to import this to Postmann as an Collection ? – Elrond Mar 09 '21 at 08:49
  • A note on importing fs/path module(s) if you get an error try importing like this. import * as fs from fs import * as path from path – aaronmbmorse Aug 13 '23 at 17:17
16

Tested in Nestjs v9

Suppose the docs path is as follows

http://localhost:3000/docs

Get JSON

http://localhost:3000/docs-json

Get YAML

http://localhost:3000/docs-yaml
Suhail Akhtar
  • 1,718
  • 15
  • 29
11

Try visiting /api/json instead of /api-json if you followed https://docs.nestjs.com/recipes/swagger.

JobaerAhamed
  • 134
  • 1
  • 5
  • in my case I use `SwaggerModule.setup('docs', app, document)`, so I download json file from _localhost:3000/docs-json_ – Hector Oct 19 '21 at 19:53
  • Are you sure about that? It seems the linked webpage references `/api-json`. – Winny Jan 11 '23 at 07:35
  • above comment is incorrect, directly from the linked doc as of April, 2023: To generate and download a Swagger JSON file, navigate to `http://localhost:3000/api-json` (assuming that your Swagger documentation is available under `http://localhost:3000/api`). – oyalhi Apr 14 '23 at 18:06
1

As well as the solution shown to write to disk (https://stackoverflow.com/a/51736406/5693245), you can still access on your own API endpoint.

As per docs, depending on whether you use swagger-ui-express or fastify to serve docs the location will be different

To generate and download a Swagger JSON file, navigate to http://localhost:3000/api-json (swagger-ui-express) or http://localhost:3000/api/json (fastify-swagger) in your browser (assuming that your Swagger documentation is available under http://localhost:3000/api).

It also depends on where you serve your API from, and assumes you use /api. If this is not the case replace with your endpoint, or in case you are not using a base URL for swagger-ui-express this would be http://localhost:3000/-json

chrismclarke
  • 1,995
  • 10
  • 16
0

Tested in Nestjs v8

GET http://{host}:{port}/docs

Get JSON

GET http://{host}:{port}/docs/json
0

With the latest version (v8 as of writing) of NestJS, following the setup in the openapi documentation, you should be able to access the json document without any extra setup with

GET http://{host}:{port}/api-docs
TameBadger
  • 1,580
  • 11
  • 15