Integrate Open API (Swagger) With Node And Express

This article will explain how to integrate swagger (Open API) with Node & express. Swagger makes it very easy for a backend developer to document, test, and explain API endpoints he/she’s been working on a front-end developer or anyone looking to consume those endpoints.

 

Integrate Open API (Swagger) With Node And Express

 

Setup 

 

Before we get started into this we should have few things installed in our machine.

  • Visual Studio Code -> Visual Studio Code
  • Node.js -> Node.js

Source Code – Git Code

 

Required Packages

 

npm init

npm install swagger-jsdoc swagger-ui-express express nodemon

 

Express – For Server

 

Swagger – For API’s Documentation in UI

 

Nodemon – will use this to restart our server automatically whenever we make changes to our files.

 

After installing the required packages let’s add the new file to set up the Swagger configuration and as well adding the API endpoints in Node

 

Structure of the Project

 

Integrate Open API (Swagger) With Node And Express

 

Setting up the swagger 

 

Swagger UI can be set up for both the front end & backend as well. Since this article is about the Swagger with Node.js. I will be setting up the the swagger in Node.js express app only. you can explore the other options here

 

In your api.js

  1.   

  2. const

     swaggerOptions = {  

  3.     swaggerDefinition: {  

  4.         info: {  

  5.             title:

    ‘Employee API’

    ,  

  6.             version:

    ‘1.0.0’

      

  7.         }  

  8.     },  

  9.     apis:[

    ‘api.js’

    ],  

  10. }  

  11. const

     swaggerDocs = swaggerJSDoc(swaggerOptions);  

  12. app.use(

    ‘/api-docs’

    ,swaggerUI.serve,swaggerUI.setup(swaggerDocs));  

  13.    

 

Add Swagger Before Each End Point

  1.  

  2.  

  3.  

  4.  

  5.  

  6.  

  7.  

  8.  

  9.  

  10.   

  11. app.get(

    ‘/Employees’

    ,(req,res)=>{  

  12.     res.send([  

  13.         {  

  14.             id:1, Name:

    ‘Jk’

      

  15.         },  

  16.         {  

  17.             id:2,Name:

    ‘Jay’

      

  18.         }  

  19.     ])  

  20. });  

For the demo purpose, I have added four API’s (Get, Post, Put, Delete) added the swagger setup for the remaining endpoints as well

 

Final api.js

  1. const

     express = require(

    ‘express’

    );  

  2. const

     swaggerJSDoc = require(

    ‘swagger-jsdoc’

    );  

  3. const

     swaggerUI = require(

    ‘swagger-ui-express’

    );  

  4.   

  5. const

     app = express();  

  6.   

  7. app.listen(5000,()=>console.log(

    “listening on 5000”

    ));  

  8.   

  9.   

  10. const

     swaggerOptions = {  

  11.     swaggerDefinition: {  

  12.         info: {  

  13.             title:

    ‘Employee API’

    ,  

  14.             version:

    ‘1.0.0’

      

  15.         }  

  16.     },  

  17.     apis:[

    ‘api.js’

    ],  

  18. }  

  19. const

     swaggerDocs = swaggerJSDoc(swaggerOptions);  

  20. app.use(

    ‘/api-docs’

    ,swaggerUI.serve,swaggerUI.setup(swaggerDocs));  

  21.    

  22.  

  23.  

  24.  

  25.  

  26.  

  27.  

  28.  

  29.  

  30.  

  31.   

  32. app.get(

    ‘/Employees’

    ,(req,res)=>{  

  33.     res.send([  

  34.         {  

  35.             id:1, Name:

    ‘Jk’

      

  36.         },  

  37.         {  

  38.             id:2,Name:

    ‘Jay’

      

  39.         }  

  40.     ])  

  41. });  

  42.   

  43.  

  44.  

  45.  

  46.  

  47.  

  48.  

  49.  

  50.  

  51.  

  52.  

  53.  

  54.  

  55.  

  56.  

  57.  

  58.   

  59.  app.post(

    ‘/Employees’

    ,(req,res)=>{  

  60.    res.status(201).send();  

  61. });  

  62.  

  63.  

  64.  

  65.  

  66.  

  67.  

  68.  

  69.  

  70.  

  71.  

  72.  

  73.  

  74.  

  75.  

  76.  

  77.   

  78.  app.put(

    ‘/Employees’

    ,(req,res)=>{  

  79.     res.status(201).send();  

  80.  });  

  81.    

  82.  

  83.  

  84.  

  85.  

  86.  

  87.  

  88.  

  89.  

  90.  

  91.  

  92.  

  93.  

  94.  

  95.  

  96.   

  97.   app.

    delete

    (

    ‘/Employees’

    ,(req,res)=>{  

  98.     res.status(201).send();  

  99.  });  

Run the URL in the browser

 

Now you can do the npm start in the terminal to your app and it will navigate to the browser we have to add the /api-docs at the end of the URL so that i will navigate to the swagger which we have configured and you will see the Swagger UI based on your generated swagger.json.

 

Terminal

 

Integrate Open API (Swagger) With Node And Express

 

Swagger

 

Integrate Open API (Swagger) With Node And Express

 

Testing the API with Swagger

 

Integrate Open API (Swagger) With Node And Express

 

Hope this article helps you !! 

 

Keep learning ……!

After integrating the swagger setup lets define the swagger endpoint with description and response codes in a particular format so that we can able to access those API’s inside the swagger after running in the browser