Create an OpenAPI definition for a serverless API using Azure API Management

REST APIs are often described using an OpenAPI definition. This definition contains information about what operations are available in an API and how the request and response data for the API should be structured.

In this tutorial, you create a function that determines whether an emergency repair on a wind turbine is cost-effective. You then create an OpenAPI definition for the function app using Azure API Management so that the function can be called from other apps and services.

In this tutorial, you learn how to:

  • Create a function in Azure
  • Generate an OpenAPI definition using Azure API Management
  • Test the definition by calling the function
  • Download the OpenAPI definition

Create a function app

You must have a function app to host the execution of your functions. A function app lets you group functions as a logical unit for easier management, deployment, scaling, and sharing of resources.

  1. From the Azure portal menu, select Create a resource.

    Add resource by using the Azure portal menu

  2. In the New page, select Compute > Function App.

  3. Use the function app settings as specified in the table below the image.


    Setting Suggested value Description
    Subscription Your subscription The subscription under which this new function app is created.
    Resource Group myResourceGroup Name for the new resource group in which to create your function app.
    Function App name Globally unique name Name that identifies your new function app. Valid characters are a-z (case insensitive), 0-9, and -.
    Publish Code Option to publish code files or a Docker container.
    Runtime stack Preferred language Choose a runtime that supports your favorite function programming language. Choose .NET for C# and F# functions.
    Region Preferred region Choose a region near you or near other services your functions access.

    Select the Next : Hosting > button.

  4. Enter the following settings for hosting.


    Setting Suggested value Description
    Storage account Globally unique name Create a storage account used by your function app. Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only. You can also use an existing account, which must meet the storage account requirements.
    Operating system Preferred operating system An operating system is pre-selected for you based on your runtime stack selection, but you can change the setting if necessary.
    Plan Consumption plan Hosting plan that defines how resources are allocated to your function app. In the default Consumption Plan, resources are added dynamically as required by your functions. In this serverless hosting, you only pay for the time your functions run. When you run in an App Service plan, you must manage the scaling of your function app.

    Select the Next : Monitoring > button.

  5. Enter the following settings for monitoring.


    Setting Suggested value Description
    Application Insights Default Creates an Application Insights resource of the same App name in the nearest supported region. By expanding this setting, you can change the New resource name or choose a different Location in an Azure geography where you want to store your data.

    Select Review + Create to review the app configuration selections.

  6. Select Create to provision and deploy the function app.

  7. Select the Notification icon in the upper-right corner of the portal and watch for the Deployment succeeded message.

    Deployment notification

  8. Select Go to resource to view your new function app. You can also select Pin to dashboard. Pinning makes it easier to return to this function app resource from your dashboard.

Create the function

This tutorial uses an HTTP triggered function that takes two parameters:

  • The estimated time to make a turbine repair, in hours.
  • The capacity of the turbine, in kilowatts.

The function then calculates how much a repair will cost, and how much revenue the turbine could make in a 24 hour period. To create the HTTP triggered function in the Azure portal:

  1. Expand your function app and select the + button next to Functions. Select In-portal > Continue.

  2. Select More templates..., then select Finish and view templates

  3. Select HTTP trigger, type TurbineRepair for the function Name, choose Function for Authentication level, and then select Create.

    Create HTTP function for OpenAPI

  4. Replace the contents of the run.csx C# script file with the following code, then choose Save:

    #r "Newtonsoft.Json"
    using System.Net;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Primitives;
    using Newtonsoft.Json;
    const double revenuePerkW = 0.12;
    const double technicianCost = 250;
    const double turbineCost = 100;
    public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
        // Get query strings if they exist
        int tempVal;
        int? hours = Int32.TryParse(req.Query["hours"], out tempVal) ? tempVal : (int?)null;
        int? capacity = Int32.TryParse(req.Query["capacity"], out tempVal) ? tempVal : (int?)null;
        // Get request body
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        // Use request body if a query was not sent
        capacity = capacity ?? data?.capacity;
        hours = hours ?? data?.hours;
        // Return bad request if capacity or hours are not passed in
        if (capacity == null || hours == null){
            return new BadRequestObjectResult("Please pass capacity and hours on the query string or in the request body");
        // Formulas to calculate revenue and cost
        double? revenueOpportunity = capacity * revenuePerkW * 24;  
        double? costToFix = (hours * technicianCost) +  turbineCost;
        string repairTurbine;
        if (revenueOpportunity > costToFix){
            repairTurbine = "Yes";
        else {
            repairTurbine = "No";
        return (ActionResult)new OkObjectResult(new{
            message = repairTurbine,
            revenueOpportunity = "$"+ revenueOpportunity,
            costToFix = "$"+ costToFix

    This function code returns a message of Yes or No to indicate whether an emergency repair is cost-effective, as well as the revenue opportunity that the turbine represents, and the cost to fix the turbine.

  5. To test the function, click Test at the far right to expand the test tab. Enter the following value for the Request body, and then click Run.

    "hours": "6",
    "capacity": "2500"

    Test the function in the Azure portal

    The following value is returned in the body of the response.


Now you have a function that determines the cost-effectiveness of emergency repairs. Next, you generate an OpenAPI definition for the function app.

Generate the OpenAPI definition

Now you're ready to generate the OpenAPI definition.

  1. Select the function app, then in Platform features, choose API Management and select Create new under API Management.

    Choose API Management in Platform Features

  2. Use the API Management settings as specified in the table below the image.

    Create new API Management service

    Setting Suggested value Description
    Name Globally unique name A name is generated based on the name of your function app.
    Subscription Your subscription The subscription under which this new resource is created.
    Resource Group myResourceGroup The same resource as your function app, which should get set for you.
    Location West US Choose the West US location.
    Organization name Contoso The name of the organization used in the developer portal and for email notifications.
    Administrator email your email Email that received system notifications from API Management.
    Pricing tier Consumption (preview) Consumption tier is in preview and isn't available in all regions. For complete pricing details, see the API Management pricing page
  3. Choose Create to create the API Management instance, which may take several minutes.

  4. Select Enable Application Insights to send logs to the same place as the function application, then accept the remaining defaults and select Link API.

  5. The Import Azure Functions opens with the TurbineRepair function highlighted. Choose Select to continue.

    Import Azure Functions into API Management

  6. In the Create from Function App page, accept the defaults and select Create

    Create from Function App

The API is now created for the function.

Test the API

Before you use the OpenAPI definition, you should verify that the API works.

  1. On the Test tab of your function, select POST operation.

  2. Enter values for hours and capacity

    "hours": "6",
    "capacity": "2500"
  3. Click Send, then view the HTTP response.

    Test function API

Download the OpenAPI definition

If your API works as expected, you can download the OpenAPI definition.

  1. Select Download OpenAPI definition at the top of the page.

    Download OpenAPI definition

  2. Open the downloaded JSON file and review the definition.

Clean up resources

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.

From the Azure portal menu or Home page, select Resource groups, and on the Resource groups page, select myResourceGroup.

On the myResourceGroup page, make sure that the listed resources are the ones you want to delete.

Select Delete, type myResourceGroup in the text box, and then select Delete.

Next steps

You have used API Management integration to generate an OpenAPI definition of your functions. You can now edit the definition in API Management in the portal. You can also learn more about API Management.