For the complete documentation index, see llms.txt. This page is also available as Markdown.

Custom Backoffice API

Example of a Custom Backoffice API with Authorization and OpenAPI.

This article covers how to create a Custom API controller protected by the backoffice authorization policies. It also shows how to enable authorization in Swagger UI.

Before proceeding, make sure to read the Management API article. It provides information about the OpenAPI documentation and Authorization used in this article.

The following example can be a starting point for creating a secure custom API with automatic OpenAPI documentation. You can find other examples in the API versioning and OpenAPI article.

  1. Create a composer to register the OpenAPI document so that the new API shows in the OpenAPI documentation and Swagger UI:

MyApiComposer.cs
using Umbraco.Cms.Api.Common.OpenApi;
using Umbraco.Cms.Api.Management.OpenApi;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Composing;

namespace Umbraco.Cms.Web.UI.Custom;

public class MyApiComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
        => builder.AddBackOfficeOpenApiDocument(
            "my-api-v1",
            document => document
                .WithTitle("My API v1")
                .WithBackOfficeAuthentication()
                .WithJsonOptions(Constants.JsonOptionsNames.BackOffice));
}

AddBackOfficeOpenApiDocument registers a custom OpenAPI document with Umbraco's defaults applied:

  • It includes controllers whose [MapToApi] attribute value matches the document name.

  • It applies Umbraco's schema and operation ID conventions.

  • It adds the document to the Swagger UI dropdown.

The builder methods configure additional behavior:

  • WithBackOfficeAuthentication() enables OAuth2-based backoffice authorization in Swagger UI.

  • WithJsonOptions(Constants.JsonOptionsNames.BackOffice) aligns schema generation with how the backoffice serializes responses at runtime.

  1. Create a new file MyApiController.cs with the following controller:

[JsonOptionsName(Constants.JsonOptionsNames.BackOffice)] tells the controller to serialize responses using the backoffice JSON options. This must match the WithJsonOptions call in the composer so the OpenAPI schema reflects the actual serialization output.

  1. Run the project and navigate to {yourdomain}/umbraco/openapi.

  2. Choose the OpenAPI document created with the code above named My API v1 from Select a definition.

Created Custom API in OpenAPI documentation

Here, you can find the endpoint that was created:

  1. Click on the Authorize button to authenticate.

  2. Try out the endpoint using the Try it out button.

  3. Click on Execute.

Trying out the endpoint

You now get the response you have set up using the code: "Hello, <user name>".

Last updated

Was this helpful?