Sitecore 10.3 and 10.4: A C# Tip for Custom APIs

Sitecore 10.3 and 10.4 have introduced several enhancements aimed at improving the overall experience when working with the platform. In the article below, let’s explore a bit about building custom APIs.

Using Sitecore.Services.Client to Simplify Custom APIs

Sitecore.Services.Client (SSC) has received significant improvements in Sitecore 10.3 and 10.4, allowing for more efficient creation and management of custom APIs. Here’s how you can leverage this to create a custom API endpoint:

  1. Create a Controller: First, define a new API controller in your Sitecore project
using System.Web.Http;
using Sitecore.Services.Core;
using Sitecore.Services.Infrastructure.Web.Http;

[ServicesController]
public class CustomDataController : ServicesApiController
{
    [HttpGet]
    public IHttpActionResult GetData()
    {
        var data = new { Message = "Hello, Sitecore!" };
        return Request.CreateResponse(HttpStatusCode.OK, data);
    }
}
  1. Register the Route: Ensure that your route is registered. Update the Initialize pipeline in your project
  1. Configure Permissions: Adjust the access permissions in the web.config to allow your API to be accessible.xml
<configuration>
    <location path="sitecore/api/ssc">
        <system.web>
            <authorization>
                <allow users="*" />
            </authorization>
        </system.web>
    </location>
</configuration>

Creating endpoints is often part of daily tasks when working with Sitecore, so I hope this helps you.