Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • M. Umar 2 posts 72 karma points
    Mar 09, 2024 @ 07:21
    M. Umar
    0

    Headless Content ApI Custom Route

    Hello there, hope you are doing great.

    I am using Umbraco Headless Content Api in a project with nextjs.

    Let's say I have different kinds of Products in Umbraco backoffice. I want to return these products using Content Api from a custom route ?

    Is even it possible ? How can I do it ? Can anyone suggest a tutorial for it ?

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Mar 13, 2024 @ 20:50
    Marc Goodson
    0

    Hi M. Umar

    There is a pretty good introduction to the content delivery API here:

    https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api

    And you should be able to construct a custom query to filter your products by different type or properties...

    /umbraco/delivery/api/v2/content

    Accepts filter options

    But in terms of having this content returned via a custom route, then I don't think the delivery API is setup to allow the Url to be changed...

    ... but if it is important to return the content via a custom route then you could create your own UmbracoApiController, route it how you need, but you'd need to implement the getting of the content from the Umbraco published cache and returning the response.

    https://docs.umbraco.com/umbraco-cms/v/12.latest/reference/routing/umbraco-api-controllers/routing#routing

    https://docs.umbraco.com/umbraco-cms/reference/routing/umbraco-api-controllers#using-mvc-attribute-routing-in-umbraco-web-api-controllers

    Regards

    Marc

  • M. Umar 2 posts 72 karma points
    Mar 14, 2024 @ 01:58
    M. Umar
    0

    Ok. Thanks for your input. I got you about the routing part. But how can I implement the getting of the content from the Umbraco published cache and returning the response, for content delievry content api specifically ?

    is there any tutorial for this ?

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Mar 18, 2024 @ 16:02
    Marc Goodson
    0

    Hi M. Umar

    If I understand your question correctly, they are two different things

    Content Delivery API - a new thing added to Umbraco that allows you to request things from the Umbraco Published Cache via fixed routing endpoints:

    https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api

    And 'Creating a custom API endpoint' here has nothing to do with the Content Delivery Api, so you can use this approach to create your own endpoint at any Url, once you have mapped your custom route to an Api Controller, inside that controller you can inject IPublishedContentQuery to make requests to the Umbraco published cache and build your custom models to return on the API.

    Here is an example of injecting IPublishedContentQuery into a controller:

    https://docs.umbraco.com/umbraco-cms/implementation/services#accessing-core-services-and-helpers-in-a-controller

    So you probably need to decide if you want to have a specific Url or happy to use the Content Deliver Api Url format to pull back your content from Umbraco, and this will decide which approach you go for.

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft