Can Azure APIM compress the response message sent from a backend application

Thirumal Shanmugavel 0 Reputation points
2025-04-27T17:45:52.1433333+00:00

I have got an use case where requirement to check the capability of azure apim - if it can compress the message on its own . In this requirement, backend will be sending a message response payload of 30 MB to 200 MB, which needs to be compressed in APIM, is is it possible by just doing simple policy?

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,395 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Shireesha Eeraboina 2,855 Reputation points Microsoft External Staff Moderator
    2025-04-28T10:29:15.6333333+00:00

    Hi Thirumal Shanmugavel,

    Yes, Azure API Management (APIM) can compress the response messages sent from your backend application. This helps to reduce the size of the data being sent to the client, making the process faster and more efficient.

    To enable compression in APIM, you can use the gzip compression policy. This policy automatically compresses the response before sending it to the client. It’s a simple way to improve performance.

    Here’s an example of how you can set up this compression policy in APIM:

    @{
    
    var body = context.Response.Body.As();
    
    var compressedBody = new System.IO.MemoryStream();
    
    using (var gzip = new System.IO.Compression.GZipStream(compressedBody, System.IO.Compression.CompressionMode.Compress))
    
    {
    
    gzip.Write(body, 0, body.Length);
    
    }
    
    return compressedBody.ToArray();
    
    }
    
    

    In this example, the response message will automatically be compressed using gzip if it’s larger than 1 KB. You can change this size limit based on what works best for your needs.

    By adding this setting to the outbound section of your API Management policy, you can make sure that only larger responses get compressed, improving performance and saving bandwidth

    let me know incase of further queries, I would be glad to assist you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.