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
- Open
Visual Studio
- Go
to File > New > Project
- Select
"Azure Functions" project
- Click
Next
Step 2: Configure the Project
- Project
Name: SitecoreAzureFunctionApp
- Location:
Choose a folder
- Click
Next
Step 3: Configure Azure Functions Settings
- Functions
Worker: .NET 8.0 Isolated (Long Term Support)
- Function:
Http trigger
- Authorization
level: Function or Anonymous
- Storage:
Use Azurite for runtime storage account (AzureWebJobsStorage)
- Click
Create
Project Structure
Step 4: Add packages for Sitecore Headless Integration
- Right-click
on the project → Manage NuGet Packages
- Add:
- Microsoft.Extensions.Http
- Newtonsoft.Json
(optional)
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
- Open
the local.settings.json file
- 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"
}
}
Step 6: Add HttpClient to Program.cs
var builder = FunctionsApplication.CreateBuilder(args);
builder.ConfigureFunctionsWebApplication();
builder.Services
.AddApplicationInsightsTelemetryWorkerService()
.ConfigureFunctionsApplicationInsights()
.AddHttpClient();
builder.Build().Run();
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;
}
}
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
Post a Comment