Backend APIs are imported into an Azure API Management API or created and managed manually. The steps in this tutorial describe how to:
Use API Management to create a blank HTTP API.
Manually manage an HTTP API.
Set a policy on an API so that it returns a mock response.
This method enables developers to continue with the implementation and testing of the API Management instance even if the backend isn't available to send real responses.
Tip
API teams can use this feature in workspaces. Workspaces provide isolated administrative access to APIs and their own API runtime environments.
The ability to create mock responses is useful in many scenarios:
When the API façade is designed first and the backend implementation occurs later, or when the backend is being developed in parallel.
When the backend is temporarily not operational or is not able to scale.
The steps in this section show how to create an HTTP API with no backend.
Sign in to the Azure portal, and then navigate to your API Management instance.
Select APIs > + Add API > HTTP tile:
In the Create an HTTP API window, select Full.
In Display name, enter Test API.
In Products, select Unlimited, if that value is available. This value is available only in some tiers. You can leave the value blank for this tutorial, but you need to associate the API with a product to publish it. For more information, see Import and publish your first API.
In Gateways, select Managed if this option is available. (This option is avaiable only in certain service tiers.)
Select Create.
Add an operation to the test API
An API exposes one or more operations. In this section, you add an operation to the HTTP API you created. Calling the operation after completing the steps in this section triggers an error. After you complete the steps in the Enable response mocking section, you won't get an error.
An optional description of the operation. It provides documentation in the developer portal to the developers who use the API.
Select the Responses tab, which is located under the URL, Display name, and Description boxes. You'll enter values on this tab to define response status codes, content types, examples, and schemas.
Select + Add response, and then select 200 OK from the list.
In the Representations section, select + Add representation.
Enter application/json into the search box and then select the application/json content type.
In the Sample box, enter { "sampleField" : "test" }.
Select Save.
Although it's not required for this example, you can configure more settings for an API operation on other tabs, as described in the following table:
Tab
Description
Query
Add query parameters. Besides providing a name and description, you can also provide values that are assigned to a query parameter. You can mark one of the values as default (optional).
Request
Define request content types, examples, and schemas.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Authenticate to Azure using Azure CLI.
az apim api operation list --resource-group <resource-group-name> \
--api-id test-api --service-name <API-management-service-name> --output table
Keep this operation for use in the rest of this article. If you want to remove an operation, you can use the az apim api operation delete command. Get the operation ID from the previous command.
az apim api operation delete --resource-group <resource-group-name> \
--api-id test-api --operation-id <ID> \
--service-name <API-management-service-name>
In the Inbound processing section, select + Add policy.
Select the Mock responses tile from the gallery:
Ensure that 200 OK, application/json appears in the API Management response box. This selection indicates that your API should return the response sample that you defined in the previous section.
Select Save.
Tip
A yellow bar displaying the text Mocking is enabled appears. This message indicates that the responses returned from API Management are mocked by the mocking policy and aren't produced by the backend.