Custom Headless APIs
Custom Headless APIs have two primary use cases:
For a fully headless solution, you will need custom APIs to serve catalogs, categories, and products to your client application.
For any special requirements that are not covered by the out-of-the-box APIs.
The goal is to create endpoints that give you freedom of implementation while respecting our authentication.
Prerequisites
Out-of-the-box Headless API available (for example, by using our Standalone Template as a starting point).
Secrets are set up for your stores, and the URI whitelist is correctly configured in the API Access part of the administration interface. You can learn about it here.
Creating the API Controller
In your project, create a new Class and Inherit from
Ucommerce.Web.WebSite.Controllers.HeadlessControllerBase
This will inherit our authentication, restricting its usage to only authenticated clients.
Add a route to your controller. It can match the out-of-the-box route as follows:
In your controller's constructor, you can inject any components you may need in your implementation. For example,
Creating the method
Create and annotate a new method with the HTTP method type and route.
For help with return types and choosing the right implementation details, visit Microsoft's documentation.
Headless API authentication happens on a per-store basis. You can resolve the StoreId from the claim as follows:
Complete your implementation and return the results to your client.
Complete example
For reference, here is a full controller example that will return products for a given Category:
Related Articles
HeadlessLast updated