One of the relatively new services that Azure offers, or at least one that is gaining traction, is the Azure Static Web Pages. This service allows us to host what we know as Single-Page Applications (SPA) in a simple and effective manner. This kind of applications are those focused on the Front-End and are created with JavaScript frameworks/libraries (like Angular, React, Vue, etc) or even with Blazor. And most of the time consuming Back-End services to consume data or some other functionality on the server side.
In this article, we are going to review how we can implement Back-End services in our Azure Static Web Apps with Azure Functions.
Requirements
This time we will primarily use the command line, more specifically PowerShell together with the Command-Line Interface (CLI) tools for Static Web Apps and Azure Functions
You can find more information about in the following links:
- Static Web Apps CLI (SWA CLI) – https://github.com/Azure/static-web-apps-cli or https://azure.github.io/static-web-apps-cli/
- Azure Functions Core Tools – https://github.com/Azure/azure-functions-core-tools
Front-End
We'll start creating a vanilla Single-Page Application (SPA), with the purpose of focusing on the Static Web App/Azure Functions subject, but you can create your desired Front-End app.
To start, we need to create a working folder and create a simple HTML file
mkdir swa-vanilla
cd swa-vanilla
New-Item -Path . -Name "index.html"
* Remember we are working with PowerShell that is why we use New-Item command, but you can create the HTML file as you feel more comfortable
And the content for the index.html file is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vanilla JavaScript App</title>
</head>
<body>
<main>
<h1>Vanilla JavaScript App</h1>
<p>Loading content from the API: <b id="apiresponse">...</b></p>
</main>
<script>
(async function () {
let response = await fetch(`/api/message`);
let data = await response.text();
console.log(data);
document.querySelector('#apiresponse').textContent = data;
}())
</script>
</body>
</html>
As you can see, in the JavaScript code we are making a call to an API called 'message'. And the reference is even relative, it means, we are expecting to provide the service in the same domain. Let's proceed to create it.
API with Azure Functions
By design, the API needs to be created using Azure Functions. In this case, we'll create an Azure Function and after a simple endpoint called 'message'. Here is where we'll use our first CLI or toolset, the “Azure Functions Core Tools“.
Inside our working folder
func init 'api' --worker-runtime dotnet-isolated --target-framework net6.0
cd api
func new --name 'message' --template "HTTP trigger" --authlevel "anonymous"
In the first command we created the Azure Function project named 'api' with an isolated execution model in .NET 6 (you can create the project with your own configurations). And a folder with the same name is created, where after we create the 'message' HttpTrigger function.









Join the conversation! Your thoughts help the community grow.