Add an authorization header to your swagger-ui with Swashbuckle

Out of the box there’s no way to add an Authorization header to your API requests from swagger-ui. Fortunately (if you’re using ASP.NET), Swashbuckle 5.0 is extendable, so it’s very easy to add a new IOperationFilter to do it for us:

public class AddAuthorizationHeaderParameterOperationFilter : IOperationFilter
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        if (operation.parameters != null)
            operation.parameters.Add(new Parameter
                name = "Authorization",
                @in = "header",
                description = "access token",
                required = false,
                type = "string"

Now all you need to do is register it in your EnableSwagger call:

    .EnableSwagger(c =>
        c.SingleApiVersion("v1", "Commerce Services - Discounts");

        foreach (var commentFile in xmlCommentFiles)

    .EnableSwaggerUi(config => config.DocExpansion(DocExpansion.List));

Once that’s done it’ll give you an input field where you can paste your Authorization header. Don’t forget to add the word “bearer” if you’re using a JWT token:

Edit: I wrote this more than a year ago using Swashbuckle 5.2.1, it may not work with later versions.


5 thoughts on “Add an authorization header to your swagger-ui with Swashbuckle

  1. Thanks for sharing. This only works for POSTs. My apis have some GETs that require a token and no other inputs, params, etc, Therefore the authorization input field does not show for this.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s