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/azure-functions/functions-create-serverless-api.md
+55-45Lines changed: 55 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,62 +3,69 @@ title: Customize an HTTP endpoint in Azure Functions
3
3
description: Learn how to customize an HTTP trigger endpoint in Azure Functions
4
4
author: mattchenderson
5
5
ms.topic: conceptual
6
-
ms.date: 05/04/2017
6
+
ms.date: 04/27/2020
7
7
ms.author: mahender
8
8
ms.custom: mvc
9
9
---
10
10
11
11
# Customize an HTTP endpoint in Azure Functions
12
12
13
-
In this article, you learn how Azure Functions allows you to build highly scalable APIs. Azure Functions comes with a collection of built-in HTTP triggers and bindings, which make it easy to author an endpoint in a variety of languages, including Node.js, C#, and more. In this article, you will customize an HTTP trigger to handle specific actions in your API design. You will also prepare for growing your API by integrating it with Azure Functions Proxies and setting up mock APIs. All of this is accomplished on top of the Functions serverless compute environment, so you don't have to worry about scaling resources - you can just focus on your API logic.
13
+
In this article, you learn how Azure Functions allows you to build highly scalable APIs. Azure Functions comes with a collection of built-in HTTP triggers and bindings, which make it easy to author an endpoint in a variety of languages, including Node.js, C#, and more. In this article, you'll customize an HTTP trigger to handle specific actions in your API design. You'll also prepare for growing your API by integrating it with Azure Functions Proxies and setting up mock APIs. These tasks are accomplished on top of the Functions serverless compute environment, so you don't have to worry about scaling resources - you can just focus on your API logic.
The resulting function will be used for the rest of this article.
20
20
21
-
###Sign in to Azure
21
+
## Sign in to Azure
22
22
23
-
Open the Azure portal. To do this, sign in to [https://portal.azure.com](https://portal.azure.com) with your Azure account.
23
+
Sign in to the [Azure portal](https://portal.azure.com) with your Azure account.
24
24
25
25
## Customize your HTTP function
26
26
27
-
By default, your HTTP-triggered function is configured to accept any HTTP method. There is also a default URL of the form `http://<yourapp>.azurewebsites.net/api/<funcname>?code=<functionkey>`. If you followed the quickstart, then `<funcname>` probably looks something like "HttpTriggerJS1". In this section, you will modify the function to respond only to GET requests against`/api/hello` route instead.
27
+
By default, your HTTP trigger function is configured to accept any HTTP method. You can also use the default URL, `http://<yourapp>.azurewebsites.net/api/<funcname>?code=<functionkey>`. In this section, you modify the function to respond only to GET requests with`/api/hello`.
28
28
29
-
1. Navigate to your function in the Azure portal. Select **Integrate** in the left navigation.
29
+
1. Navigate to your function in the Azure portal. Select **Integration** in the left menu, and then select **HTTP (req)** under **Trigger**.
30
30
31
-

31
+
:::image type="content" source="./media/functions-create-serverless-api/customizing-http.png" alt-text="Customizing an HTTP function":::
32
32
33
-
1. Use the HTTP trigger settings as specified in the table.
33
+
1. Use the HTTP trigger settings as specified in the following table.
34
34
35
35
| Field | Sample value | Description |
36
36
|---|---|---|
37
-
| Allowed HTTP methods | Selected methods | Determines what HTTP methods may be used to invoke this function |
38
-
| Selected HTTP methods | GET | Allows only selected HTTP methods to be used to invoke this function |
39
37
| Route template | /hello | Determines what route is used to invoke this function |
40
-
| Authorization Level | Anonymous | Optional: Makes your function accessible without an API key |
38
+
| Authorization level | Anonymous | Optional: Makes your function accessible without an API key |
39
+
| Selected HTTP methods | GET | Allows only selected HTTP methods to be used to invoke this function |
41
40
42
-
> [!NOTE]
43
-
> Note that you did not include the `/api` base path prefix in the route template, as this is handled by a global setting.
41
+
You didn't include the `/api` base path prefix in the route template, because it's handled by a global setting.
44
42
45
-
1.Click**Save**.
43
+
1.Select**Save**.
46
44
47
-
You can learn more about customizing HTTP functions in[Azure Functions HTTP bindings](https://docs.microsoft.com/azure/azure-functions/functions-bindings-http-webhook).
45
+
For more information about customizing HTTP functions, see[Azure Functions HTTP bindings](https://docs.microsoft.com/azure/azure-functions/functions-bindings-http-webhook).
48
46
49
47
### Test your API
50
48
51
-
Next, test your function to see it working with the new API surface.
52
-
1. Navigate back to the development page by clicking on the function's name in the left navigation.
53
-
1. Click **Get function URL** and copy the URL. You should see that it uses the `/api/hello` route now.
54
-
1. Copy the URL into a new browser tab or your preferred REST client. Browsers will use GET by default.
55
-
1. Add parameters to the query string in your URL e.g. `/api/hello/?name=John`
56
-
1. Hit 'Enter' to confirm that it is working. You should see the response "*Hello John*"
57
-
1. You can also try calling the endpoint with another HTTP method to confirm that the function is not executed. For this, you will need to use a REST client, such as cURL, Postman, or Fiddler.
49
+
Next, test your function to see how it works with the new API surface:
50
+
1. On the function page, select **Code + Test** from the left menu.
51
+
52
+
1. Select **Get function URL** from the top menu and copy the URL. Confirm that it now uses the `/api/hello` path.
53
+
54
+
1. Copy the URL into a new browser tab or your preferred REST client.
55
+
56
+
Browsers use GET by default.
57
+
58
+
1. Add parameters to the query string in your URL.
59
+
60
+
For example, `/api/hello/?name=John`.
61
+
62
+
1. Press Enter to confirm that it's working. You should see the response, "*Hello John*."
63
+
64
+
1. You can also try calling the endpoint with another HTTP method to confirm that the function isn't executed. To do so, use a REST client, such as cURL, Postman, or Fiddler.
58
65
59
66
## Proxies overview
60
67
61
-
In the next section, you will surface your API through a proxy. Azure Functions Proxies allows you to forward requests to other resources. You define an HTTP endpoint just like with HTTP trigger, but instead of writing code to execute when that endpoint is called, you provide a URL to a remote implementation. This allows you to compose multiple API sources into a single API surface which is easy for clients to consume. This is particularly useful if you wish to build your API as microservices.
68
+
In the next section, you'll surface your API through a proxy. Azure Functions Proxies allows you to forward requests to other resources. You define an HTTP endpoint just like with HTTP trigger. However, instead of writing code to execute when that endpoint is called, you provide a URL to a remote implementation. Doing so allows you to compose multiple API sources into a single API surface, which is easy for clients to consume, which is useful if you wish to build your API as microservices.
62
69
63
70
A proxy can point to any HTTP resource, such as:
64
71
- Azure Functions
@@ -70,52 +77,55 @@ To learn more about proxies, see [Working with Azure Functions Proxies].
70
77
71
78
## Create your first proxy
72
79
73
-
In this section, you will create a new proxy which serves as a frontend to your overall API.
80
+
In this section, you create a new proxy, which serves as a frontend to your overall API.
74
81
75
82
### Setting up the frontend environment
76
83
77
-
Repeat the steps to [Create a function app](https://docs.microsoft.com/azure/azure-functions/functions-create-first-azure-function#create-a-function-app) to create a new function app in which you will create your proxy. This new app's URL will serve as the frontend for our API, and the function app you were previously editing will serve as a backend.
84
+
Repeat the steps to [Create a function app](https://docs.microsoft.com/azure/azure-functions/functions-create-first-azure-function#create-a-function-app) to create a new function app in which you will create your proxy. This new app's URL serves as the frontend for our API, and the function app you were previously editing serves as a backend.
78
85
79
86
1. Navigate to your new frontend function app in the portal.
80
87
1. Select **Platform Features** and choose **Application Settings**.
81
-
1. Scroll down to **Application settings** where key/value pairs are stored and create a new setting with key "HELLO_HOST". Set its value to the host of your backend function app, such as `<YourBackendApp>.azurewebsites.net`. This is part of the URL that you copied earlier when testing your HTTP function. You'll reference this setting in the configuration later.
88
+
1. Scroll down to **Application settings**, where key/value pairs are stored, and create a new setting with the key `HELLO_HOST`. Set its value to the host of your backend function app, such as `<YourBackendApp>.azurewebsites.net`. This value is part of the URL that you copied earlier when testing your HTTP function. You'll reference this setting in the configuration later.
82
89
83
90
> [!NOTE]
84
91
> App settings are recommended for the host configuration to prevent a hard-coded environment dependency for the proxy. Using app settings means that you can move the proxy configuration between environments, and the environment-specific app settings will be applied.
85
92
86
-
1.Click**Save**.
93
+
1.Select**Save**.
87
94
88
95
### Creating a proxy on the frontend
89
96
90
-
1. Navigate back to your frontend function app in the portal.
91
-
1. In the left-hand navigation, click the plus sign '+' next to "Proxies".
92
-

93
-
1. Use proxy settings as specified in the table.
97
+
1. Navigate back to your front-end function app in the portal.
98
+
99
+
1. In the left-hand menu, select **Proxies**, and then select **Add**.
100
+
101
+
1. On the **New Proxy** page, use the settings in the following table, and then select **Create**.
94
102
95
103
| Field | Sample value | Description |
96
104
|---|---|---|
97
105
| Name | HelloProxy | A friendly name used only for management |
98
106
| Route template | /api/remotehello | Determines what route is used to invoke this proxy |
99
107
| Backend URL | https://%HELLO_HOST%/api/hello | Specifies the endpoint to which the request should be proxied |
108
+
100
109
101
-
1. Note that Proxies does not provide the `/api` base path prefix, and this must be included in the route template.
102
-
1. The `%HELLO_HOST%` syntax will reference the app setting you created earlier. The resolved URL will point to your original function.
103
-
1. Click **Create**.
104
-
1. You can try out your new proxy by copying the Proxy URL and testing it in the browser or with your favorite HTTP client.
:::image type="content" source="./media/functions-create-serverless-api/creating-proxy.png" alt-text="Creating a proxy":::
111
+
112
+
Azure Functions Proxies doesn't provide the `/api` base path prefix, which must be included in the route template. The `%HELLO_HOST%` syntax references the app setting you created earlier. The resolved URL will point to your original function.
113
+
114
+
1. Try out your new proxy by copying the proxy URL and testing it in the browser or with your favorite HTTP client:
Next, you will use a proxy to create a mock API for your solution. This allows client development to progress, without needing the backend fully implemented. Later in development, you could create a new function app which supports this logic and redirect your proxy to it.
122
+
Next, you'll use a proxy to create a mock API for your solution. This proxy allows client development to progress, without needing the backend fully implemented. Later in development, you can create a new function app, which supports this logic and redirect your proxy to it.
113
123
114
-
To create this mock API, we will create a new proxy, this time using the [App Service Editor](https://github.com/projectkudu/kudu/wiki/App-Service-Editor). To get started, navigate to your function app in the portal. Select **Platform features** and under **Development Tools** find **App Service Editor**. Clicking this will open the App Service Editor in a new tab.
124
+
To create this mock API, we'll create a new proxy, this time using the [App Service Editor](https://github.com/projectkudu/kudu/wiki/App-Service-Editor). To get started, navigate to your function app in the portal. Select **Platform features**, and under **Development Tools** find **App Service Editor**. The App Service Editor opens in a new tab.
115
125
116
-
Select `proxies.json` in the left navigation. This is the file which stores the configuration for all of your proxies. If you use one of the [Functions deployment methods](https://docs.microsoft.com/azure/azure-functions/functions-continuous-deployment), this is the file you will maintain in source control. To learn more about this file, see [Proxies advanced configuration](https://docs.microsoft.com/azure/azure-functions/functions-proxies#advanced-configuration).
126
+
Select `proxies.json` in the left navigation. This file stores the configuration for all of your proxies. If you use one of the [Functions deployment methods](https://docs.microsoft.com/azure/azure-functions/functions-continuous-deployment), you maintain this file in source control. To learn more about this file, see [Proxies advanced configuration](https://docs.microsoft.com/azure/azure-functions/functions-proxies#advanced-configuration).
117
127
118
-
If you've followed along so far, your proxies.json should look like the following:
128
+
If you've followed along so far, your proxies.json should look like as follows:
119
129
120
130
```json
121
131
{
@@ -131,7 +141,7 @@ If you've followed along so far, your proxies.json should look like the followin
131
141
}
132
142
```
133
143
134
-
Next you'll add your mock API. Replace your proxies.json file with the following:
144
+
Next, you'll add your mock API. Replace your proxies.json file with the following code:
135
145
136
146
```json
137
147
{
@@ -167,7 +177,7 @@ Next you'll add your mock API. Replace your proxies.json file with the following
167
177
}
168
178
```
169
179
170
-
This adds a new proxy, "GetUserByName", without the backendUri property. Instead of calling another resource, it modifies the default response from Proxies using a response override. Request and response overrides can also be used in conjunction with a backend URL. This is particularly useful when proxying to a legacy system, where you might need to modify headers, query parameters, etc. To learn more about request and response overrides, see [Modifying requests and responses in Proxies](https://docs.microsoft.com/azure/azure-functions/functions-proxies).
180
+
This code adds a new proxy, `GetUserByName`, without the `backendUri` property. Instead of calling another resource, it modifies the default response from Proxies using a response override. Request and response overrides can also be used in conjunction with a backend URL. This technique is particularly useful when proxying to a legacy system, where you might need to modify headers, query parameters, and so on. To learn more about request and response overrides, see [Modifying requests and responses in Proxies](https://docs.microsoft.com/azure/azure-functions/functions-proxies).
171
181
172
182
Test your mock API by calling the `<YourProxyApp>.azurewebsites.net/api/users/{username}` endpoint using a browser or your favorite REST client. Be sure to replace _{username}_ with a string value representing a username.
0 commit comments