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
+90-30Lines changed: 90 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,58 +1,106 @@
1
1
---
2
-
title: Create serverless APIs in Visual Studio using Azure Functions and API Management
3
-
description: Use Visual Studio to create an HTTP triggered function along with an OpenAPI definition, which enables Azure API Management integration so that other apps and services can call your serverless function-based API.
2
+
title: Create serverless APIs using Azure Functions and API Management
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: 01/18/2023
5
+
ms.date: 08/04/2024
6
6
---
7
7
8
8
# Create serverless APIs in Visual Studio using Azure Functions and API Management integration
9
9
10
-
REST APIs are often described using an OpenAPI definition. This file contains information about operations in an API and how the request and response data for the API should be structured.
10
+
REST APIs are often described using an OpenAPI definition (formerly known as Swagger) file. This file contains information about operations in an API and how the request and response data for the API should be structured.
11
11
12
12
In this tutorial, you learn how to:
13
13
14
14
> [!div class="checklist"]
15
-
> * Create a serverless function project in Visual Studio
15
+
> * Create the code project in Visual Studio
16
+
> * Install the OpenAPI extension
17
+
> * Add an HTTP trigger endpoint, which includes OpenAPI definitions
16
18
> * Test function APIs locally using built-in OpenAPI functionality
17
-
> * Publish project to a function app in Azure with API Management integration
18
-
> *Get the access key for the function and set it in API Management
19
+
> * Publish project to a function app in Azure
20
+
> *Enable API Management integration
19
21
> * Download the OpenAPI definition file
20
22
21
-
The serverless function you create provides an API that lets you determine whether an emergency repair on a wind turbine is cost-effective. Because both the function app and API Management instance you create use consumption plans, your cost for completing this tutorial is minimal.
22
-
23
-
> [!NOTE]
24
-
> The OpenAPI and API Management integration featured in this article is currently only supported for [in-process](functions-dotnet-class-library.md) C# class library functions. [Isolated worker process](dotnet-isolated-process-guide.md) C# class library functions and all other language runtimes should instead [use Azure API Management integration from the portal](functions-openapi-definition.md).
23
+
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.
25
24
26
25
## Prerequisites
27
26
28
27
+[Visual Studio 2022](https://azure.microsoft.com/downloads/). Make sure you select the **Azure development** workload during installation.
29
28
30
29
+ An active [Azure subscription](../guides/developer/azure-developer-guide.md#understanding-accounts-subscriptions-and-billing), create a [free account](https://azure.microsoft.com/free/dotnet/) before you begin.
31
30
32
-
## Create a Functions project
31
+
## Create the code project
33
32
34
-
The Azure Functions project template in Visual Studio creates a project that you can publish to a function app in Azure. You'll also create an HTTP triggered function supports OpenAPI definition file (formerly Swagger file) generation.
33
+
The Azure Functions project template in Visual Studio creates a project that you can publish to a function app in Azure. You'll also create an HTTP triggered function from a template that supports OpenAPI definition file (formerly Swagger file) generation.
35
34
36
35
1. From the Visual Studio menu, select **File** > **New** > **Project**.
37
36
38
37
1. In **Create a new project**, enter *functions* in the search box, choose the **Azure Functions** template, and then select **Next**.
39
38
40
39
1. In **Configure your new project**, enter a **Project name** for your project like `TurbineRepair`, and then select **Create**.
41
40
42
-
1. For the **Create a new Azure Functions application** settings, use the values in the following table:
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:
**.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
+
---
54
+
55
+
1. For the rest of the options, use the values in the following table:
|**Functions worker**|**.NET 6**| This value creates a function project that runs in-process on version 4.x of the Azure Functions runtime, which is required for OpenAPI file generation. |
47
-
|**Function template**|**HTTP trigger with OpenAPI**| This value creates a function triggered by an HTTP request, with the ability to generate an OpenAPI definition file. |
59
+
|**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. |
48
60
|**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. |
49
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
+
1. Select **Create** to create the function project.
64
+
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
+
67
+
## Install the OpenAPI extension
68
+
69
+
To install the OpenAPI extension:
70
+
71
+
1. From the **Tools** menu, select **NuGet Package Manager** > **Package Manager Console**.
72
+
73
+
1. In the console, run the following [Install-Package](/nuget/tools/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
+
---
90
+
91
+
Now, you can add your HTTP endpoint function.
92
+
93
+
## Add an HTTP endpoint function
94
+
95
+
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
+
1. In **Solution Explorer**, right-click your project node and select**Add**>**New Azure Function**.
52
98
53
-
1.Select**Create**to create the function project and HTTP trigger function, with support for OpenAPI.
99
+
1. Enter**Turbine.cs**forthe class, and thenselect**Add**.
54
100
55
-
Visual Studio creates a project and class named `Function1` that contains boilerplate code for the HTTP trigger function type. Next, you replace this function template code with your own customized code.
101
+
1. Choose the **Http trigger** template, set**Authorization level** to **Function**, and thenselect**Add**. A Turbine.cs code file is added to your project that defines a new functionendpoint with an HTTP trigger.
102
+
103
+
Now you can replace the HTTP trigger template code with code that implements the Turbine functionendpoint, along with attributes that use OpenAPI to define endpoint.
56
104
57
105
## Update the function code
58
106
@@ -65,10 +113,18 @@ The function uses an HTTP trigger that takes two parameters:
65
113
66
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.
67
115
68
-
In the Turbine.cs project file, replace the contents of the generated class library code with the following code:
116
+
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:
This functioncode returns a message of `Yes` or `No` to indicate whether an emergency repair is cost-effective. It also returns the revenue opportunity that the turbine represents and the cost to fix the turbine.
73
129
74
130
## Run and verify the API locally
@@ -127,39 +183,43 @@ Before you can publish your project, you must have a function app in your Azure
127
183
| **API name** | TurbineRepair | Name for the API. |
128
184
| **Subscription name** | Your subscription | The Azure subscription to use. Accept this subscription or select a new one from the drop-down list. |
129
185
| **Resource group** | Name of your resource group | Select the same resource group as your function app from the drop-down list. |
130
-
|**API Management service**| New instance | Select **New** to create a new API Management instance in the serverless tier. |
186
+
| **API Management service** | New instance | Select **New** to create a new API Management instance in the same location in the serverless tier. Select **OK** to create the instance. |
131
187
132
188
:::image type="content" source="media/openapi-apim-integrate-vs/create-api-management-api.png" alt-text="Create API Management instance with API":::
133
189
134
190
1. Select **Create** to create the API Management instance with the TurbineRepair API from the function integration.
135
191
136
-
1. select **Finish**, verify the Publish page says **Ready to publish**, and then select **Publish** to deploy the package containing your project files to your new function app in Azure.
192
+
1. Select **Finish** and after the publish profile creation process completes, select **Close**.
193
+
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.
137
195
138
196
After the deployment completes, the root URL of the function app in Azure is shown in the **Publish** tab.
139
197
140
198
## Get the function access key
141
199
142
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.
143
201
144
-
1. Under **Functions**, select **Functions**> **TurbineRepair** then select **Function keys**.
202
+
1. Under **Functions** on the **Overview page**, select > **Turbine** then select **Function keys**.
145
203
146
204
:::image type="content" source="media/openapi-apim-integrate-vs/get-function-keys.png" alt-text="Get an access key for the TurbineRepair function":::
147
205
148
-
1. Under **Function keys**, select **default** and copy the **value**. You can now set this key in API Management so that it can access the function endpoint.
206
+
1. Under **Function keys**, select the *copy to clipboard* icon next to the **default** key. You can now set this key you copied in API Management so that it can access the function endpoint.
149
207
150
208
## Configure API Management
151
209
152
-
1. In the **Publish** tab, select the ellipses (**...**) next to **Hosting**and select **Open API in Azure portal**. The API Management instance you created is opened in the Azure portal in your default browser. This API Management instance is already linked to your function app.
210
+
1. In the function app page, expand **API** and select **API Management**.
153
211
154
-
1.Under **APIs**, select **OpenAPI Document on Azure Functions** > **POST Run**, then under**Inbound processing** select **Add policy**.
212
+
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**.
155
213
156
-
:::image type="content" source="media/openapi-apim-integrate-vs/apim-add-policy.png" alt-text="Add an inbound policy to the API":::
214
+
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**.
157
217
158
218
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.
159
219
160
220
:::image type="content" source="media/openapi-apim-integrate-vs/inbound-processing-rule.png" alt-text="Provide Function credentials to the API inbound processing rule":::
161
221
162
-
Now that the function key is set, you can call the API to verify that it works when hosted in Azure.
222
+
Now that the functionkey is set, you can call the `turbine`API endpoint to verify that it works when hosted in Azure.
163
223
164
224
## Verify the API in Azure
165
225
@@ -180,7 +240,7 @@ Now that the function key is set, you can call the API to verify that it works w
180
240
181
241
## Download the OpenAPI definition
182
242
183
-
If your API works as expected, you can download the OpenAPI definition.
243
+
If your API works as expected, you can download the OpenAPI definitionfor the new hosted APIs from API Management.
184
244
185
245
1. 1. Under **APIs**, select**OpenAPI Document on Azure Functions**, selectthe ellipses (**...**), and select**Export**.
186
246
@@ -200,7 +260,7 @@ Select **Delete resource group**, type the name of your group in the text box to
200
260
201
261
## Next steps
202
262
203
-
You've used Visual Studio 2022 to create a function that is self-documenting because of the [OpenAPI Extension](https://github.com/Azure/azure-functions-openapi-extension) and integrated with API Management. You can now refine the definition in API Management in the portal. You can also [learn more about API Management](../api-management/api-management-key-concepts.md).
263
+
You've used Visual Studio 2022 to create a functionthat's self-documenting because of the [OpenAPI Extension](https://github.com/Azure/azure-functions-openapi-extension) and integrated with API Management. You can now refine the definition in API Management in the portal. You can also [learn more about API Management](../api-management/api-management-key-concepts.md).
204
264
205
265
> [!div class="nextstepaction"]
206
266
> [Edit the OpenAPI definition in API Management](../api-management/edit-api.md)
0 commit comments