Edit

Share via


Tutorial: Mock API responses

APPLIES TO: All API Management tiers

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.

In this tutorial, you learn how to:

  • Create a test API
  • Add an operation to the test API
  • Enable response mocking
  • Test the mocked API

Screenshot that shows the APIs page in the Azure portal.

Prerequisites

Create a test API

The steps in this section show how to create an HTTP API with no backend.

  1. Sign in to the Azure portal, and then navigate to your API Management instance.

  2. Select APIs > + Add API > HTTP tile:

    Screenshot that shows the first steps for defining an API.

  3. In the Create an HTTP API window, select Full.

  4. In Display name, enter Test API.

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

  6. In Gateways, select Managed if this option is available. (This option is avaiable only in certain service tiers.)

  7. Select Create.

    Screenshot that shows the Create an HTTP API window.

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.

  1. Select the API that you created in the previous step.

  2. Select + Add Operation.

  3. In the Frontend window, enter the following values:

    Setting Value Description
    Display name Test call The name that's displayed in the developer portal.
    URL (first box) GET Select one of the predefined HTTP verbs.
    URL (second box) /test A URL path for the API.
    Description An optional description of the operation. It provides documentation in the developer portal to the developers who use the API.

    Screenshot that shows the Frontend window.

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

  5. Select + Add response, and then select 200 OK from the list.

    Screenshot that shows the Responses tab.

  6. In the Representations section, select + Add representation.

  7. Enter application/json into the search box and then select the application/json content type.

  8. In the Sample box, enter { "sampleField" : "test" }.

  9. Select Save.

    Screenshot that shows the Representations section.

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.

Enable response mocking

  1. Select the API you created in Create a test API.

  2. Ensure that the Design tab is selected.

  3. Select the test operation that you added.

  4. In the Inbound processing section, select + Add policy.

    Screenshot that shows the first steps for enabling response mocking.

  5. Select the Mock responses tile from the gallery:

    Screenshot that shows the Mock responses tile.

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

    Screenshot that shows the API Management response selection.

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

Test the mocked API

  1. Select the API you created in Create a test API.

  2. On the Test tab, ensure that the Test call API is selected, and then select Send to make a test call:

    Screenshot that shows the steps for testing the mocked API.

  3. The HTTP response displays the JSON provided as a sample in the first section of the tutorial:

    Screenshot that shows the mock HTTP response.

Next step

Go to the next tutorial:


Additional resources