Exercise - Create a new API in API Management from a function app
To present Azure Functions to users as parts of a single API, you can add them to Azure API Management.
In your online store company, your developers 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.
In this exercise, you create a new API Management instance and then add a Product Details function to it.
Create functions
In the following steps, you add an Azure Function app to Azure API Management. Later, you add a second function app to the same API Management instance to create a single serverless API from multiple functions. Let's start by using a script to create the functions:
To clone the functions project, run the following command in Azure Cloud Shell on the right.
git clone https://github.com/MicrosoftDocs/mslearn-apim-and-functions.git ~/OnlineStoreFuncs
Run the following commands in Cloud Shell to set up the necessary Azure resources we need for this exercise.
cd ~/OnlineStoreFuncs bash setup.sh
The
setup.sh
script creates the two function apps in the sandbox resource group that are activated for this module. As the following graphic illustrates, each app hosts a single function -OrderDetails
andProductDetails
. 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 ProductDetails function
Now, let's test the ProductDetails function to see how it behaves before we add it to API Management.
Sign in to the Azure portal using the same account that you used to activate the sandbox.
On the Azure portal menu or from the Home page, select All resources. The All resources pane appears.
Select the Function App whose name begins with ProductFunction. The Function App pane appears with the Overview tab selected.
From the list of functions for your function app, select ProductDetails. The ProductDetails Function pane appears.
In the Product Details menu, under Developer, select Code + Test. The Code + Test pane for the ProductDetails function appears, showing the contents of
function.json
file.On the command bar, select Test/Run. The Input/Output pane appears for testing your HTTP request.
On the Input tab, in the HTTP method field dropdown list, select GET.
Under Query, Select Add parameter
In the Name field, enter id, and in the Value field, enter 3, and then select Run.
A log pane appears to connect the request. The Output tab displays the HTTP response code (200 OK) and content for item 3 in the database.
Optionally, you can also test the function by inputting ID values 1 and 2.
Select Close to close the Input Output tabs for Code + Test.
On the command bar of the ProductDetails Code + Test pane, select Get function URL. Notice that the URL subdomain is the name of your function app, followed by the azurewebsites.net domain.
Note
You can use this URL to test the function in your browser. The URL requires an input; append the query string &id=1
to request details about the first item in your product collection.
Expose function app as an API using Azure API Management
After the function app is deployed and tested, let's export it as an API using Azure API Management so that it can be called from other apps and services.
In the resource menu, select All resources. The All resources pane appears.
Sort the list of resources by type. Notice you have an OrderFunction app and a ProductFunction app.
Select the ProductFunction function app.
In the Function App menu, scroll to the API category, and then select API Management. The API Management pane for ProductFunction function app appears.
Under the API Management field, select Create new. The Install API Management gateway pane appears.
Enter the following values for each setting.
Setting Value Subscription Concierge Subscription Resource group [sandbox resource group name] Region Choose a location that supports the Consumption Plan: (US) West US, North Central US, West Europe, North Europe, Southeast Asia, Australia East Resource name OnlineStore Organization name OnlineStore Administrator email Enter an email address. Pricing tier Consumption (99.95% SLA) Select Next: Monitoring and clear the Application Insights option.
Select Review + Create. When the validation completes, select Create to create a linked API Management instance. Allow several minutes for the export to complete.
When the API Management instance is deployed, select Link API.
The Import Azure Functions API Management service pane appears with the ProductDetails function highlighted.
Select Select to continue. The Create from Function App dialog box appears.
Change the API URL suffix value to products, and then select Create. Azure creates the API for the ProductDetails function. The Design tab for API Management pane for your function app appears.
This exercise shows that you can define your API, all from within the Azure Function App service.
Test the OnlineStore products endpoint
You now have a ProductDetails
API in the API Management instance that you created. Let's test that API with the API Management tools in Azure.
In the API Management pane of your function app, select the Test tab. The available operations for your API appear in the left column.
Select GET ProductDetails. The ProductDetails Console pane appears.
Under Query parameters, select Add parameter.
In the NAME field, enter id, and in the VALUE field, enter 1, and then select Send to generate a GET request.
The HTTP response section of the console has two tabs, Message and Trace. The Message tab is populated with the HTTP response. The product details appear in JSON format at the end of the response. Scroll up to the HTTP request section and notice the format of the request. The request was sent to a destination in the azure-api.net domain. This location is different from the azurewebsites.net domain where the function app is hosted. You can test this API with other query parameters, such as id=2 or id=3.