Microsoft cost management - Query Usage API using Python script

Jancy John 86 Reputation points
2022-11-17T16:24:44.723+00:00

Hi All,

I am trying to access Microsoft cost management - Query Usage API using Python script.

I am able to get the output JSON response with 6 columns of data (PreTaxCost,UsageDate,ResourceGroup,ResourceType,ResourceLocation,Currency) using the below same POST request body in Microsoft API portal, Power BI and in Azure data factory.

I wanted to do some formatting to the output JSON response and trying to do that in Python. But, the Python code is returning only 4 columns in the resulting JSON (PreTaxCost,UsageDate,ResourceLocation,Currency), ignoring the grouping of ResourceGroup,ResourceType in Python.

Could anyone please help me with this issue?

API end point : https://learn.microsoft.com/en-gb/rest/api/cost-management/query/usage?tabs=HTTP
POST https://management.azure.com/{scope}/providers/Microsoft.CostManagement/query?api-version=2022-10-01

Below is the Python code I am using

usageApi_url = "https://management.azure.com//subscriptions/"+subscription_id+"//providers/Microsoft.CostManagement/query?api-version=2021-10-01"

auth_header ={
'Authorization' : 'Bearer ' + token,
'Content-Type':'application/json'
}

usage_body ={
"type": "ActualCost",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
],
"grouping": [
{
"type": "Dimension",
"name": "ResourceType"
}
],
"grouping": [
{
"type": "Dimension",
"name": "ResourceLocation"
}
]
}
}

response = requests.post(usageApi_url,json = usage_body, headers = auth_header)
response.json()

261515-json-response.png

Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
0 comments No comments
{count} votes

Answer accepted by question author
  1. SadiqhAhmed-MSFT 49,456 Reputation points Moderator
    2022-11-18T16:42:06.967+00:00

    Hi @Anonymous Thank you for posting your question on Microsoft Q&A forum. Happy to assist!

    I see that you are running a python script to get data for a set of groups. However, couple of grouping data is missing.

    You need to modify your python script a bit. You can only have one grouping value. Make that an array of all the groupings. This should help yielding desired results.

    Feel free to post questions if you need any further assistance!

    ----------------------------------------------------------------------------------------------------------------------

    If the response helped, do "Accept Answer" and up-vote it

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Vinodh247 40,031 Reputation points MVP Volunteer Moderator
    2022-11-18T17:10:00.847+00:00

    Hi

    Thanks for reaching out to Microsoft Q&A.

    Check out your grouping values, you cannot have so many of it. As sadiq has pointed out, try to make an array for the groupings.

    Please Upvote and Accept as answer if the reply was helpful, this will be helpful to other community members.

    1 person found this answer helpful.

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.