Building a Headless Integration Between Sitecore and Azure Functions

In today’s digital landscape, headless CMS architectures have become a game-changer for delivering personalized, dynamic, and scalable content experiences across multiple channels. Sitecore, with its powerful headless capabilities via Sitecore JSS and Layout Service, provides a flexible content management backbone. But what if we want to extend Sitecore’s capabilities beyond the platform, leveraging cloud-native serverless technologies?

Here comes Azure Functions, a serverless compute service that lets us run code on-demand without managing infrastructure. Combining Sitecore with Azure Functions enables developers to build scalable, decoupled integrations that enhance content delivery, personalization, or business logic in a lightweight, cost-effective way.

Benefits to Integrate Sitecore with Azure Functions

  • Scalability: Azure Functions automatically scale with your workload.
  • Cost-efficiency: Pay only for compute time, no idle server costs.
  • Extensibility: Execute custom business logic or third-party integrations outside of Sitecore.
  • Decoupling: Maintain a clean separation between CMS and serverless logic.
  • Modern Architecture: Align with cloud-native and microservices best practices.

In this post, we will walk through how to build a headless integration between Sitecore and Azure Functions using .NET.

Prerequisites

  • Running Sitecore instance with JSS enabled
  • Visual Studio 2022 with .NET 8 SDK and Azure Development workload installed
  • Sitecore API key (To access Layout Service)

Steps to Build a Headless Integration Between Sitecore and Azure Functions

Step 1: Create New Azure Functions Project in Visual Studio

  1. Open Visual Studio
  2. Go to File > New > Project
  3. Select "Azure Functions" project
  4. Click Next

A screenshot of a computer

AI-generated content may be incorrect.

Step 2: Configure the Project

  1. Project Name: SitecoreAzureFunctionApp
  2. Location: Choose a folder
  3. Click Next

A screenshot of a computer

AI-generated content may be incorrect.

Step 3: Configure Azure Functions Settings

  1. Functions Worker: .NET 8.0 Isolated (Long Term Support)
  2. Function: Http trigger
  3. Authorization level: Function or Anonymous
  4. Storage: Use Azurite for runtime storage account (AzureWebJobsStorage)
  5. Click Create

A screenshot of a computer

AI-generated content may be incorrect.

Project Structure

A screenshot of a computer

AI-generated content may be incorrect.

Step 4: Add packages for Sitecore Headless Integration

  1. Right-click on the project → Manage NuGet Packages
  2. Add:
    • Microsoft.Extensions.Http
    • Newtonsoft.Json (optional)

A screenshot of a computer

AI-generated content may be incorrect.

Step 5: Store Sitecore host URL and API key securely in local.settings.json for local development

SitecoreHost is the running sitecore instance host name and SitecoreApiKey is the API key to access layout service, If the key does not exist then we can create the key at path

/sitecore/system/Settings/Services/API Keys à Add new key

  1. Open the local.settings.json file
  2. Set Environment Variables - Add SitecoreApiKey and SitecoreHost

{

  "IsEncrypted": false,

  "Values": {

    "AzureWebJobsStorage": "UseDevelopmentStorage=true",

    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",

    "SitecoreApiKey": "{DA214022-F337-4E99-B4FA-129140D58041}",

    "SitecoreHost": "https://sitecore103sc.dev.local"

  }

}

A screen shot of a computer code

AI-generated content may be incorrect.

Step 6: Add HttpClient to Program.cs

var builder = FunctionsApplication.CreateBuilder(args);

builder.ConfigureFunctionsWebApplication();

builder.Services

    .AddApplicationInsightsTelemetryWorkerService()

    .ConfigureFunctionsApplicationInsights()

    .AddHttpClient();

builder.Build().Run();

A screenshot of a computer program

AI-generated content may be incorrect.

Step 7: Access Sitecore Content via Azure Functions

Azure Functions act as an intermediary to fetch Sitecore content via the Layout Service

Example: Fetching Sitecore Item via Layout Service

Open the Function1.cs file and update with code below for accessing specified sitecore item path and read the json response

Access Environment Variables APiKey and host from settings

------------------------------------------------------------------------------------------------------------------------

public class Function1

{

    private readonly ILogger<Function1> _logger;

    private readonly HttpClient _httpClient;   

    public Function1(IHttpClientFactory factory, ILogger<Function1> logger)

    {

        _httpClient = factory.CreateClient();

        _logger = logger;

    }

    [Function("Function1")]

    public async Task<HttpResponseData> Run(

        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "items/{path}")] HttpRequestData req, string path)

    {

        var apiKey = Environment.GetEnvironmentVariable("SitecoreApiKey");

        var host = Environment.GetEnvironmentVariable("SitecoreHost");

        var url = $"{host}/sitecore/api/layout/render/jss?item=/{path}&sc_apikey={apiKey}";

        var sitecoreResponse = await _httpClient.GetAsync(url);

        var content = await sitecoreResponse.Content.ReadAsStringAsync();

        var response = req.CreateResponse(sitecoreResponse.StatusCode);

        await response.WriteStringAsync(content);

        return response;

    }

}

A screenshot of a computer program

AI-generated content may be incorrect.

Step 8: Start the Azure Function locally

Run the application using F5 key

Call the function via HTTP GET from browser or postman

http://localhost:7222/api/items/home

JSON response for the Sitecore item data

Integrating Sitecore with Azure Functions is a powerful pattern for building modern, scalable, and maintainable headless applications. Leveraging Azure’s serverless capabilities allows us to extend Sitecore flexibly without increasing platform complexity.

If you are ready to modernize your Sitecore architecture, then start experimenting with Azure Functions today.

We can use the Azure Functions for different use cases like to

  • -          Enrich Sitecore content with external data
  • -          Implement real-time personalization logic outside Sitecore
  • -          Integrate third-party APIs without impacting Sitecore performance
  • -          Build lightweight microservices that interact with Sitecore content

In Next blog we will learn more by taking any real time use case and how to Deploy this app to Azure and Publish the Azure Function.

Stay tuned!

 

 

 

 

Comments

Popular posts from this blog

Setting Up Sitecore Headless with Next.js in Disconnected Mode

Step-by-Step Guide: Sitecore Headless GraphQL Filtering, Sorting & Pagination