2

I am able to either get the API key to be represented as a header or as a tag on the end of the URL, but I am needing it to be both. Is there any way for this to be possible? The picture is linked here

Arvind Maurya
  • 910
  • 12
  • 25
tdmiller
  • 2,102
  • 2
  • 13
  • 28

2 Answers2

2

Define both the header and the query parameter in the securityDefinitions section (in OpenAPI 2.0) or the components/securitySchemes section (in OpenAPI 3.0) of your API definition:

# swagger: '2.0'

securityDefinitions:
  apiKeyHeader:
    type: apiKey
    in: header
    name: X-EGEN-AccessTokenID
  apiKeyQueryParam:
    type: apiKey
    in: query
    name: api_key   # replace with your query param name

Then, if you need both the header and query param be passed in the same request:

security:
  - apiKeyHeader: []
    apiKeyQueryParam: []

Or if either the header or query param should be used, but not both:

security:
  - apiKeyHeader: []
  - apiKeyQueryParam: []

More info here: http://swagger.io/docs/specification/authentication/api-keys/

In Swagger UI, when you click "Authorize", you will be enter the values for both the header and the query parameter.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • Could you please help me with this , not able to add apiKey as query param in swagger 3.x i.e. latest version ...https://stackoverflow.com/questions/49050969/setting-swagger-apikey-in-version-2-0?noredirect=1#comment85145482_49050969 – BdEngineer Mar 05 '18 at 10:42
  • Thanks for this, worked for me with `openapi: '3.0.0'` – Paul Watson Jan 28 '20 at 08:08
0
window.swaggerUi.api.clientAuthorizations.add(swashbuckleConfig.apiKeyName, new SwaggerClient.ApiKeyAuthorization(swashbuckleConfig.apiKeyName, key, "header"));
      window.swaggerUi.api.clientAuthorizations.add(swashbuckleConfig.apiKeyName + " query", new SwaggerClient.ApiKeyAuthorization(swashbuckleConfig.apiKeyName, key, "query"));
tdmiller
  • 2,102
  • 2
  • 13
  • 28