How to mock responses with Azure API Management

How to mock responses with Azure API Management

What is Azure API Managment?

Microsoft Azure API Management is a solution for publishing APIs to external and internal consumers. You can quickly configure API gateways for your existing backend services which can be hosted in Azure or everywhere else. With Azure API Management you can protect and secure your APIs from overuse and abuse. The developer portal of Azure API Management provides a great way to document your APIs and manage access to your API definitions.

Azure API Management also provides high scalability and great real-time analytics reports.

Why mock responses?

There are several scenarios when it makes sense to mock responses in Azure API Management. The developers will be able to access your APIs and continue implementation and testing even if:

  • The backend is temporarely not available
  • When the API facade is designed first and the backend processes are not finished

How to mock responses?

Microsoft Azure API Management provides a way to maintain policies. Those can be used for formatting input and output JSON and XML, configure call rate limits to restrict the amount of incoming calls and different other things. A complete list of the policy settings and their statements can be found here

By configuring a so called return-response policy it is easily possible to mock the API response.

The policy has the following format:

<return-response response-variable-name="existing response variable">

    <set-status code="code" reason="reason" />

    <set-header name="header name" exists-action="override | skip | append | delete">

        <value>value</value> <!-- for multiple headers with the same name add additional value elements -->


    <set-body>new body value as text</set-body>


To show how to use policies for mocking a response, I have created a sample API with the existing calculator sample in the Azure API Management Publisher Portal.

For demonstration I added a new operation „Add 3 integers“


When the developers try to test this new operation via the Developer Portal they will receive an error message. This is because the back end functionality is not implemented yet.


To create a mock for this new functionality a so called return-response policy has to be configured. This can be done in the Publisher Portal in the area Policies.

Change the scope to the API the policy is used for. In our case „Calculator“ and click on „Add Policy“. The policy has to be added to the inbound section of the policy.

The policy will look like this. As you see, it is possible to use simple C# code to read

request parameters and do simple calculations.



When the API operation is tested now, the policy will be used to mock the result:


It is also possible to create JSON or XML responses in the policy and deliver them back to the API consumer.

  • Erstellt am .
Copyright by Orange Networks GmbH