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.

- 54,283
- 17
- 197
- 195

- 173
- 1
- 1
- 5
-
1Yeah I've been wondering about this too, NestJS can be a bit obfuse – Lars Holdaas Oct 22 '19 at 07:41
6 Answers
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);

- 54,283
- 17
- 197
- 195
-
4This 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
-
-
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
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

- 1,718
- 15
- 29
-
http://localhost:3000/docs-json works ok. http://localhost:3000/docs-yaml not found. – yunnysunny Jul 24 '23 at 03:26
-
1I just tried and both routes worked for me. Make sure you are using the latest version of Nestjs (10 for core and 7 for swagger) – Farid Hajnal Aug 16 '23 at 18:40
Try visiting /api/json
instead of /api-json
if you followed https://docs.nestjs.com/recipes/swagger.

- 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
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

- 1,995
- 10
- 16
Tested in Nestjs v8
GET http://{host}:{port}/docs
Get JSON
GET http://{host}:{port}/docs/json
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

- 1,580
- 11
- 15