swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

API Key not supplied on initial resource listing call.

Open watchwithmike opened this issue 11 years ago • 5 comments

When loading the Swagger UI with an API key configured, the initial call to the resource listing (i.e. swagger.json) does not include the configured API key. Subsequent calls to the resource listing do have the key appended.

To reproduce:

  1. Uncomment the key pre-population code in the sample index.html file.
  2. Load the page while viewing the Network tab in your favorite browser development window.
    • Observe swagger.json is pulled from http://petstore.swagger.io/v2/swagger.json
  3. Click the Explore button.
    • Observe swagger.json is pulled from http://petstore.swagger.io/v2/swagger.json?api_key=myApiKeyXXXX123456789
    • Note the api_key is appended.

This behavior seems inconsistent. Our resource listings require an access key be provided, is there another way we should be approaching this?

watchwithmike avatar Jun 25 '15 19:06 watchwithmike

You should be passing your authorizations to the SwaggerUi constructor so they are available when swagger-ui retrieves these documents. If you add the authorizations after you initialize SwaggerUi, they will only be used for subsequent API calls. So your code should look like this:

new SwaggerUi({
// ...
  authorizations: {
    "api_key": new SwaggerClient.ApiKeyAuthorization("X-YOUR-API-KEY-HEADER", "YOUR-API-KEY-VALUE", "query")
  }
// ...
});

whitlockjc avatar Jun 25 '15 19:06 whitlockjc

Thanks, that did the trick. I added your comments to the README.md and submitted that as a pull request in case it's useful to others.

watchwithmike avatar Jun 25 '15 20:06 watchwithmike

How do I support multiple API keys based on the user? This solution seems to be hardcoded for single user unless I am missing something here.

mit2nil avatar Apr 29 '16 04:04 mit2nil

@mit2nil you might open a new issue. You can have as many keys as needed based on your securityDefinitions.

fehguy avatar May 03 '16 04:05 fehguy

@whitlockjc Can you please help with this. https://stackoverflow.com/questions/49050969/setting-swagger-apikey-in-version-2-0

shatestest avatar Mar 02 '18 10:03 shatestest