You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/static-web-apps/add-api.md
+97-90Lines changed: 97 additions & 90 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
---
2
2
title: Add an API to Azure Static Web Apps with Azure Functions
3
3
description: Get started with Azure Static Web Apps by adding a Serverless API to your static web app using Azure Functions.
4
-
services: azure-functions
4
+
services: static-web-apps
5
5
author: manekinekko
6
-
ms.service: azure-functions
6
+
ms.service: static-web-apps
7
7
ms.topic: how-to
8
8
ms.date: 05/08/2020
9
9
ms.author: wachegha
@@ -24,20 +24,20 @@ You can add serverless APIs to Azure Static Web Apps via integration with Azure
24
24
25
25
The following steps demonstrate how to create a new repository and clone the files to your computer.
26
26
27
-
1. Navigate to https://github.com/staticwebdev/vanilla-basic/generate to create a new repository
28
-
1. In the _Repository name_ box, enter **my-vanilla-api**
29
-
1. Click **Create repository from template**
27
+
1. Navigate to https://github.com/staticwebdev/vanilla-basic/generate to create a new repository.
28
+
1. In the _Repository name_ box, enter **my-vanilla-api**.
29
+
1. Click **Create repository from template**.
30
30
31
-
:::image type="content" source="media/add-api/create-repository.png" alt-text="Create a new repository from vanilla-basic":::
31
+
:::image type="content" source="media/add-api/create-repository.png" alt-text="Create a new repository from vanilla-basic":::
32
32
33
33
Once your project is created, you can use Visual Studio Code to clone the Git repository.
34
34
35
-
1. Press **F1** to open command in the Command Palette.
36
-
1. Paste the URL into the _Git: Clone_ prompt, and press **Enter**.
35
+
1. Press **F1** to open command in the Command Palette.
36
+
1. Paste the URL into the _Git: Clone_ prompt, and press **Enter**.
37
37
38
-
:::image type="content" source="media/add-api/vscode-git-0.png" alt-text="Clone a GitHub project using Visual Studio Code":::
38
+
:::image type="content" source="media/add-api/vscode-git-0.png" alt-text="Clone a GitHub project using Visual Studio Code":::
39
39
40
-
:::image type="content" source="media/add-api/github-clone-url.png" alt-text="Clone a GitHub project using Visual Studio Code":::
40
+
:::image type="content" source="media/add-api/github-clone-url.png" alt-text="Clone a GitHub project using Visual Studio Code":::
41
41
42
42
43
43
## Create your local project
@@ -46,8 +46,8 @@ In this section, you use Visual Studio Code to create a local Azure Functions pr
46
46
47
47
1. Inside the _my-vanilla-api_ project, create a sub-folder named **api**.
48
48
49
-
> [!NOTE]
50
-
> You can give this folder any name. This example uses `api`
49
+
> [!NOTE]
50
+
> You can give this folder any name. This example uses `api`.
51
51
52
52
2. Press **F1** to open the Command Palette
53
53
3. Type **Azure Functions: Create New Project...**
@@ -56,7 +56,7 @@ In this section, you use Visual Studio Code to create a local Azure Functions pr
56
56
6. Select the **api** folder as the directory for your project workspace
57
57
7. Choose **Select**
58
58
59
-
:::image type="content" source="media/add-api/create-azure-functions-vscode-1.png" alt-text="Create a new Azure Functions using Visual Studio Code":::
59
+
:::image type="content" source="media/add-api/create-azure-functions-vscode-1.png" alt-text="Create a new Azure Functions using Visual Studio Code":::
60
60
61
61
8. Provide the following information at the prompts:
62
62
@@ -72,61 +72,63 @@ In this section, you use Visual Studio Code to create a local Azure Functions pr
72
72
73
73
10. Your app should now have a project structure similar to this example.
74
74
75
-
```files
76
-
├── api
77
-
│ ├── GetMessage
78
-
│ │ ├── function.json
79
-
│ │ ├── index.js
80
-
│ │ └── sample.dat
81
-
│ ├── host.json
82
-
│ ├── local.settings.json
83
-
│ ├── package.json
84
-
│ └── proxies.json
85
-
├── index.html
86
-
├── readme.md
87
-
└── styles.css
88
-
```
75
+
```files
76
+
├── api
77
+
│ ├── GetMessage
78
+
│ │ ├── function.json
79
+
│ │ ├── index.js
80
+
│ │ └── sample.dat
81
+
│ ├── host.json
82
+
│ ├── local.settings.json
83
+
│ ├── package.json
84
+
│ └── proxies.json
85
+
├── index.html
86
+
├── readme.md
87
+
└── styles.css
88
+
```
89
89
90
90
11. Next, update the `GetMessage` function under _api/GetMessage/index.js_ with the following code.
91
91
92
-
```JavaScript
93
-
module.exports=asyncfunction (context, req) {
94
-
context.res= {
95
-
body: {
96
-
text:"Hello from the API"
97
-
}
98
-
};
99
-
};
100
-
```
92
+
```JavaScript
93
+
module.exports = async function (context, req) {
94
+
context.res = {
95
+
body: {
96
+
text: "Hello from the API"
97
+
}
98
+
};
99
+
};
100
+
```
101
101
102
102
12. Update the `GetMessage` configuration under `api/GetMessage/function.json` with the following settings.
103
103
104
-
```json
105
-
{
106
-
"bindings": [
107
-
{
108
-
"authLevel": "anonymous",
109
-
"type": "httpTrigger",
110
-
"direction": "in",
111
-
"name": "req",
112
-
"methods": [
113
-
"get"
114
-
],
115
-
"route": "message"
116
-
},
104
+
```json
117
105
{
118
-
"type": "http",
119
-
"direction": "out",
120
-
"name": "res"
106
+
"bindings": [
107
+
{
108
+
"authLevel": "anonymous",
109
+
"type": "httpTrigger",
110
+
"direction": "in",
111
+
"name": "req",
112
+
"methods": [
113
+
"get"
114
+
],
115
+
"route": "message"
116
+
},
117
+
{
118
+
"type": "http",
119
+
"direction": "out",
120
+
"name": "res"
121
+
}
122
+
]
121
123
}
122
-
]
123
-
}
124
-
```
124
+
```
125
+
125
126
With the above settings, the API endpoint is:
126
127
127
128
- Triggered with an HTTP request is made to the function
128
129
- Available to all requests regardless of authentication status
129
130
- Exposed via the _/api/message_ route
131
+
130
132
## Run the function locally
131
133
132
134
Visual Studio Code integrates with [Azure Functions Core Tools](https://docs.microsoft.com/azure/azure-functions/functions-run-local) to let you run this project on your local development computer before you publish to Azure.
@@ -137,15 +139,15 @@ Visual Studio Code integrates with [Azure Functions Core Tools](https://docs.mic
137
139
138
140
When the Core Tools are installed, your app starts in the _Terminal_ panel. As a part of the output, you can see the URL endpoint of your HTTP-triggered function running locally.
139
141
140
-
:::image type="content" source="media/add-api/create-azure-functions-vscode-2.png" alt-text="Create a new Azure Functions using Visual Studio Code":::
142
+
:::image type="content" source="media/add-api/create-azure-functions-vscode-2.png" alt-text="Create a new Azure Functions using Visual Studio Code":::
141
143
142
144
3. With Core Tools running, navigate to the following URL to execute a `GET` request.
143
145
144
146
<http://localhost:7071/api/message>
145
147
146
-
A response is returned, which looks like the following in the browser:
148
+
A response is returned, which looks like the following in the browser:
147
149
148
-
:::image type="content" source="media/add-api/create-azure-functions-vscode-3.png" alt-text="Create a new Azure Functions using Visual Studio Code":::
150
+
:::image type="content" source="media/add-api/create-azure-functions-vscode-3.png" alt-text="Create a new Azure Functions using Visual Studio Code":::
149
151
150
152
After you've verified that the function runs correctly, you can call the API from the JavaScript application.
151
153
@@ -155,44 +157,45 @@ After you've verified that the function runs correctly, you can call the API fro
155
157
156
158
157
159
#### Update files to access the API
160
+
158
161
1. Next, update the content of the _index.html_ file with the following code to fetch the text from the API function and display it on the screen:
With Core Tools running, use the [Live Server](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer) Visual Studio Code extension to serve the _index.html_ file and open it a in browser.
191
+
With Core Tools running, use the [Live Server](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer) Visual Studio Code extension to serve the _index.html_ file and open it a in browser.
189
192
190
193
2. Press **F1** and choose **Live Server: Open with Live Server**.
191
194
192
-
:::image type="content" source="media/add-api/create-azure-functions-vscode-4.png" alt-text="Create a new Azure Functions using Visual Studio Code":::
195
+
:::image type="content" source="media/add-api/create-azure-functions-vscode-4.png" alt-text="Create a new Azure Functions using Visual Studio Code":::
193
196
194
-
> [!NOTE]
195
-
> You can use other HTTP servers or proxies to serve the `index.html` file. Accessing the `index.html` from `file:///` will not work.
197
+
> [!NOTE]
198
+
> You can use other HTTP servers or proxies to serve the `index.html` file. Accessing the `index.html` from `file:///` will not work.
196
199
197
200
### Commit and push your changes to GitHub
198
201
@@ -226,11 +229,15 @@ Using Visual Studio Code, commit and push your changes to the remote git reposit
226
229
227
230
Next, add the following the build details.
228
231
229
-
1. Enter **./** for the _App location_
230
-
1. Enter **api** in the _Api location_ box
231
-
- This is the name of the API folder created in the previous step.
232
-
1. Clear the default value out of the _App artifact location_, leaving the box empty
233
-
1. Click **Review + create**
232
+
1. Enter **./** for the _App location_.
233
+
234
+
1. Enter **api** in the _Api location_ box.
235
+
236
+
This is the name of the API folder created in the previous step.
237
+
238
+
1. Clear the default value out of the _App artifact location_, leaving the box empty.
Copy file name to clipboardExpand all lines: articles/static-web-apps/apis.md
+2-10Lines changed: 2 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,16 +20,7 @@ Azure Static Web Apps provides serverless API endpoints via [Azure Functions](..
20
20
21
21
## Configuration
22
22
23
-
API endpoints are available to the web app through the _api_ route. While this route is fixed, you have control over the folder where you locate the associated Azure Functions app. You can change this location by editing the workflow YAML file located in your repository's _.github/workflows_ folder.
24
-
25
-
Set the the following location's value to the desired folder location for the API app in your repository.
26
-
27
-
```schema
28
-
jobs
29
-
└── steps
30
-
└── with
31
-
└── api_location
32
-
```
23
+
API endpoints are available to the web app through the _api_ route. While this route is fixed, you have control over the folder where you locate the associated Azure Functions app. You can change this location by [editing the workflow YAML file](github-actions-workflow.md#build-and-deploy) located in your repository's _.github/workflows_ folder.
33
24
34
25
## Constraints
35
26
@@ -38,6 +29,7 @@ Azure Static Web Apps provides an API through Azure Functions. The capabilities
38
29
- The API route prefix is must be _api_.
39
30
- Triggers and bindings are limited to [HTTP](../azure-functions/functions-bindings-http-webhook.md).
40
31
- All other [Azure Functions triggers and bindings](../azure-functions/functions-triggers-bindings.md#supported-bindings) except for output bindings are restricted.
32
+
- Logs are only available if you add [Application Insights](../azure-functions/functions-monitoring.md) to your Functions app.
0 commit comments