Transform and protect your API
The tutorial shows how to transform your API so it does not reveal a private backend info. For example, you might want to hide the info about technology stack that is running on the backend. You might also want to hide original URLs that appear in the body of API's HTTP response and instead redirect them to the APIM gateway.
This tutorial also shows you how easy it is to add protection for your backend API by configuring rate limit with Azure API Management. For example, you may want to limit a number of calls the API is called so it is not overused by developers. For more information, see API Management policies
In this tutorial, you learn how to:
- Transform an API to strip response headers
- Replace original URLs in the body of the API response with APIM gateway URLs
- Protect an API by adding rate limit policy (throttling)
- Test the transformations

Prerequisites
- Learn the Azure API Management terminology.
- Understand the concept of policies in Azure API Management.
- Complete the following quickstart: Create an Azure API Management instance.
- Also, complete the following tutorial: Import and publish your first API.
Go to your API Management instance
In the Azure portal, search for and select API Management services.

On the API Management screen, select your API Management instance.

Transform an API to strip response headers
This section shows how to hide the HTTP headers that you do not want to show to your users. In this example, the following headers get deleted in the HTTP response:
- X-Powered-By
- X-AspNet-Version
Test the original response
To see the original response:
- In your APIM service instance, select APIs (under API MANAGEMENT).
- Click Demo Conference API from your API list.
- Click the Test tab, on the top of the screen.
- Select the GetSpeakers operation.
- Press the Send button, at the bottom of the screen.
The original response should look like this:

Set the transformation policy

Select Demo Conference API.
On the top of the screen, select Design tab.
Select All operations.
In the Outbound processing section, click the </> icon.
Position the cursor inside the <outbound> element.
In the right window, under Transformation policies, click + Set HTTP header twice (to insert two policy snippets).

Modify your <outbound> code to look like this:
<set-header name="X-Powered-By" exists-action="delete" /> <set-header name="X-AspNet-Version" exists-action="delete" />
Click the Save button.
Replace original URLs in the body of the API response with APIM gateway URLs
This section shows how to hide original URLs that appear in the body of API's HTTP response and instead redirect them to the APIM gateway.
Test the original response
To see the original response:
Select Demo Conference API.
Click the Test tab, on the top of the screen.
Select the GetSpeakers operation.
Press the Send button, at the bottom of the screen.
As you can see the original response looks like this:

Set the transformation policy
Select Demo Conference API.
Select All operations.
On the top of the screen, select Design tab.
In the Outbound processing section, click the </> icon.
Position the cursor inside the <outbound> element.
In the right window, under Transformation policies, click + Find and replace string in body.
Modify your find-and-replace code (in the <outbound> element) to replace the URL to match your APIM gateway. For example:
<find-and-replace from="://conferenceapi.azurewebsites.net" to="://apiphany.azure-api.net/conference"/>
Protect an API by adding rate limit policy (throttling)
This section shows how to add protection for your backend API by configuring rate limits. For example, you may want to limit a number of calls the API is called so it is not overused by developers. In this example, the limit is set to 3 calls per 15 seconds for each subscription Id. After 15 seconds, a developer can retry calling the API.

Select Demo Conference API.
Select All operations.
On the top of the screen, select Design tab.
In the Inbound processing section, click the </> icon.
Position the cursor inside the <inbound> element.
In the right window, under Access restriction policies, click + Limit call rate per key.
Modify your rate-limit-by-key code (in the <inbound> element) to the following code:
<rate-limit-by-key calls="3" renewal-period="15" counter-key="@(context.Subscription.Id)" />
Test the transformations
At this point if you look at the code in the code editor, your policies look like this:
<policies>
<inbound>
<rate-limit-by-key calls="3" renewal-period="15" counter-key="@(context.Subscription.Id)" />
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<set-header name="X-Powered-By" exists-action="delete" />
<set-header name="X-AspNet-Version" exists-action="delete" />
<find-and-replace from="://conferenceapi.azurewebsites.net:443" to="://apiphany.azure-api.net/conference"/>
<find-and-replace from="://conferenceapi.azurewebsites.net" to="://apiphany.azure-api.net/conference"/>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
The rest of this section tests policy transformations that you set in this article.
Test the stripped response headers
Select Demo Conference API.
Select the Test tab.
Click the GetSpeakers operation.
Press Send.
As you can see the headers have been stripped:

Test the replaced URL
Select Demo Conference API.
Select the Test tab.
Click the GetSpeakers operation.
Press Send.
As you can see the URL has been replaced.

Test the rate limit (throttling)
Select Demo Conference API.
Select the Test tab.
Click the GetSpeakers operation.
Press Send three times in a row.
After sending the request 3 times, you get 429 Too many requests response.
Wait 15 seconds or so and press Send again. This time you should get a 200 OK response.

Video
Next steps
In this tutorial, you learned how to:
- Transform an API to strip response headers
- Replace original URLs in the body of the API response with APIM gateway URLs
- Protect an API by adding rate limit policy (throttling)
- Test the transformations
Advance to the next tutorial:
Feedback
Loading feedback...




