Skip to content

Commit 4483d97

Browse files
update to Functions v4
1 parent 08cf401 commit 4483d97

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

articles/static-web-apps/add-api.md

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ services: static-web-apps
55
author: craigshoemaker
66
ms.service: azure-static-web-apps
77
ms.topic: how-to
8-
ms.date: 08/29/2022
8+
ms.date: 11/25/2024
99
ms.author: cshoe
1010
ms.custom:
1111
---
1212

1313
# Add an API to Azure Static Web Apps with Azure Functions
1414

15-
You can add serverless APIs to Azure Static Web Apps that are powered by Azure Functions. This article demonstrates how to add and deploy an API to an Azure Static Web Apps site.
15+
You can add serverless APIs to Azure Static Web Apps powered by Azure Functions. This article demonstrates how to add and deploy an API to an Azure Static Web Apps site.
1616

1717
> [!NOTE]
1818
> The functions provided by default in Static Web Apps are pre-configured to provide secure API endpoints and only support HTTP-triggered functions. See [API support with Azure Functions](apis-functions.md) for information on how they differ from standalone Azure Functions apps.
@@ -34,10 +34,12 @@ You can add serverless APIs to Azure Static Web Apps that are powered by Azure F
3434

3535
Before adding an API, create and deploy a frontend application to Azure Static Web Apps by following the [Building your first static site with Azure Static Web Apps](getting-started.md) quickstart.
3636

37-
Once you have a frontend application deployed to Azure Static Web Apps, [clone your app repository](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository). For example, to clone using the `git` command line:
37+
Once you have a frontend application deployed to Azure Static Web Apps, [clone your app repository](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository). For example, you can clone your repository using the `git` command line.
38+
39+
Before you run the following command, make sure to replace `<YOUR_GITHUB_USERNAME>` with your GitHub username.
3840

3941
```bash
40-
git clone https://github.com/my-username/my-first-static-web-app
42+
git clone https://github.com/<YOUR_GITHUB_USERNAME>/my-first-static-web-app
4143
```
4244

4345
In Visual Studio Code, open the root of your app's repository. The folder structure contains the source for your frontend app and the Static Web Apps GitHub workflow in _.github/workflows_ folder.
@@ -63,12 +65,12 @@ You create an Azure Functions project for your static web app's API. By default,
6365
| Prompt | Value |
6466
| --- | --- |
6567
| Select a language | **JavaScript** |
66-
| Select a programming model | **V3** |
68+
| Select a programming model | **V4** |
6769
| Provide a function name | **message** |
6870

6971
> [!TIP]
7072
> You can learn more about the differences between programming models in the [Azure Functions developer guide](/azure/azure-functions/functions-reference)
71-
73+
7274
An Azure Functions project is generated with an HTTP triggered function. Your app now has a project structure similar to the following example.
7375

7476
```Files
@@ -77,24 +79,31 @@ You create an Azure Functions project for your static web app's API. By default,
7779
│ └── azure-static-web-apps-<DEFAULT_HOSTNAME>.yml
7880
7981
├── api
80-
│ ├── message
81-
│ │ ├── function.json
82-
│ │ └── index.js
82+
├── └──src
83+
│ │ │ functions
84+
│ │ │ └── message.js
85+
│ │ └── index.js
86+
│ ├── .funcignore
8387
│ ├── host.json
8488
│ ├── local.settings.json
89+
│ ├── package-lock.json
8590
│ └── package.json
8691
87-
└── (folders and files from your static web app)
92+
└── (...plus other folders and files from your static web app)
8893
```
8994
90-
1. Next, change the `message` function to return a message to the frontend. Update the function in _api/message/index.js_ with the following code.
95+
1. Next, change the `message` function to return a message to the frontend. Update the function in _src/functions/message.js_ with the following code.
9196
9297
```javascript
93-
module.exports = async function (context, req) {
94-
context.res.json({
95-
text: "Hello from the API"
96-
});
97-
};
98+
const { app } = require('@azure/functions');
99+
100+
app.http('message', {
101+
methods: ['GET', 'POST'],
102+
authLevel: 'anonymous',
103+
handler: async (request, context) => {
104+
return { body: `Hello, from the API!` };
105+
}
106+
});
98107
```
99108
100109
> [!TIP]
@@ -319,11 +328,11 @@ Run the frontend app and API together by starting the app with the Static Web Ap
319328

320329
---
321330

322-
1. Windows Firewall may prompt to request that the Azure Functions runtime can access the Internet. Select **Allow**.
323-
324-
2. When the CLI processes start, access your app at `http://localhost:4280/`. Notice how the page calls the API and displays its output, `Hello from the API`.
331+
1. Windows Firewall might prompt to request that the Azure Functions runtime can access the Internet. Select **Allow**.
332+
333+
1. When the CLI processes start, access your app at `http://localhost:4280/`. Notice how the page calls the API and displays its output, `Hello from the API`.
325334

326-
3. To stop the CLI, type <kbd>Ctrl + C</kbd>.
335+
1. To stop the CLI, type <kbd>Ctrl + C</kbd>.
327336

328337
## Add API location to workflow
329338

@@ -341,7 +350,8 @@ Before you can deploy your app to Azure, update your repository's GitHub Actions
341350
output_location: "" # Built app content directory - optional
342351
###### End of Repository/Build Configurations ######
343352
```
344-
**Note**: The above values of `api_location` ,`app_location`,`output_location` are for no framework and these value changes based on framework.
353+
354+
**Note**: The above values of `api_location` ,`app_location`,`output_location` are for no framework and these values change based on your framework.
345355

346356
1. Save the file.
347357

0 commit comments

Comments
 (0)