How to Restrict Umbraco Content Delivery API by Doctype

12/06/2023 umbraco headless

In this post I will discuss how to restrict your Umbraco Content Delivery API to only return items of specific doctypes

As per my previous post, Umbraco 12 will support content delivery APIs out of the box with Umbraco. Follow the instructions in that post on how to get set up and test your APIs.

At this point, all content will be returned by the API. This may be fine for your scenario, but there may be occasions we want to restrict which content is returned. For example, by Doctype.

For some sample content to test this, I have installed the Starter Kit package:

dotnet add "MyProject" package Umbraco.TheStarterKit

This gives us some content in our CMS:

Now we have content, lets have a look whats returned from Content Delivery API. Per my previous post, I am using Postman to test this. If we scroll through the response object, we will see all the pages in the CMS returned as JSON:

Restricting by Doctype

To disallow specific content doctypes from the Content Delivery API, we can add the following configuration to the appSettings.json:

"DeliveryApi": {
        "Enabled": true,
        "DisallowedContentTypeAliases": [{{list the doctypes you want to hide}}]
    }

Say we want to ensure the API doesn't return any Products, we can add the "product" and "products" doctype alias to the list:

"DeliveryApi": {
        "Enabled": true,
        "DisallowedContentTypeAliases": ["product", "products"]
    }

Now, when we call our API in postman again, we see a much smaller list returned. We can see a much smaller response object. 2.35KB vs 11.27KB previously. As we get more content in the CMS, the performance savings could be significant by ensuring only the necessary items are returned: 

Interestingly, the count is still the full amount we got previously (30), even though less content items are returned. I am unsure if this is intended by Umbraco team or a bug, but something to be aware of incase you use that total value in your integration.

 

... and there we have our Content Delivery API returning only the content items we want it to!

 

Related posts: