Tutorial: Mock API responses

Backend APIs can be imported into an API Management (APIM) API or created and managed manually. The steps in this tutorial show you how to use APIM to create a blank API and manage it manually, then set a policy on an API so it returns a mocked response. This method enables developers to proceed with implementation and testing of the APIM instance even if the backend is not available to send real responses.

The ability to mock up responses can be useful in a number of scenarios:

  • When the API fa├žade is designed first and the backend implementation comes later. Or, the backend is being developed in parallel.
  • When the backend is temporarily not operational or 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

Mocked API response


Create a test API

The steps in this section show how to create a blank API with no backend.

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

  2. Select APIs > + Add API > Blank API.

  3. In the Create a Blank API window, select Full.

  4. Enter Test API for Display name.

  5. Select Unlimited for Products.

  6. Ensure Managed is selected in Gateways.

  7. Select Create.

    Create blank API

Add an operation to the test API

An API exposes one or more operations. In this section, add an operation to the blank API you created. Calling the operation after completing steps in this section produces an error. You will get no errors after you complete steps later in the Enable response mocking section.

  1. Select the API 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 is displayed in the developer portal.
    URL (HTTP verb) GET Select one of the predefined HTTP verbs.
    URL /test A URL path for the API.
    Description Optional description of the operation, used to provide documentation in the developer portal to the developers using this API.
  4. Select the Responses tab, located under the URL, Display name, and Description fields. Enter settings on this tab to define response status codes, content types, examples, and schemas.

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

  6. Under the Representations heading on the right, select + Add representation.

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

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

  9. Select Save.

Add API operation

Although not required for this example, additional settings for an API operation can be configured on other tabs, including:

Tab Description
Query Add query parameters. Besides providing a name and description, you can provide values that are assigned to a query parameter. One of the values can be marked 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. Select the test operation that you added.

  3. In the window on the right, ensure that the Design tab is selected.

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

    Add processing policy

  5. Select Mock responses from the gallery.

    Mock responses policy tile

  6. In the API Management response textbox, type 200 OK, application/json. This selection indicates that your API should return the response sample you defined in the previous section.

    Set mocking response

  7. Select Save.


    A yellow bar with the text Mocking is enabled for your API indicates that responses returned from API Management are mocked by the mocking policy and are not produced by the backend.

Test the mocked API

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

  2. Select the Test tab.

  3. Ensure the Test call API is selected. Select Send to make a test call.

    Test the mocked API

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

    Mock HTTP response

Next steps

In this tutorial, you learned how to:

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

Advance to the next tutorial: