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

API Versioning and OpenAPI

How to use API versioning and OpenAPI for your own APIs.

Umbraco uses Microsoft.AspNetCore.OpenApi to document its APIs. Out of the box, you get the following OpenAPI documents:

  • Management — the backoffice Management API.

  • Delivery — the Content Delivery API. Only present when the Delivery API is enabled.

  • Default — a catch-all containing every API endpoint not mapped to a named document.

All documents and the Swagger UI are served from {yourdomain}/umbraco/openapi.

OpenAPI documents and the Swagger UI are disabled in production environments by default to avoid exposing API structure on public-facing websites. See Route and availability to override this.

Adding your own OpenAPI documents

Your custom APIs will appear in the default document unless you register them elsewhere or exclude them explicitly. If you want more control over where your APIs show up, you can add your own OpenAPI documents.

Building a custom backoffice API? See Custom Backoffice API for the AddBackOfficeOpenApiDocument builder, which wires up backoffice authentication and Umbraco's conventions for you.

The ASP.NET Core OpenAPI documentation covers OpenAPI configuration in depth.

To add a custom OpenAPI document, use AddOpenApi in Program.cs or a composer, then AddOpenApiDocumentToUi to add it to the OpenAPI UI dropdown.

The following code sample creates an OpenAPI document called "My API v1" that includes only controllers from a specific namespace:

The ShouldInclude predicate above filters by namespace. See Controlling which endpoints appear in your document for other approaches.

Ensure your API controllers are in the matching namespace:

Controlling which endpoints appear in your document

Set ShouldInclude on your document to control which endpoints appear in it. The example above filters by namespace.

You can also filter by the [MapToApi] attribute. This is how the AddBackOfficeOpenApiDocument builder filters by default. The attribute is optional — controllers only need it when your ShouldInclude predicate checks for it.

Excluding endpoints from the default document

When you create a custom OpenAPI document, your controllers may still appear in the default document. There are two ways to exclude them:

  • Apply [MapToApi("your-document-name")] to the controller. This both maps the controller to your named document (when your ShouldInclude filters by [MapToApi]) and removes it from the default.

  • Apply [ExcludeFromDefaultOpenApiDocument] to the controller. Use this when your custom document does not filter by [MapToApi]:

Customizing operation and schema IDs

Operation and schema IDs control how endpoints and types are named in your OpenAPI document. Custom IDs can make consumers' generated code more readable, especially when those consumers generate API contracts from your OpenAPI documents.

Operation IDs

There are three ways to customize operation IDs:

Use Umbraco's transformer. UmbracoOperationIdTransformer generates operation IDs following Umbraco's naming conventions:

Use the Name property on the route attribute. The most direct way to set an explicit ID for a specific endpoint:

Write a custom transformer. For complete control over generation:

Apply the custom transformer when registering the document:

Schema IDs

Schema IDs are configured via the CreateSchemaReferenceId property when adding your OpenAPI document. Umbraco applies custom schema IDs to its own APIs; the same pattern works for yours:

CreateSchemaReferenceId is called for all types in the document, including .NET Framework types. The namespace check ensures you only customize schema IDs for your own types. The fallback uses ASP.NET Core's default schema ID generator. If you want to use Umbraco's naming conventions (which adds a "Model" suffix), you can use UmbracoSchemaIdGenerator.Generate(jsonTypeInfo.Type) from the Umbraco.Cms.Api.Common.OpenApi namespace.

Returning null from CreateSchemaReferenceId will inline the schema instead of creating a reference. This can be useful for types that don't need to be reused.

Versioning your APIs

Separate OpenAPI documents per version

A common use case for custom OpenAPI documents is maintaining multiple versions of the same API, with one OpenAPI document per version.

The following code sample creates two OpenAPI documents: "My API v1" and "My API v2". Each document uses ShouldInclude to filter controllers by their namespace:

With these OpenAPI documents in place, ensure your API controllers are in their respective namespaces:

See the V2 controller example

The V2 controller is in a separate namespace (My.Custom.Api.V2) and uses ApiVersion("2.0"). You can also extend the view model with additional properties:

Reading the requested version

The Umbraco APIs rely on having the requested API version as part of the URL. If you prefer a different versioning scheme for your own APIs, you can set up alternatives while still preserving the functionality of the Umbraco API.

The following code sample illustrates how you can use a custom header to pass the requested API version to your own APIs.

Route and UI configuration

Route and availability

You can customize the OpenAPI route, UI prefix, and production availability using UmbracoOpenApiOptions in Program.cs. Use PostConfigure to override the defaults:

Using an alternative UI

The built-in UI is based on Swagger UI. If you prefer an alternative UI, you can disable the default UI while keeping the OpenAPI documents available:

With the default UI disabled, you can register your preferred UI yourself. Refer to the UI's documentation for setup details.

Last updated

Was this helpful?