Skip to content

Commit b9b753f

Browse files
authored
Merge pull request #312663 from lootle1/FR37
Freshness Edit: Functions
2 parents 104d3ee + 3b8c139 commit b9b753f

4 files changed

Lines changed: 51 additions & 50 deletions

File tree

-7.77 KB
Loading
3.68 KB
Loading
-40.1 KB
Loading

articles/azure-functions/openapi-apim-integrate-visual-studio.md

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Create serverless APIs using Azure Functions and API Management
33
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.
44
ms.topic: tutorial
5-
ms.date: 08/04/2024
5+
ms.date: 03/04/2026
66
---
77

88
# 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
1212
In this tutorial, you learn how to:
1313

1414
> [!div class="checklist"]
15+
>
1516
> * Create the code project in Visual Studio
1617
> * Install the OpenAPI extension
1718
> * Add an HTTP trigger endpoint, which includes OpenAPI definitions
1819
> * 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
2122
> * Download the OpenAPI definition file
2223
2324
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.
2425

2526
## Prerequisites
2627

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.
2829

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.
3032

3133
## Create the code project
3234

@@ -36,56 +38,55 @@ The Azure Functions project template in Visual Studio creates a project that you
3638

3739
1. In **Create a new project**, enter *functions* in the search box, choose the **Azure Functions** template, and then select **Next**.
3840

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**.
4042

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:
4244

4345
### [Isolated worker model](#tab/isolated-process)
44-
45-
**.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).
48+
4749
### [In-process](#tab/in-process)
4850

4951
[!INCLUDE [functions-in-process-model-retirement-note](../../includes/functions-in-process-model-retirement-note.md)]
5052

51-
**.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+
5355
---
5456

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:
5658

5759
| Setting | Value | Description |
5860
| ------------ | ------- |----------------------------------------- |
5961
| **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. |
6062
| **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). |
6263

63-
1. Select **Create** to create the function project.
64+
1. Select **Create** to create the function project.
6465

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.
6667

6768
## Install the OpenAPI extension
6869

69-
To install the OpenAPI extension:
70+
To install the OpenAPI extension:
7071

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**.
7273

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:
7475

7576
### [Isolated worker model](#tab/isolated-process)
76-
77+
7778
```command
7879
NuGet\Install-Package Microsoft.Azure.Functions.Worker.Extensions.OpenApi -Version 1.5.1
7980
```
8081
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.
81-
82-
### [In-process model](#tab/in-process)
82+
83+
### [In-process model](#tab/in-process)
8384

8485
```command
8586
NuGet\Install-Package Microsoft.Azure.WebJobs.Extensions.OpenApi -Version 1.5.1
8687
```
8788
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+
8990
---
9091

9192
Now, you can add your HTTP endpoint function.
@@ -94,7 +95,7 @@ Now, you can add your HTTP endpoint function.
9495

9596
In a C# class library, the bindings used by the function are defined by applying attributes in the code. To create a function with an HTTP trigger:
9697

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**.
9899

99100
1. Enter **Turbine.cs** for the class, and then select **Add**.
100101

@@ -111,15 +112,15 @@ The function uses an HTTP trigger that takes two parameters:
111112
| *hours* | The estimated time to make a turbine repair, up to the nearest whole hour. |
112113
| *capacity* | The capacity of the turbine, in kilowatts. |
113114

114-
The function then 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 function then 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.
115116

116117
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:
117118

118119
### [Isolated worker model](#tab/isolated-process)
119-
120+
120121
:::code language="csharp" source="~/functions-openapi-turbine-repair-isolated/TurbineRepair/Turbine.cs":::
121122

122-
### [In-process model](#tab/in-process)
123+
### [In-process model](#tab/in-process)
123124

124125
:::code language="csharp" source="~/functions-openapi-turbine-repair/TurbineRepair/Turbine.cs":::
125126

@@ -135,12 +136,12 @@ When you run the function, the OpenAPI endpoints make it easy to try out the fun
135136
136137
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.
137138
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**.
139140
140141
:::image type="content" source="media/openapi-apim-integrate-vs/swagger-ui-post.png" alt-text="Swagger UI for testing the TurbineRepair API":::
141142
142143
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+
144145
:::image type="content" source="media/openapi-apim-integrate-vs/swagger-ui-response.png" alt-text="Response JSON data from the TurbineRepair function.":::
145146
146147
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
149150
150151
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.
151152
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**.
153154
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**.
155156
156157
1. In **Function Instance**, choose **+ Create a new Azure Function...**.
157158
@@ -162,19 +163,19 @@ Before you can publish your project, you must have a function app in your Azure
162163
| Setting | Value | Description |
163164
| ------------ | ------- | -------------------------------------------------- |
164165
| **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. |
168169
| **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). |
170171
171172
:::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":::
172173
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.
174175
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.
176177
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**.
178179
179180
1. Create an **API in API Management** by using values in the following table:
180181
@@ -191,15 +192,15 @@ Before you can publish your project, you must have a function app in your Azure
191192
192193
1. Select **Finish** and after the publish profile creation process completes, select **Close**.
193194
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.
195196
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.
197198
198199
## Get the function access key
199200
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.
201202
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**.
203204
204205
:::image type="content" source="media/openapi-apim-integrate-vs/get-function-keys.png" alt-text="Get an access key for the TurbineRepair function":::
205206
@@ -212,8 +213,8 @@ Before you can publish your project, you must have a function app in your Azure
212213
1. If the function app isn't already connected to the new API Management instance, select it 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**.
213214

214215
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**.
217218

218219
1. Below **Inbound processing**, in **Set query parameters**, type `code` for **Name**, select **+Value**, paste in the copied function key, and select **Save**. API Management includes the function key when it passes calls through to the function endpoint.
219220

@@ -223,7 +224,7 @@ Now that the function key is set, you can call the `turbine` API endpoint to ver
223224

224225
## Verify the API in Azure
225226

226-
1. In the API, select the **Test** tab and then **POST Run**, enter the following code in the **Request body** > **Raw**, and select **Send**:
227+
1. In the API, select the **Test** tab, and then **POST Run**. Enter the following code in the **Request body** > **Raw**, and select **Send**:
227228

228229
```json
229230
{
@@ -234,24 +235,24 @@ Now that the function key is set, you can call the `turbine` API endpoint to ver
234235

235236
:::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":::
236237

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.
238239

239240
1. Select **Send**, and then view the **HTTP response** to verify the same results are returned from the API.
240241

241242
## Download the OpenAPI definition
242243

243244
If your API works as expected, you can download the OpenAPI definition for the new hosted APIs from API Management.
244245

245-
1. 1. Under **APIs**, select **OpenAPI Document on Azure Functions**, select the ellipses (**...**), and select **Export**.
246-
246+
1. Under **APIs**, select **OpenAPI Document on Azure Functions**, select the ellipses (**...**), and select **Export**.
247+
247248
![Download OpenAPI definition](media/openapi-apim-integrate-vs/download-definition.png)
248249

249-
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).
250251

251252
## Clean up resources
252253

253254
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+
255256
From the Azure portal menu or **Home** page, select **Resource groups**. Then, on the **Resource groups** page, select the group you created.
256257
257258
On the **myResourceGroup** page, make sure that the listed resources are the ones you want to delete.

0 commit comments

Comments
 (0)