1
---
2
name: azure-static-web-apps
3
description: Helps create, configure, and deploy Azure Static Web Apps using the SWA CLI. Use when deploying static sites to Azure, setting up SWA local development, configuring staticwebapp.config.json, adding Azure Functions APIs to SWA, or setting up GitHub Actions CI/CD for Static Web Apps.
4
---
5
6
## Overview
7
8
Azure Static Web Apps (SWA) hosts static frontends with optional serverless API backends. The SWA CLI (`swa`) provides local development emulation and deployment capabilities.
9
10
**Key features:**
11
- Local emulator with API proxy and auth simulation
12
- Framework auto-detection and configuration
13
- Direct deployment to Azure
14
- Database connections support
15
16
**Config files:**
17
- `swa-cli.config.json` - CLI settings, **created by `swa init`** (never create manually)
18
- `staticwebapp.config.json` - Runtime config (routes, auth, headers, API runtime) - can be created manually
19
20
## General Instructions
21
22
### Installation
23
24
```bash
25
npm install -D @azure/static-web-apps-cli
26
```
27
28
Verify: `npx swa --version`
29
30
### Quick Start Workflow
31
32
**IMPORTANT: Always use `swa init` to create configuration files. Never manually create `swa-cli.config.json`.**
33
34
1. `swa init` - **Required first step** - auto-detects framework and creates `swa-cli.config.json`
35
2. `swa start` - Run local emulator at `http://localhost:4280`
36
3. `swa login` - Authenticate with Azure
37
4. `swa deploy` - Deploy to Azure
38
39
### Configuration Files
40
41
**swa-cli.config.json** - Created by `swa init`, do not create manually:
42
- Run `swa init` for interactive setup with framework detection
43
- Run `swa init --yes` to accept auto-detected defaults
44
- Edit the generated file only to customize settings after initialization
45
46
Example of generated config (for reference only):
47
```json
48
{
49
"$schema": "https://aka.ms/azure/static-web-apps-cli/schema",
50
"configurations": {
51
"app": {
52
"appLocation": ".",
53
"apiLocation": "api",
54
"outputLocation": "dist",
55
"appBuildCommand": "npm run build",
56
"run": "npm run dev",
57
"appDevserverUrl": "http://localhost:3000"
58
}
59
}
60
}
61
```
62
63
**staticwebapp.config.json** (in app source or output folder) - This file CAN be created manually for runtime configuration:
64
```json
65
{
66
"navigationFallback": {
67
"rewrite": "/index.html",
68
"exclude": ["/images/*", "/css/*"]
69
},
70
"routes": [
71
{ "route": "/api/*", "allowedRoles": ["authenticated"] }
72
],
73
"platform": {
74
"apiRuntime": "node:20"
75
}
76
}
77
```
78
79
## Command-line Reference
80
81
### swa login
82
83
Authenticate with Azure for deployment.
84
85
```bash
86
swa login # Interactive login
87
swa login --subscription-id <id> # Specific subscription
88
swa login --clear-credentials # Clear cached credentials
89
```
90
91
**Flags:** `--subscription-id, -S` | `--resource-group, -R` | `--tenant-id, -T` | `--client-id, -C` | `--client-secret, -CS` | `--app-name, -n`
92
93
### swa init
94
95
Configure a new SWA project based on an existing frontend and (optional) API. Detects frameworks automatically.
96
97
```bash
98
swa init # Interactive setup
99
swa init --yes # Accept defaults
100
```
101
102
### swa build
103
104
Build frontend and/or API.
105
106
```bash
107
swa build # Build using config
108
swa build --auto # Auto-detect and build
109
swa build myApp # Build specific configuration
110
```
111
112
**Flags:** `--app-location, -a` | `--api-location, -i` | `--output-location, -O` | `--app-build-command, -A` | `--api-build-command, -I`
113
114
### swa start
115
116
Start local development emulator.
117
118
```bash
119
swa start # Serve from outputLocation
120
swa start ./dist # Serve specific folder
121
swa start http://localhost:3000 # Proxy to dev server
122
swa start ./dist --api-location ./api # With API folder
123
swa start http://localhost:3000 --run "npm start" # Auto-start dev server
124
```
125
126
**Common framework ports:**
127
| Framework | Port |
128
|-----------|------|
129
| React/Vue/Next.js | 3000 |
130
| Angular | 4200 |
131
| Vite | 5173 |
132
133
**Key flags:**
134
- `--port, -p` - Emulator port (default: 4280)
135
- `--api-location, -i` - API folder path
136
- `--api-port, -j` - API port (default: 7071)
137
- `--run, -r` - Command to start dev server
138
- `--open, -o` - Open browser automatically
139
- `--ssl, -s` - Enable HTTPS
140
141
### swa deploy
142
143
Deploy to Azure Static Web Apps.
144
145
```bash
146
swa deploy # Deploy using config
147
swa deploy ./dist # Deploy specific folder
148
swa deploy --env production # Deploy to production
149
swa deploy --deployment-token <TOKEN> # Use deployment token
150
swa deploy --dry-run # Preview without deploying
151
```
152
153
**Get deployment token:**
154
- Azure Portal: Static Web App → Overview → Manage deployment token
155
- CLI: `swa deploy --print-token`
156
- Environment variable: `SWA_CLI_DEPLOYMENT_TOKEN`
157
158
**Key flags:**
159
- `--env` - Target environment (`preview` or `production`)
160
- `--deployment-token, -d` - Deployment token
161
- `--app-name, -n` - Azure SWA resource name
162
163
### swa db
164
165
Initialize database connections.
166
167
```bash
168
swa db init --database-type mssql
169
swa db init --database-type postgresql
170
swa db init --database-type cosmosdb_nosql
171
```
172
173
## Scenarios
174
175
### Create SWA from Existing Frontend and Backend
176
177
**Always run `swa init` before `swa start` or `swa deploy`. Do not manually create `swa-cli.config.json`.**
178
179
```bash
180
# 1. Install CLI
181
npm install -D @azure/static-web-apps-cli
182
183
# 2. Initialize - REQUIRED: creates swa-cli.config.json with auto-detected settings
184
npx swa init # Interactive mode
185
# OR
186
npx swa init --yes # Accept auto-detected defaults
187
188
# 3. Build application (if needed)
189
npm run build
190
191
# 4. Test locally (uses settings from swa-cli.config.json)
192
npx swa start
193
194
# 5. Deploy
195
npx swa login
196
npx swa deploy --env production
197
```
198
199
### Add Azure Functions Backend
200
201
1. **Create API folder:**
202
```bash
203
mkdir api && cd api
204
func init --worker-runtime node --model V4
205
func new --name message --template "HTTP trigger"
206
```
207
208
2. **Example function** (`api/src/functions/message.js`):
209
```javascript
210
const { app } = require('@azure/functions');
211
212
app.http('message', {
213
methods: ['GET', 'POST'],
214
authLevel: 'anonymous',
215
handler: async (request) => {
216
const name = request.query.get('name') || 'World';
217
return { jsonBody: { message: `Hello, ${name}!` } };
218
}
219
});
220
```
221
222
3. **Set API runtime** in `staticwebapp.config.json`:
223
```json
224
{
225
"platform": { "apiRuntime": "node:20" }
226
}
227
```
228
229
4. **Update CLI config** in `swa-cli.config.json`:
230
```json
231
{
232
"configurations": {
233
"app": { "apiLocation": "api" }
234
}
235
}
236
```
237
238
5. **Test locally:**
239
```bash
240
npx swa start ./dist --api-location ./api
241
# Access API at http://localhost:4280/api/message
242
```
243
244
**Supported API runtimes:** `node:18`, `node:20`, `node:22`, `dotnet:8.0`, `dotnet-isolated:8.0`, `python:3.10`, `python:3.11`
245
246
### Set Up GitHub Actions Deployment
247
248
1. **Create SWA resource** in Azure Portal or via Azure CLI
249
2. **Link GitHub repository** - workflow auto-generated, or create manually:
250
251
`.github/workflows/azure-static-web-apps.yml`:
252
```yaml
253
name: Azure Static Web Apps CI/CD
254
255
on:
256
push:
257
branches: [main]
258
pull_request:
259
types: [opened, synchronize, reopened, closed]
260
branches: [main]
261
262
jobs:
263
build_and_deploy:
264
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
265
runs-on: ubuntu-latest
266
steps:
267
- uses: actions/checkout@v3
268
- name: Build And Deploy
269
uses: Azure/static-web-apps-deploy@v1
270
with:
271
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
272
repo_token: ${{ secrets.GITHUB_TOKEN }}
273
action: upload
274
app_location: /
275
api_location: api
276
output_location: dist
277
278
close_pr:
279
if: github.event_name == 'pull_request' && github.event.action == 'closed'
280
runs-on: ubuntu-latest
281
steps:
282
- uses: Azure/static-web-apps-deploy@v1
283
with:
284
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
285
action: close
286
```
287
288
3. **Add secret:** Copy deployment token to repository secret `AZURE_STATIC_WEB_APPS_API_TOKEN`
289
290
**Workflow settings:**
291
- `app_location` - Frontend source path
292
- `api_location` - API source path
293
- `output_location` - Built output folder
294
- `skip_app_build: true` - Skip if pre-built
295
- `app_build_command` - Custom build command
296
297
## Troubleshooting
298
299
| Issue | Solution |
300
|-------|----------|
301
| 404 on client routes | Add `navigationFallback` with `rewrite: "/index.html"` to `staticwebapp.config.json` |
302
| API returns 404 | Verify `api` folder structure, ensure `platform.apiRuntime` is set, check function exports |
303
| Build output not found | Verify `output_location` matches actual build output directory |
304
| Auth not working locally | Use `/.auth/login/<provider>` to access auth emulator UI |
305
| CORS errors | APIs under `/api/*` are same-origin; external APIs need CORS headers |
306
| Deployment token expired | Regenerate in Azure Portal → Static Web App → Manage deployment token |
307
| Config not applied | Ensure `staticwebapp.config.json` is in `app_location` or `output_location` |
308
| Local API timeout | Default is 45 seconds; optimize function or check for blocking calls |
309
310
**Debug commands:**
311
```bash
312
swa start --verbose log # Verbose output
313
swa deploy --dry-run # Preview deployment
314
swa --print-config # Show resolved configuration
315
```
316