Loading...
Skip to main content

Setup OpenAPI / Swagger

Why?​

OpenApi allows standardization of all the typings and communication between Backend and Frontend, and is language agnostic. Leveraging openapi-generator, it will generate the sdks for our mobile frontend and SPA web frontends.

Setup in NestJS​

The code that is used to setup NestJS Swagger are all residing in main.ts file of our NestJS project. It shall look as follows;

async function bootstrap() {
// ... Some code above
const config = new DocumentBuilder()
.setTitle("Cerev API")
.setDescription("List of APIs used in Cerev")
.setVersion("1.2.0")
.addBearerAuth(
{
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
description: "Enter Firebase Id Token",
},
"JWT-auth"
)
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup("api", app, document);

await app.listen(3000);
}
bootstrap();

With this, you will be able to access the full list of API when you access http://localhost:3000/api which is the reserved endpoint to display our Swagger documentation.

Read more on Swagger Nestjs

Generate yaml / json​

We need to leverage openapi-generator to generate our client side sdk for us to make use of. The address to obtain the yaml or json are as follows;

TypeAddres
yamlhttp://localhost:3000/api-yaml
jsonhttp://localhost:3000/api-json

We shall now move to either mobile section / or web section to continue our front-end work