# simple and best way to document a rest api created in nodejs

The best and simplest way to document a Node.js REST API is to <mark class="HxTRcb">use the [Swagger/OpenAPI specification](https://www.google.com/search?client=ubuntu-sn&channel=fs&q=Swagger%2FOpenAPI+specification&mstk=AUtExfBkf1TCFmy-XeGI2Qu0qyDR6M50mbfInI36mzivEgxjR3VmrNuA55DGEXPNMBI_M2U9S98k5MjemEu3bMDIz7wPpb0_Wv6lplFDYLHao-llLwGdsqZ1KwIPVrCBDyjuMwdpJKyofSWYYThawL262u-zbLZ8_F9NB3euNAPYgzlm7SY&csui=3&ved=2ahUKEwiS-9K434qRAxVyR2wGHaLMGWAQgK4QegQIARAE) with tools like [swagger-jsdoc](https://www.google.com/search?client=ubuntu-sn&channel=fs&q=swagger-jsdoc&mstk=AUtExfBkf1TCFmy-XeGI2Qu0qyDR6M50mbfInI36mzivEgxjR3VmrNuA55DGEXPNMBI_M2U9S98k5MjemEu3bMDIz7wPpb0_Wv6lplFDYLHao-llLwGdsqZ1KwIPVrCBDyjuMwdpJKyofSWYYThawL262u-zbLZ8_F9NB3euNAPYgzlm7SY&csui=3&ved=2ahUKEwiS-9K434qRAxVyR2wGHaLMGWAQgK4QegQIARAF) and [swagger-ui-express](https://www.google.com/search?client=ubuntu-sn&channel=fs&q=swagger-ui-express&mstk=AUtExfBkf1TCFmy-XeGI2Qu0qyDR6M50mbfInI36mzivEgxjR3VmrNuA55DGEXPNMBI_M2U9S98k5MjemEu3bMDIz7wPpb0_Wv6lplFDYLHao-llLwGdsqZ1KwIPVrCBDyjuMwdpJKyofSWYYThawL262u-zbLZ8_F9NB3euNAPYgzlm7SY&csui=3&ved=2ahUKEwiS-9K434qRAxVyR2wGHaLMGWAQgK4QegQIARAG)</mark>. This approach involves writing documentation in a standard format (like a JavaScript/JSDoc comment block) that the `swagger-jsdoc` package parses into an OpenAPI specification. The `swagger-ui-express` package then serves this specification to generate an interactive and user-friendly documentation UI.<span class="uJ19be notranslate" data-wiz-uids="GXW3hf_k,GXW3hf_l,GXW3hf_m"><span class="vKEkVd" data-animation-atomic=""> </span></span>

<div class="Y3BBE" data-hveid="CAEQAA" data-processed="true" data-sfc-cp="" id="bkmrk-" jsaction="rcuQ6b:&GXW3hf_8|npT2md" jscontroller="zcfIf" jsuid="GXW3hf_8">  
</div><div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--1" jsaction="rcuQ6b:&GXW3hf_p|npT2md" jscontroller="KHhJQ" jsuid="GXW3hf_p">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-1.-set-up-your-proje" jscontroller="a7qCn" jsuid="GXW3hf_q" role="heading">1. Set up your project</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--2" jscontroller="a7qCn" jsuid="GXW3hf_q" role="heading">  
</div>- <span class="T286Pc" data-sfc-cp="">**Initialize your project:**</span><div class="r1PmQe" data-hveid="CAUQAQ" data-wiz-uids="GXW3hf_25,GXW3hf_26,GXW3hf_27" jscontroller="HP6Sjf" jsuid="GXW3hf_24"><div><div class="pHpOfb" data-animation-atomic=""><div class="vVRw1d">bash</div></div></div></div>
- ```
    mkdir my-api
    cd my-api
    npm init -y
    
    ```
- <span class="T286Pc" data-sfc-cp="">**Install necessary packages:**</span><div class="r1PmQe" data-hveid="CAUQBA" data-wiz-uids="GXW3hf_2e,GXW3hf_2f,GXW3hf_2g" jscontroller="HP6Sjf" jsuid="GXW3hf_2d"><div><div class="pHpOfb" data-animation-atomic=""><div class="vVRw1d">bash</div></div></div></div>
- ```
    npm install express swagger-ui-express swagger-jsdoc
    
    ```
- <span class="T286Pc" data-sfc-cp="">**Create a basic Express server** (`app.js`):</span><div class="r1PmQe" data-hveid="CAUQBw" data-wiz-uids="GXW3hf_2o,GXW3hf_2p,GXW3hf_2q" jscontroller="HP6Sjf" jsuid="GXW3hf_2n"><div><div class="pHpOfb" data-animation-atomic=""><div class="vVRw1d">javascript</div></div></div></div>

```
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
});

```

<span class="uJ19be notranslate" data-wiz-uids="GXW3hf_2t,GXW3hf_2u,GXW3hf_2v"><span class="vKEkVd" data-animation-atomic=""> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--3" jsaction="rcuQ6b:&GXW3hf_2w|npT2md" jscontroller="KHhJQ" jsuid="GXW3hf_2w">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-2.-define-the-api-do" jscontroller="a7qCn" jsuid="GXW3hf_2x" role="heading">2. Define the API documentation<span class="uJ19be notranslate" data-wiz-uids="GXW3hf_2y,GXW3hf_2z,GXW3hf_30"><span class="vKEkVd" data-animation-atomic=""> </span></span></div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--4" jscontroller="a7qCn" jsuid="GXW3hf_2x" role="heading">  
</div>- <span class="T286Pc" data-sfc-cp="">**Create a file for your documentation specification** (e.g., `swaggerDocs.js`):</span><div class="r1PmQe" data-hveid="CAcQAQ" data-wiz-uids="GXW3hf_37,GXW3hf_38,GXW3hf_39" jscontroller="HP6Sjf" jsuid="GXW3hf_36"><div><div class="pHpOfb" data-animation-atomic=""><div class="vVRw1d">javascript</div></div></div></div>
- ```
    const swaggerJsdoc = require('swagger-jsdoc');
    
    const options = {
      definition: {
        openapi: '3.0.0',
        info: {
          title: 'My Node.js API',
          version: '1.0.0',
          description: 'A sample REST API for testing documentation',
        },
        servers: [
          {
            url: 'http://localhost:3000',
            description: 'Development server',
          },
        ],
      },
      apis: ['./routes/*.js'], // Path to the API routes files
    };
    
    const swaggerSpec = swaggerJsdoc(options);
    
    module.exports = swaggerSpec;
    
    ```
- <span class="T286Pc" data-sfc-cp="">**Add JSDoc comments to your route files** (e.g., `routes/userRoutes.js`) to define the endpoints:</span><div class="r1PmQe" data-hveid="CAcQBA" data-wiz-uids="GXW3hf_3h,GXW3hf_3i,GXW3hf_3j" jscontroller="HP6Sjf" jsuid="GXW3hf_3g"><div><div class="pHpOfb" data-animation-atomic=""><div class="vVRw1d">javascript</div></div></div></div>

```
// routes/userRoutes.js

/**
 * @openapi
 * /users:
 *   get:
 *     summary: Get all users
 *     tags:
 *       - Users
 *     responses:
 *       '200':
 *         description: A list of users
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/User'
 */
// ... your router code

```

<span class="uJ19be notranslate" data-wiz-uids="GXW3hf_3m,GXW3hf_3n,GXW3hf_3o"><span class="vKEkVd" data-animation-atomic=""> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--5" jsaction="rcuQ6b:&GXW3hf_3p|npT2md" jscontroller="KHhJQ" jsuid="GXW3hf_3p">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-3.-integrate-with-yo" jscontroller="a7qCn" jsuid="GXW3hf_3q" role="heading">3. Integrate with your Express app<span class="uJ19be notranslate" data-wiz-uids="GXW3hf_3r,GXW3hf_3s,GXW3hf_3t"><span class="vKEkVd" data-animation-atomic=""> </span></span></div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--6" jscontroller="a7qCn" jsuid="GXW3hf_3q" role="heading">  
</div>- <span class="T286Pc" data-sfc-cp="">**Require the Swagger UI Express and your Swagger specification** in your `app.js` file:</span><div class="r1PmQe" data-hveid="CAkQAQ" data-wiz-uids="GXW3hf_40,GXW3hf_41,GXW3hf_42" jscontroller="HP6Sjf" jsuid="GXW3hf_3z"><div><div class="pHpOfb" data-animation-atomic=""><div class="vVRw1d">javascript</div></div></div></div>

```
const express = require('express');
const app = express();
const port = 3000;
const swaggerUi = require('swagger-ui-express');
const swaggerSpec = require('./swaggerDocs.js'); // Path to your swaggerDocs.js

// ... (your existing code)

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

// ... (rest of your app.js)

```

<span class="uJ19be notranslate" data-wiz-uids="GXW3hf_45,GXW3hf_46,GXW3hf_47"><span class="vKEkVd" data-animation-atomic=""> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--7" jsaction="rcuQ6b:&GXW3hf_48|npT2md" jscontroller="KHhJQ" jsuid="GXW3hf_48">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-4.-run-the-server%C2%A0" jscontroller="a7qCn" jsuid="GXW3hf_49" role="heading">4. Run the server<span class="uJ19be notranslate" data-wiz-uids="GXW3hf_4a,GXW3hf_4b,GXW3hf_4c"><span class="vKEkVd" data-animation-atomic=""> </span></span></div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--8" jscontroller="a7qCn" jsuid="GXW3hf_49" role="heading">  
</div>- <span class="T286Pc" data-sfc-cp="">**Start your server:**</span><div class="r1PmQe" data-hveid="CAsQAQ" data-wiz-uids="GXW3hf_4i,GXW3hf_4j,GXW3hf_4k" jscontroller="HP6Sjf" jsuid="GXW3hf_4h"><div><div class="pHpOfb" data-animation-atomic=""><div class="vVRw1d">bash</div></div></div></div>
- ```
    node app.js
    
    ```
- <span class="T286Pc" data-sfc-cp="">**Access the interactive documentation**<span id="bkmrk-by-navigating-to"> by navigating to </span><span id="bkmrk-http%3A%2F%2Flocalhost%3A300">`http://localhost:3000/api-docs`</span><span id="bkmrk-in-your-browser"> in your browser</span></span>