Swagger: Feature Request: Export to JSON

Created on 24 Feb 2018  Â·  4Comments  Â·  Source: nestjs/swagger

It would be very useful can export the Swagger documentation to JSON for external use, or exchange among other tools.

example

Most helpful comment

It would probably be useful to add to the docs, but if you're just asking for how to do 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);

All 4 comments

It would probably be useful to add to the docs, but if you're just asking for how to do 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);

thanks @malbertSC

Or try to add -json at the end of your URL path, there is a hint about this in the doc : https://docs.nestjs.com/recipes/swagger

(Works with custom path, always add -json at the end of your URL)

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings