openapi

How to define schema of API key in securitySchemes for OpenAPI?


I want to define the format/schema for the API key as a UUID in the securitySchemes section of my OpenAPI spec. Below are my two attempts, neither of which pass validation with error Schema validation: Property 'format' / 'schema' is not allowed.

Can this be done and what would the right syntax be?

components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      schema:
        type: string
        format: uuid
      in: header
      name: authToken
components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      format: uuid
      in: header
      name: authToken

Solution

  • you need to define the securityScheme component and you can additionally define the same header name, in this case, authorization as a request parameter at the operation level.

    openapi: 3.0.4
    info:
      title: Example API
      version: 1.0.0
      description: An example API to demonstrate OpenAPI specification with security schemes.
    paths:
      'things/':
        get:
          summary: List things
          security:
            - apiKey: []
          parameters:
            - $ref: '#/components/parameters/header_authorization'
    components:
      securitySchemes:
        apiKey:
          type: apiKey
          name: authorization
          in: header
      parameters:
        header_authorization:
          name: authorization
          in: header
          required: true
          schema:
            type: string
            format: uuid