Search code examples
djangoswagger

How to add Bearer {JWT} in swagger django?


when I authorize myself in Swagger UI, I have to write "Bearer {then I write JWT} here"

How can I add the string "Bearer" automatically before the JWT token in swagger UI? Here is my Swagger Settings:

SWAGGER_SETTINGS = {
    "SECURITY_DEFINITIONS": {
        "JWT [Bearer {JWT}]": {
            "name": "Authorization",
            "type": "apiKey",
            "in": "header",
        }
    },
    "USE_SESSION_AUTH": False,
}

FORCE_SCRIPT_NAME = "/"

Solution

  • I recommend you to migrate from drf-yasg to drf_spectacular, it already includes JWT authentication automatically and without so many complications, it even uses OpenAPI 3 (drf-yasg uses OpenAPI 2 and is becoming obsolete)

    Automatic Generation

    enter image description here

    Authenticate with JWT

    enter image description here

    See this From drf-yasg to OpenAPI 3