How to keep APIM SubscriptionName in the policy

Lily 131 Reputation points
2024-02-19T11:31:53.44+00:00

Hi, we want to keep APIM Subscription Name (the unique, immutable one, not display name) to the log by API policy for further analysis.

We find there are two types of Name on the UI.
The red one is what we want to keep in the log, for it's immutable, like primary key.User's image

After some tests, we cannot find a way to store this red name in the API policy.

What we've done so far .

  1. Use following code snippet in the API policy
new JProperty("SubscriptionName", context.Subscription.Name), ==> Display Name is stored

new JProperty("SubscriptionId", context.Subscription.Id) ==> Nothing stored, it's blank.

User's image

But the Name fetched is Display Name, and if using SubscriptionId, it's blank. User's image

We hope to fetch the value like this, in the APIM > Logs .
(Though its field name here is ApimSubscriptionId)
User's image

Need your advice to know how we can make it in the API policy configuration.

Thank you

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

1 answer

Sort by: Most helpful
  1. JananiRamesh-MSFT 29,276 Reputation points
    2024-02-20T09:36:52.3166667+00:00

    @Lily Thanks for reaching out. I see with context.Subscription.Id it retrieves name of the subscription and with context.Subscription.Name it retrieves display name of the subscription as shown below

      <inbound>
            <base />
            <set-variable name="subname" value="@(context.Subscription.Id)" />
            <set-variable name="subname" value="@(context.Subscription.Key)" />
            <set-variable name="subname" value="@(context.Subscription.Name)" />
        </inbound>
    
    
    
    set-variable (0.008 ms)
    {
        "message": "Expression was successfully evaluated.",
        "expression": "context.Subscription.Id",
        "value": "thisisname"
    }
    set-variable (0.012 ms)
    {
        "message": "Context variable was successfully set.",
        "name": "subname",
        "value": "thisisname"
    }
    set-variable (0.003 ms)
    {
        "message": "Expression was successfully evaluated.",
        "expression": "context.Subscription.Key",
        "value": "7e59xxxxxxxxxxxx9bf794b"
    }
    set-variable (0.002 ms)
    {
        "message": "Context variable was successfully set.",
        "name": "subname",
        "value": "7e5xxxxxxxxxxxf794b"
    }
    set-variable (0.002 ms)
    {
        "message": "Expression was successfully evaluated.",
        "expression": "context.Subscription.Name",
        "value": "thisisdispalyname"
    }
    set-variable (0.001 ms)
    {
        "message": "Context variable was successfully set.",
        "name": "subname",
        "value": "thisisdispalyname"
    }
    

    do check and let me know incase of further queries, I would be happy to assist you.

    1 person found this answer helpful.

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.