(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to create an Azure Static Web App with an API in Azure Functions

João Barros 25 de November de 2026 4 min read

This tutorial shows how to create an Azure Static Web App integrated with an API in Azure Functions, useful for hosting a static site with a serverless backend. You learn why automatic linking is useful, how to deploy with the CLI and how to troubleshoot common errors during the process.

Prerequisites

  • Active Azure subscription and permissions to create resources.
  • Installed: Azure CLI, Git, Node.js (includes npm) and VS Code (optional).
  • GitHub account (or Git repository) for the automatic deployment workflow.
  • Basic knowledge of HTML/JavaScript and Azure Functions.

Step 1: Prepare the static app and the API

Create a folder with the simple frontend application and an API in Azure Functions (JavaScript). The minimal structure is: /app for the frontend and /api for the Functions.

mkdir my-static-webapp && cd my-static-webapp
mkdir app api
# frontend simples
cat > app/index.html <<EOF
<!doctype html>
<html><head><meta charset="utf-8"><title>Static Web App</title></head>
<body>
  <h1>Olá Azure Static Web Apps</h1>
  <div id="msg">A carregar...</div>
  <script>fetch('/api/hello').then(r => r.json()).then(j => document.getElementById('msg').textContent = j.message);</script>
</body>
</html>
EOF

# criar Azure Function simples (JavaScript)
npm init -y
npm i -D azure-functions-core-tools@4 || true
func init api --javascript --worker-runtime node --skip-templates
cd api && func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous"
# ajustar para devolver JSON
sed -i "s/context.res = { status: 200, body: \"Hello\" }/context.res = { body: { message: 'Olá do backend' } }/" HttpExample/index.js || true
cd ..

Step 2: Initialize a Git repository and push to GitHub

Azure Static Web Apps performs automatic deployment via GitHub Actions. Initialize a repository, commit and push to a remote repo on GitHub.

git init
git add .
git commit -m "Initial Static Web App + Azure Functions"
# cria um repo no GitHub manualmente e depois:
git remote add origin git@github.com:SEU-USER/SEU-REPO.git
git branch -M main
git push -u origin main

Step 3: Create the Static Web App in Azure via CLI

Use the Azure CLI to create the Static Web App resource. The CLI creates the GitHub Actions workflow that handles the build and automatic deployment.

# login e seleccionar subscrição (se necessário)
az login
az account set --subscription "SEU-NOME-DA-SUBSCRIPCAO"

# criar o Static Web App (requer GitHub para deploy automático)
az staticwebapp create \
  --name myStaticAppExample \
  --resource-group myResourceGroup \
  --location westeurope \
  --source https://github.com/SEU-USER/SEU-REPO \
  --branch main \
  --app-location "app" \
  --api-location "api" \
  --output json

Step 4: Understand the workflow and troubleshoot common errors

After creation, check GitHub Actions in the repository. The workflow will build the frontend and the API. Common errors: incorrect paths in app-location/api-location, incompatible Node version, or missing dependencies in the api folder.

# verificar o estado do workflow no GitHub Actions (exemplo de comando local para ver logs já depois do push)
# ou ver no portal GitHub: Actions > workflow criado por Azure Static Web Apps

# se a Function der erro por dependências, instala na pasta api:
cd api
npm install
cd ..

Step 5: Configure authentication and simple rules (optional)

Static Web Apps includes support for authentication (production providers like GitHub, Microsoft, Google, etc.) via the /.auth/login route. To test, create a staticwebapp.config.json file for simple routes and authorization.

cat > app/staticwebapp.config.json <<JSON
{
  "routes": [
    { "route": "/api/*", "allowedRoles": ["anonymous"] }
  ]
}
JSON

git add . && git commit -m "Add config" && git push

Verify the result

After the GitHub Actions workflow completes, go to the Static Web App URL provided by Azure. You should see the static page and the message from the API: "Olá do backend". If you get a 404 on the /api call, confirm the app-location and api-location paths in the workflow and that the Functions were built without errors.

Conclusion

You now have an Azure Static Web App with an API in Azure Functions, automatic deployment via GitHub Actions and basic route configuration. Next steps: add authentication, use Managed Identities to access other Azure resources, or modify the build for frameworks like React/Vue. Tip: if the workflow fails, check the logs in GitHub Actions — they are the clearest source for diagnosis.