Custom Swagger API
Example of a Custom API with Authorization and Swagger
Last updated
Was this helpful?
Example of a Custom API with Authorization and Swagger
Last updated
Was this helpful?
This article covers how to create a Custom API controller protected by the backoffice authorization policies. It also shows how to enable the authorization UI in Swagger docs.
This example can be a starting point for creating a secure custom API with automatic Swagger documentation. You can find other examples in the article.
Create a new .cs
file called MyBackOfficeSecurityRequirementsOperationFilter
in your Umbraco project.
Add the following code so that the new API shows in the Swagger documentation and Swagger UI:
Our filter inherits from BackOfficeSecurityRequirementsOperationFilterBase
. This marks our API as supporting authorization via Swagger.
MyConfigureSwaggerGenOptions
configures our API swagger docs with our filter applied.
MyComposer
makes sure the swagger generator knows about our API docs configuration at runtime.
Add the ApiController to setup the logic behind the endpoint:
Run the project and navigate to {yourdomain}/umbraco/swagger
.
Choose the swagger documentation we created with the code above named My API v1 from Select a definition.
Here, we can find the endpoint that we created:
Click on the Authorize button to authenticate.
Try out the endpoint using the Try it out button.
Click on Execute.
We now get the response we have setup using the code: "Hello, {{userName}}"
.