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/openapi-apim-integrate-visual-studio.md
+51-50Lines changed: 51 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
title: Create serverless APIs using Azure Functions and API Management
3
3
description: Use Visual Studio to create an HTTP triggered function that uses an OpenAPI definition (formerly swagger) to expose a serverless API using API Management.
4
4
ms.topic: tutorial
5
-
ms.date: 08/04/2024
5
+
ms.date: 03/04/2026
6
6
---
7
7
8
8
# Create serverless APIs in Visual Studio using Azure Functions and API Management integration
@@ -12,21 +12,23 @@ REST APIs are often described using an OpenAPI definition (formerly known as Swa
12
12
In this tutorial, you learn how to:
13
13
14
14
> [!div class="checklist"]
15
+
>
15
16
> * Create the code project in Visual Studio
16
17
> * Install the OpenAPI extension
17
18
> * Add an HTTP trigger endpoint, which includes OpenAPI definitions
18
19
> * Test function APIs locally using built-in OpenAPI functionality
19
-
> * Publish project to a function app in Azure
20
-
> * Enable API Management integration
20
+
> * Publish project to a function app in Azure
21
+
> * Enable API Management integration
21
22
> * Download the OpenAPI definition file
22
23
23
24
The serverless function you create provides an API that lets you determine whether an emergency repair on a wind turbine is cost-effective. Since you create both the function app and API Management instance in a consumption tier, your cost for completing this tutorial is minimal.
24
25
25
26
## Prerequisites
26
27
27
-
+[Visual Studio 2022](https://azure.microsoft.com/downloads/). Make sure you select the **Azure development** workload during installation.
28
+
*[Visual Studio](https://azure.microsoft.com/products/visual-studio/). Make sure you select the **Azure development** workload during installation.
28
29
29
-
+ An active [Azure subscription](../guides/developer/azure-developer-guide.md#understanding-accounts-subscriptions-and-billing), create a [free account](https://azure.microsoft.com/pricing/purchase-options/azure-account?cid=msft_learn) before you begin.
30
+
* An active [Azure subscription](/azure/developer/#understanding-accounts-subscriptions-and-billing
31
+
), create a [free account](https://azure.microsoft.com/pricing/purchase-options/azure-account?cid=msft_learn) before you begin.
30
32
31
33
## Create the code project
32
34
@@ -36,56 +38,55 @@ The Azure Functions project template in Visual Studio creates a project that you
36
38
37
39
1. In **Create a new project**, enter *functions* in the search box, choose the **Azure Functions** template, and then select **Next**.
38
40
39
-
1. In **Configure your new project**, enter a **Project name** for your project like `TurbineRepair`, and then select **Create**.
41
+
1. In **Configure your new project**, enter a **Project name** for your project like `TurbineRepair`, and then select **Next**.
40
42
41
-
1. For the **Create a new Azure Functions application** settings, select one of these options for **Functions worker**, where the option you choose depends on your chosen process model:
43
+
1. For the **Additional information** settings, select one of these options for **Functions worker**, where the option you choose depends on your chosen process model:
**.NET 8.0 Isolated (Long Term Support)**: Your C# functions run in the isolated worker model, which is recommended. For more information, see the [isolated worker model guide](dotnet-isolated-process-guide.md).
46
-
46
+
47
+
**.NET 8.0 Isolated (Long Term Support)**: Your C# functions run in the isolated worker model, which is recommended. For more information, see the [isolated worker model guide](dotnet-isolated-process-guide.md).
**.NET 8.0 (Long Term Support)**: Your C# functions run in the [in-process model](functions-dotnet-class-library.md), which will be retired. To learn more, see the [in-process model guide](functions-dotnet-class-library.md).
52
-
53
+
**.NET 8.0 (Long Term Support)**: Your C# functions run in the [in-process model](functions-dotnet-class-library.md), which will be retired. To learn more, see the [in-process model guide](functions-dotnet-class-library.md).
54
+
53
55
---
54
56
55
-
1. For the rest of the options, use the values in the following table:
57
+
1. For the rest of the options, use the values in the following table:
|**Function template**|**Empty**| This creates a project without a trigger, which gives you more control over the name of the HTTP triggered function when you add it later. |
60
62
|**Use Azurite for runtime storage account (AzureWebJobsStorage)**|**Selected**| You can use the emulator for local development of HTTP trigger functions. Because a function app in Azure requires a storage account, one is assigned or created when you publish your project to Azure. |
61
-
|**Authorization level**|**Function**| When running in Azure, clients must provide a key when accessing the endpoint. For more information, see [Authorization level](functions-bindings-http-webhook-trigger.md#http-auth). |
62
63
63
-
1. Select **Create** to create the function project.
64
+
1. Select **Create** to create the function project.
64
65
65
-
Next, you update the project by installing the OpenAPI extension for Azure Functions, which enables the discoverability of API endpoints in your app.
66
+
Next, you update the project by installing the OpenAPI extension for Azure Functions, which enables the discoverability of API endpoints in your app.
66
67
67
68
## Install the OpenAPI extension
68
69
69
-
To install the OpenAPI extension:
70
+
To install the OpenAPI extension:
70
71
71
-
1. From the **Tools** menu, select **NuGet Package Manager** > **Package Manager Console**.
72
+
1. From the **Tools** menu, select **NuGet Package Manager** > **Package Manager Console**.
72
73
73
-
1. In the console, run the following [Install-Package](/nuget/tools/ps-ref-install-package) command to install the OpenAPI extension:
74
+
1. In the console, run the following [Install-Package](/nuget/reference/ps-reference/ps-ref-install-package) command to install the OpenAPI extension:
You might need to update the [specific version](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.Extensions.OpenApi), based on your version of .NET.
You might need to update the [specific version](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.OpenApi), based on your version of .NET.
88
-
89
+
89
90
---
90
91
91
92
Now, you can add your HTTP endpoint function.
@@ -94,7 +95,7 @@ Now, you can add your HTTP endpoint function.
94
95
95
96
In a C# class library, the bindings used by the functionare defined by applying attributes in the code. To create a functionwith an HTTP trigger:
96
97
97
-
1. In **Solution Explorer**, right-click your project node and select**Add**>**New Azure Function**.
98
+
1. In **Solution Explorer**, right-click your project node and select**Add**>**New Azure Function**.
98
99
99
100
1. Enter **Turbine.cs**for the class, and thenselect**Add**.
100
101
@@ -111,15 +112,15 @@ The function uses an HTTP trigger that takes two parameters:
111
112
|*hours*| The estimated time to make a turbine repair, up to the nearest whole hour. |
112
113
|*capacity*| The capacity of the turbine, in kilowatts. |
113
114
114
-
The functionthen calculates how much a repair costs, and how much revenue the turbine could make in a 24-hour period. Parameters are supplied either in the query string or in the payload of a POST request.
115
+
The functionthen calculates how much a repair costs, and how much revenue the turbine could make in a 24-hour period. Parameters are supplied either in the query string or in the payload of a POST request.
115
116
116
117
In the Turbine.cs project file, replace the contents of the class generated from the HTTP trigger template with the following code, which depends on your process model:
@@ -135,12 +136,12 @@ When you run the function, the OpenAPI endpoints make it easy to try out the fun
135
136
136
137
1. In your browser, open the RenderSwaggerUI endpoint, which should look like `http://localhost:7071/api/swagger/ui`. A page is rendered, based on your OpenAPI definitions.
137
138
138
-
1. Select **POST** > **Try it out**, enter values for `hours` and `capacity` either as query parameters or in the JSON request body, and select **Execute**.
139
+
1. Select **POST** > **Try it out**, enter values for `hours` and `capacity` either as query parameters or in the JSON request body, and select **Execute**.
139
140
140
141
:::image type="content" source="media/openapi-apim-integrate-vs/swagger-ui-post.png" alt-text="Swagger UI for testing the TurbineRepair API":::
141
142
142
143
1. When you enter integer values like 6 for `hours` and 2500 for `capacity`, you get a JSON response that looks like the following example:
143
-
144
+
144
145
:::image type="content" source="media/openapi-apim-integrate-vs/swagger-ui-response.png" alt-text="Response JSON data from the TurbineRepair function.":::
145
146
146
147
Now you have a function that determines the cost-effectiveness of emergency repairs. Next, you publish your project and API definitions to Azure.
@@ -149,9 +150,9 @@ Now you have a function that determines the cost-effectiveness of emergency repa
149
150
150
151
Before you can publish your project, you must have a function app in your Azure subscription. Visual Studio publishing creates a function app the first time you publish your project. It can also create an API Management instance that integrates with your function app to expose the TurbineRepair API.
151
152
152
-
1. In **Solution Explorer**, right-click the project and select **Publish** and in **Target**, select **Azure** then **Next**.
153
+
1. In **Solution Explorer**, right-click the project and select **Publish** and in **Target**. Select **Azure** and then **Next**.
153
154
154
-
1. For the **Specific target**, choose **Azure Function App (Windows)** to create a function app that runs on Windows, then select **Next**.
155
+
1. For the **Specific target**, choose **Azure Function App** to create a function app that runs on Windows. Then select **Next**.
155
156
156
157
1. In **Function Instance**, choose **+ Create a new Azure Function...**.
157
158
@@ -162,19 +163,19 @@ Before you can publish your project, you must have a function app in your Azure
| **Name** | Globally unique name | Name that uniquely identifies your new function app. Accept this name or enter a new name. Valid characters are: `a-z`, `0-9`, and `-`. |
165
-
| **Subscription** | Your subscription | The Azure subscription to use. Accept this subscription or select a new one from the drop-down list. |
166
-
| **[Resource group](../azure-resource-manager/management/overview.md)** | Name of your resource group | The resource group in which to create your function app. Select an existing resource group from the drop-down list or choose **New** to create a new resource group.|
167
-
| **[Plan Type](functions-scale.md)** | Consumption | When you publish your project to a function app that runs in a [Consumption plan](consumption-plan.md), you pay only for executions of your functions app. Other hosting plans incur higher costs. |
166
+
| **Subscription** | Your subscription | The Azure subscription to use. Accept this subscription, or select a new one from the drop-down list. |
167
+
| **[Resource group](../azure-resource-manager/management/overview.md)** | Name of your resource group | The resource group in which to create your function app. Select an existing resource group from the drop-down list, or choose **New** to create a new resource group.|
168
+
| **[Plan Type](functions-scale.md)** | Flex Consumption | When you publish your project to a function app that runs in a [Flex Consumption plan](flex-consumption-plan.md), you pay only for executions of your functions app. Other hosting plans incur higher costs. |
168
169
| **Location** | Location of the service | Choose a **Location** in a [region](https://azure.microsoft.com/regions/) near you or other services your functions access. |
169
-
| **[Azure Storage](storage-considerations.md)** | General-purpose storage account | An Azure Storage account is required by the Functions runtime. Select **New** to configure a general-purpose storage account. You can also choose an existing account that meets the [storage account requirements](storage-considerations.md#storage-account-requirements). |
170
+
| **[Azure Storage](storage-considerations.md)** | General-purpose storage account | The Functions runtime requires an Azure Storage account. Select **New** to configure a general-purpose storage account. You can also choose an existing account that meets the [storage account requirements](storage-considerations.md#storage-account-requirements). |
170
171
171
172
:::image type="content" source="media/openapi-apim-integrate-vs/create-function-app-with-storage.png" alt-text="Create a new function app in Azure with Storage":::
172
173
173
-
1. Select **Create** to create a function app and its related resources in Azure. Status of resource creation is shown in the lower left of the window.
174
+
1. Select **Create** to create a function app and its related resources in Azure. Status of resource creation is shown in the lower left of the window.
174
175
175
-
1. Back in **Functions instance**, make sure that **Run from package file** is checked. Your function app is deployed using [Zip Deploy](functions-deployment-technologies.md#zip-deploy) with [Run-From-Package](run-functions-from-deployment-package.md) mode enabled. This deployment method is recommended for your functions project, since it results in better performance.
176
+
1. Back in **Functions instance**, make sure that **Run from package file** is checked. Your function app is deployed using [Zip Deploy](functions-deployment-technologies.md#zip-deploy) with [Run-From-Package](run-functions-from-deployment-package.md) mode enabled. This deployment method is recommended for your functions project, since it results in better performance.
176
177
177
-
1. Select **Next**, and in **API Management** page, also choose **+ Create an API Management API**.
178
+
1. Select **Next**, and in the **API Management** page, also choose **+ Create new** or **Create a new instance**.
178
179
179
180
1. Create an **API in API Management** by using values in the following table:
180
181
@@ -191,15 +192,15 @@ Before you can publish your project, you must have a function app in your Azure
191
192
192
193
1. Select **Finish** and after the publish profile creation process completes, select **Close**.
193
194
194
-
1. Verify the Publish page now says **Ready to publish**, and then select **Publish** to deploy the package containing your project files to your new function app in Azure.
195
+
1. Verify the Publish page now says **Ready to publish**, and then select **Publish** to deploy the package containing your project files to your new function app in Azure.
195
196
196
-
After the deployment completes, the root URL of the function app in Azure is shown in the **Publish** tab.
197
+
After the deployment completes, the root URL of the function app in Azure is shown in the **Publish** tab.
197
198
198
199
## Get the function access key
199
200
200
-
1. In the **Publish** tab, select the ellipses (**...**) next to **Hosting** and select **Open in Azure portal**. The function app you created is opened in the Azure portal in your default browser.
201
+
1. In the **Publish** tab, select the ellipses (**...**) next to **Hosting** and select **Open in Azure portal**. The function app you created is opened in the Azure portal in your default browser.
201
202
202
-
1. Under **Functions** on the **Overview page**, select > **Turbine** then select **Function keys**.
203
+
1. Under **Functions** on the **Overview page**, select > **Turbine** then select **Function keys**.
203
204
204
205
:::image type="content" source="media/openapi-apim-integrate-vs/get-function-keys.png" alt-text="Get an access key for the TurbineRepair function":::
205
206
@@ -212,8 +213,8 @@ Before you can publish your project, you must have a function app in your Azure
212
213
1. If the function app isn't already connected to the new API Management instance, selectit under **API Management**, select**API**>**OpenAPI Document on Azure Functions**, make sure **Import functions** is checked, and select**Link API**. Make sure that only **TurbineRepair** is selected for import and then**Select**.
213
214
214
215
1. Select **Go to API Management** at the top of the page, and in the API Management instance, expand **APIs**.
215
-
216
-
1. Under **APIs**>**All APIs**, select**OpenAPI Document on Azure Functions**>**POST Run**, then under **Inbound processing**select**Add policy**>**Set query parameters**.
216
+
217
+
1. Under **APIs**>**All APIs**, select**OpenAPI Document on Azure Functions**>**POST Run**. Then under **Inbound processing**select**Add policy**>**Set query parameters**.
217
218
218
219
1. Below **Inbound processing**, in**Set query parameters**, type`code`for**Name**, select **+Value**, pastein the copied functionkey, and select**Save**. API Management includes the functionkey when it passes calls through to the functionendpoint.
219
220
@@ -223,7 +224,7 @@ Now that the function key is set, you can call the `turbine` API endpoint to ver
223
224
224
225
## Verify the API in Azure
225
226
226
-
1. In the API, selectthe**Test** tab and then**POST Run**, enter the following code in the **Request body**>**Raw**, and select**Send**:
227
+
1. In the API, selectthe**Test** tab, and then**POST Run**. Enter the following code in the **Request body**>**Raw**, and select**Send**:
227
228
228
229
```json
229
230
{
@@ -234,24 +235,24 @@ Now that the function key is set, you can call the `turbine` API endpoint to ver
234
235
235
236
:::image type="content" source="media/openapi-apim-integrate-vs/api-management-test-function-api.png" alt-text="OpenAPI test page in the API Management API":::
236
237
237
-
As before, you can also provide the same values as query parameters.
238
+
As before, you can also provide the same values as query parameters.
238
239
239
240
1. Select **Send**, and then view the **HTTP response** to verify the same results are returned from the API.
240
241
241
242
## Download the OpenAPI definition
242
243
243
244
If your API works as expected, you can download the OpenAPI definition for the new hosted APIs from API Management.
244
245
245
-
1. 1. Under **APIs**, select**OpenAPI Document on Azure Functions**, selectthe ellipses (**...**), and select**Export**.
246
-
246
+
1. Under **APIs**, select**OpenAPI Document on Azure Functions**, selectthe ellipses (**...**), and select**Export**.
2. Choose the means of API export, including OpenAPI files in various formats. You can also [export APIs from Azure API Management to the Power Platform](../api-management/export-api-power-platform.md).
250
+
2. Choose the means of API export, including OpenAPI files in various formats. You can also [export APIs from Azure API Management to the Power Platform](../api-management/export-api-power-platform.md).
250
251
251
252
## Clean up resources
252
253
253
254
In the preceding steps, you created Azure resources in a resource group. If you don't expect to need these resources in the future, you can delete them by deleting the resource group.
254
-
255
+
255
256
From the Azure portal menu or **Home** page, select **Resource groups**. Then, on the **Resource groups** page, select the group you created.
256
257
257
258
On the **myResourceGroup** page, make sure that the listed resources are the ones you want to delete.
0 commit comments