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()