How to add custom request body in APIM

Amy Zhang 25 Reputation points
2025-01-17T08:33:51.09+00:00

Hi,

Our customer would like to include an additional trace_id in each API call for enhanced auditing purposes.

The plan is to add the trace_id in the API policy so it becomes part of the payload, enabling the backend service to store and use it for future reference.

Ideally, we want the trace_id to be captured in APIM for easier correlation later. For example, it could be the built-in field in the ApiManagementGatewayLogs, but we’re unsure which field would be the most suitable for this.

Regarding the payload format, our backend is a SaaS system that only accepts JSON input. Below is an example of the expected JSON structure, with the trace_id included in the customFields section:

{
  "summary": "I'm issue 1",
  "description": "This is issue 1",
  "project": {
    "id": "{{projectID}}"
  },
  "customFields": [
        {
            "name": "Priority",
            "$type": "SingleEnumIssueCustomField",
            "value": {
                "name": "Normal"
            }
        },
        {
            "name": "trace_id",
            "$type": "SimpleIssueCustomField",
            "value": "traceid_001"
        }
       
    ]
}

We’re exploring how this can be achieved through API policy. Would you be able to advise on the best approach?

Thank you!

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

1 answer

Sort by: Most helpful
  1. Sina Salam 17,646 Reputation points
    2025-01-17T15:41:07.63+00:00

    Hello Amy Zhang,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you would like to add custom request body in APIM.

    To include a trace_id in each API call for enhanced auditing purposes, you can use Azure API Management (APIM) policies to modify the request payload and log the trace_id. By using the set-body policy, you can dynamically add a trace_id to the request payload. Additionally, using the log-to-application-insights policy, you can log the trace_id for auditing and correlation purposes - https://learn.microsoft.com/en-us/azure/api-management/trace-policy and https://learn.microsoft.com/en-us/azure/api-management/api-management-howto-api-inspector

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.

    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.