How to use ‘Authorization: Bearer ‘ in a Swagger Spec

Maybe this can help:

swagger: '2.0'
info:
  version: 1.0.0
  title: Bearer auth example
  description: >
    An example for how to use Bearer Auth with OpenAPI / Swagger 2.0.

host: basic-auth-server.herokuapp.com
schemes:
  - http
  - https
securityDefinitions:
  Bearer:
    type: apiKey
    name: Authorization
    in: header
    description: >-
      Enter the token with the `Bearer: ` prefix, e.g. "Bearer abcde12345".
paths:
  /:
    get:
      security:
        - Bearer: []
      responses:
        '200':
          description: 'Will send `Authenticated`'
        '403': 
          description: 'You do not have necessary permissions for the resource'

You can copy&paste it to https://editor.swagger.io to check out the results.

There are also several examples in the Swagger Editor web with more complex security configurations which could help you.

Important: In this example, API consumers must include the “Bearer” prefix as part of the token value. For example, when using Swagger UI’s “Authorize” dialog, you need to enter Bearer your_token instead of just your_token.

Swagger UI's Authorization dialog

Leave a Comment