Search code examples
azureapipolicyportal

Azure API Management/Portal: activate Mocking for "Try Out" in API Portal


I was wondering if there is the possibility to activate mocking for calls to an Azure API Management based API from the API Portal.

I don't want developers who browse the interface catalog and use the "try it" functionality to actually CRUD records from/in the backends. They should only receive a set of predefined data so they can develop against it without messing up the data in the backends.

Example

API is created on Azure API Management and productive. This API is listed in the corresponding Azure API Portal - a developer now finds that API and wants to try it out: enter image description here

POST /SalesQuote would create a Sales Quotation in SAP. A developer uses the "try it" button in the developer portal to see the behaviour. The "send" button now actually creates a record in the backend.

Can this be prevented? If so - how? Would this be something that could be achieved within the API policy (i.e. mock data for origin = api portal url)?

The only other option I can think of would be disabling the try it feature - rather a bad solution.

EDIT: general "mocking-enabled" is of course for a productive API no option.

Thanks for your help!


Solution

  • Thanks to Nacho I researched the policy features more deeply and played around a bit. I came up with the following:

    <inbound>
      <base />
      <choose>
        <when condition="@(context.User.Groups.Any(Group => Group.Name == "developer-internal"))">
          <mock-response status-code="201" content-type="application/json" />
        </when>
      </choose>
    </inbound>
    

    This should enable mocking only if the calling user is member of the group "developer-internal" (which are all developer portal users via AAD mapping).

    As an alternative I am validating the re-routing option to our test backend instead of mocking the response which would look like this:

    <inbound>
        <base />
        <choose>
            <when condition="@(context.User.Groups.Any(Group => Group.Name == "developer-internal"))">
                <!--mock-response status-code="201" content-type="application/json" /-->
                <set-backend-service base-url="https://esb-test.example.com/restv2/CostObject" />
            </when>
        </choose>
    </inbound>