Search code examples
node.jsexpressswaggerswagger-uiswagger-2.0

How to represent custom token in header in Swagger UI(swagger.json) in nodejs


I am creating a Restful server in ExpressJs. I have integrated swagger-jsdoc. Following is the related files. Below(header.png) is how I expect my header to look like in swagger UI. But, while I open my swagger UI (http://localhost:3000/api-docs/), I am not able to see Token tags (token and Authentication) in the header.

enter image description here

swagger.json

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "Viswa API"
    },
    "host": "localhost:3000",
    "basePath": "/api",
    "tags": [{
        "name": "Customers",
        "description": "API for customers in the system"
    }],
    "schemes": [
        "http"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ],
    "securityDefinitions": {
        "Bearer": {
            "type": "apiKey",
            "name": "Authorization",
            "in": "header"
        },
        "JWT": {
            "type": "apiKey",
            "name": "token",
            "in": "header"
        }
    },
    "paths": {
        "/customer": {
            "post": {
                "tags": [
                    "Customers"
                ],
                "description": "Create new customer in system",
                "parameters": [{
                    "name": "customer",
                    "in": "body",
                    "description": "Customer that we want to create",
                    "schema": {
                        "$ref": "#/definitions/Customer"
                    }
                }],
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "201": {
                        "description": "New customer is created",
                        "schema": {
                            "$ref": "#/definitions/Customer"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "Customer": {
            "required": [
                "email"
            ],
            "properties": {
                "customer_name": {
                    "type": "string"
                },
                "customer_email": {
                    "type": "string"
                }
            }
        }
    }
}

app.route

var apiRoutes = express.Router();
app.use('/api', apiRoutes);
// swagger definition
var swaggerUi = require('swagger-ui-express'),
swaggerDocument = require('../swagger.json');

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/api/v1', apiRoutes);

Current Swagger UI:


Solution

  • You are missing the security tag. You can define it either globally just below the securityDefinitions tag or one for each API endpoint.

    Have a look at this question.