Discover how to customize the Content Delivery API's response for your custom property editors.
Out of the box, the Delivery API supports custom property editors, ensuring they are rendered alongside the built-in ones in Umbraco. However, if the output generated by your property editor isn't optimal for a headless context, you have the ability to customize the API response. This customization won't impact the Razor rendering, allowing you to tailor the Content Delivery API response according to your specific requirements.
In this article, we'll look into how you can work with the IDeliveryApiPropertyValueConverter
interface and implement custom property expansion for your custom property editors.
The examples in this article revolve around the fictional My.Custom.Picker
property editor. This property editor stores the key of a single content item and is backed by a property value converter.
We will not dive into the details of creating a custom property editor for Umbraco in this article. If you need guidance on that, please refer to the Creating a Property Editor and Property Value Converters articles.
To customize the output of a property value editor in the Delivery API, we need to opt-in by implementing the IDeliveryApiPropertyValueConverter
interface.
The code example below showcases the implementation of this interface in the property value converter for My.Custom.Picker
. Our focus will be on the methods provided by the IDeliveryApiPropertyValueConverter
, as they are responsible for customizing the Delivery API response.
Towards the end of the example, you will find the response models that we will be using.
The IsConverter()
and GetPropertyValueType()
methods are inherited from the PropertyValueConverterBase
class, which is covered in the Property Value Converters article.
The Implementation of the IDeliveryApiPropertyValueConverter
interface can be found in the following methods:
GetDeliveryApiPropertyCacheLevel()
: This method specifies the cache level used for our property representation in the Delivery API response.
GetDeliveryApiPropertyCacheLevelForExpansion()
: This method specifies the cache level used for our property representation in the Delivery API response when property expansion is applied.
GetDeliveryApiPropertyValueType()
: This method defines the value type of the custom property output for the Delivery API response.
ConvertIntermediateToDeliveryApiObject()
: This method converts the value from the property editor to the desired custom object in a headless context.
In the given example, the content key (Guid
value) is used when rendering with Razor. This is sufficient because Razor provides full access to the published content within the rendering context. In a headless context, we do not have the same access. To prevent subsequent round-trips to the server, we create a richer output model specifically for the Delivery API.
The following example request shows how our custom implementation is reflected in the resulting API response. In this case, our custom property editor is configured under the alias "pickedItem"
.
Request
Response
Property expansion allows us to conditionally add another level of detail to the Delivery API output. Usually, these additional details are "expensive" to retrieve (for example, requiring database access to populate). By applying property expansion, we provide the option for the caller of the API to opt-in explicitly to this "expensive" operation. From the caller's perspective, the alternative might be an even more expensive additional round-trip to the server.
In our example, property expansion is implemented within ConvertIntermediateToDeliveryApiObject()
. By considering the value of the expanding
parameter, we can modify the BuildDeliveryApiCustomPicker()
method as follows:
If the expanding
parameter is false
, the method returns the same shallow representation of the referenced content item as before. Otherwise, we retrieve the corresponding IPublishedContent
and construct our response object accordingly.
To see the expanded output in the API response, we need to add the expand
query parameter to our request. We can use either ?expand=properties[$all]
to expand all properties or ?expand=properties[pickedItem]
to expand the specific 'pickedItem'
property.
Request
Response
The itemDetails
property of the pickedItem
in the JSON response contains the additional details of the selected content item.