Loading, please wait...

A to Z Full Forms and Acronyms

How to Create a new API in API Management from a function app Part-2

Jul 14, 2020 New API, API Management, function app, 3169 Views
In this article, we’ll discuss Create a new API in API Management from a function app

How to Create a new API in API Management from a function app Part-2

You can add Functions to Azure API Management, to present them to users as parts of a single API.

In your online store company, your developers have created multiple Azure Functions as microservices. Each function implements a small part of the store's functionality. You want to assemble these functions into a single API.

Here, you will create a new API Management instance and then add a Product Details function to it.

Create functions

In this exercise, you will add an Azure Functions app to Azure API Management. Later you will add a second function app to the same API Management instance in order to create a single serverless API from multiple functions. Let's start by using a script to create the functions:

  1. To clone the functions project, in the Cloud Shell on the right, execute this command:

    git clone https://github.com/MicrosoftDocs/mslearn-apim-and-functions.git ~/OnlineStoreFuncs
    
  2. Run the following commands in the Cloud Shell to set up the necessary Azure resources we need for this exercise.

    cd ~/OnlineStoreFuncs
    bash setup.sh
    

    The setup.sh the script creates the two function apps in the sandbox resource group that we've activated for this module. As the following graphic illustrates, each app hosts a single function - OrderDetails and ProductDetails. the script also sets up a storage account for the functions. The functions both have URLs in the azurewebsites.net domain. The function names include random numbers for uniqueness. The script takes a few minutes to complete.

Test the product details function

Now, let's test the ProductDetails function, to see how it behaves before we add it to API Management:

  1. Sign in to the Azure portal using the same account with which you activated the sandbox.

  2. On the Azure portal menu or from the Home page, select All resources.

  3. Choose the App Service whose name begins with ProductFunction. The Function of Apps UI for this app opens in the portal.

  4. Select Functions in the menu, then select ProductDetails.

     

  5. Select Code + Test, then select Test, as highlighted in the following screenshot:

  6. In the HTTP method drop-down list, select GET, and then select Add parameter.

  7. In the Name textbox, type id and in the value textbox, type 3.

  8. Select Run and examine the results in the Output box, then select Close.

    The output pane displays the details of a product in JSON format. You can also test the function with IDs 1 and 2 for different products

  9. At the top of the page, select the Get function URL. Notice that the URL is the name of the function within the azurewebsites.net domain. Make a note of this URL for later comparison.

 Note

You can use this URL to test the function in your browser. Append the query string &id=1 to request a product.

Expose our function app as an API using Azure API Management

Now that we have our function app deployed and tested, we'll expose the functionality as an API using Azure API Management so that the function can be called from other apps and services.

  1. On the Azure portal menu or from the Home page, select All resources, and select the App Service whose name begins with ProductFunction.

  2. Select API Management, and select Create new under API Management.

  3. Use the API Management settings as specified in the following table.

    TABLE 1
    Setting Value
    Name Use a unique name within the azure-api.net domain
    Subscription Concierge
    Resource group [sandbox resource group name]
    Location Choose one of the following locations that support the Consumption Plan: West USNorth Central USWest EuropeNorth EuropeSoutheast AsiaAustralia East
    Organization name OnlineStore
    Administrator email Use the default value
    Pricing tier Consumption
  4. Choose to Create to create the API Management instance, which may take several minutes, then select Link API.

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

  6. In the Create from Function App page, change API URL suffix to products and select Create. The API is now created for the ProductDetails function.

Note that we were able to define our API all from within the function app portal experience.

Test the OnlineStore products endpoint

You now have a functional product details API in the API Management instance that you created. Let's test that API, by using the API Management tools in the Azure portal.

  1. With the API Management instance still open, select the Test tab.

  2. Select Get ProductDetails as the operation that we want to test.

  3. Under Query parameters, select the Add parameter.

  4. In the Name textbox, enter id.

  5. In the value textbox, enter 1, and then select Send.

  6. In the HTTP response, notice that the details of a product have been returned in JSON format. Also, notice the HTTP request was sent to a destination within the azure-api.net domain. This location is different from the azurewebsites.net domain where the original function app is hosted.

A to Z Full Forms and Acronyms

Related Article