How to Integrate Ms Teams Webhook URLs with Azure API Policies in API Management

Himansu Sekhar Panda 0 Reputation points
2025-04-04T13:56:53.2933333+00:00

I Generate Ms Teams Incomming Webhook URL and that URL I put in my API Policies in <on-error> section to get the notification in my Teams, about 500 errors, but I did not get any notification .Here is the Screenshot for the references. please help me, to get the 500 error notification in my Teams Screenshot from 2025-04-04 19-19-56

and here is my policy

<policies>
    <inbound>
        <base />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <!-- Optional: simulate 500 error for testing -->
        <return-response>
            <set-status code="500" reason="Simulated Error" />
            <set-body>Testing 5xx error for Teams webhook.</set-body>
        </return-response>
    </outbound>
    <on-error>
        <base />
        <choose>
            <when condition="@(context.Response.StatusCode >= 500)">
                <send-one-way-request mode="new">
                    <set-url>https://unifiedinfotech.webhook.office.com/webhookb2/a422f2d6-b724-4122-909b-4f0357e5109d@23eb4305-bcae-4cae-9612-e43eb80b6577/IncomingWebhook/f8edfc6b10b44253ac2aa02f54683487/1c002ec8-aa10-41b1-b953-ccb16f922193/V2W1-SmGOKdOI41S9thQ_daosPrNOKby09tD41s3_6wGo1</set-url>
                    <set-method>POST</set-method>
                    <set-header name="Content-Type" exists-action="override">
                        <value>application/json</value>
                    </set-header>
                    <set-body>@{
                        try {
                            var payload = new {
                                @type = "MessageCard",
                                @context = "https://schema.org/extensions",
                                themeColor = "FF0000",
                                summary = "🚨 APIM 5xx Error Alert",
                                sections = new[] {
                                    new {
                                        activityTitle = "🔥 **APIM 5xx Error Detected**",
                                        facts = new[] {
                                            new { name = "Request Method", value = context.Request.Method },
                                            new { name = "Request URL", value = context.Request.Url.Path + context.Request.Url.QueryString },
                                            new { name = "Host", value = context.Request.Url.Host },
                                            new { name = "Status Code", value = context.Response.StatusCode.ToString() },
                                            new { name = "Status Reason", value = context.Response.StatusReason },
                                            new { name = "Error Message", value = context.LastError?.Message ?? "N/A" },
                                            new { name = "User Email", value = context.User?.Email ?? "N/A" }
                                        },
                                        markdown = true
                                    }
                                }
                            };
 
                            return Newtonsoft.Json.JsonConvert.SerializeObject(payload);
                        } catch (Exception ex) {
                            return Newtonsoft.Json.JsonConvert.SerializeObject(new {
                                text = $"⚠️ Failed to generate payload: {ex.Message}"
                            });
                        }
                    }</set-body>
                </send-one-way-request>
            </when>
        </choose>
    </on-error>
</policies>
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
{count} votes

1 answer

Sort by: Most helpful
  1. RithwikBojja 3,210 Reputation points Microsoft External Staff Moderator
    2025-04-08T07:08:23.75+00:00

    Hi @Himansu Sekhar Panda ,

    It is working for me by using below Policy definition:

    Here, I have done for 200 , in Place 200, you can use 500:

    
    <policies>
    
        <inbound>
    
            <base />
    
        </inbound>
    
        <backend>
    
            <base />
    
        </backend>
    
        <outbound>
    
            <base />
    
            <choose>
    
                <when condition="@(context.Response.StatusCode >= 200)">
    
                    <send-one-way-request mode="new">
    
                        <set-url>https://microsoft.webhook.office.com/webhookb2/0196b69abe@72f1db47/IncomingWebhook/75d656c/c977881U1w1</set-url>
    
                        <set-method>POST</set-method>
    
                        <set-header name="Content-Type" exists-action="override">
    
                            <value>application/json</value>
    
                        </set-header>
    
                        <set-body>@{
    
            try {
    
                var payload = new {
    
                    @type = "MessageCard",
    
                    @context = "https://schema.org/extensions",
    
                    themeColor = "FF0000",
    
                    summary = "🚨 APIM 2xx Error Alert",
    
                    sections = new[] {
    
                        new {
    
                            activityTitle = "🔥 **APIM 2xx Error Detected**",
    
                            facts = new[] {
    
                                new { name = "Request Method", value = context.Request.Method },
    
                                new { name = "Request URL", value = context.Request.Url.Path + context.Request.Url.QueryString },
    
                                new { name = "Host", value = context.Request.Url.Host },
    
                                new { name = "Status Code", value = context.Response.StatusCode.ToString() },
    
                                new { name = "Status Reason", value = context.Response.StatusReason },
    
                                new { name = "Error Message", value = context.LastError?.Message ?? "N/A" },
    
                                new { name = "User Email", value = context.User?.Email ?? "N/A" }
    
                            },
    
                            markdown = true
    
                        }
    
                    }
    
                };
    
                return Newtonsoft.Json.JsonConvert.SerializeObject(payload);
    
            } catch (Exception ex) {
    
                return "{\"text\": \"⚠️ Failed to generate payload: " + ex.Message + "\"}";
    
            }
    
        }</set-body>
    
                    </send-one-way-request>
    
                </when>
    
            </choose>
    
            <return-response>
    
                <set-status code="200" reason="Simulated Error" />
    
                <set-body>Testing Teams webhook.</set-body>
    
            </return-response>
    
        </outbound>
    
        <on-error>
    
            <base />
    
        </on-error>
    
    </policies>
    
    

    Added above policy in Inbound Processing:

    enter image description here

    Output:

    Tested for 200:

    enter image description here

    APIM sends Message/Card in Teams Channel:

    enter image description here

    Try to follow above policy code and change the values and it will provide desired result as it has worked for me.

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.


Your answer

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