Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Bearer {JWT} in swagger django?

Tags:

django

swagger

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 = "/"
like image 457
Joseph Adam Avatar asked Nov 14 '25 17:11

Joseph Adam


1 Answers

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

like image 143
Carlos Osuna Avatar answered Nov 17 '25 10:11

Carlos Osuna