pythonazureazure-billing-api

Retrieving last month cost of an Azure subscription using Python SDK


I am trying to retrieve the total cost of a subscription for a given month (let's say last month).

I tried using ComputeManagementClient but I always end up with a whole list of usage details, even when I use a filter in the following way :

c_client = ConsumptionManagementClient(creds, sub_id)
startDate = '2019-03-01T00:00:00Z'
endDate = '2019-03-31T23:59:59Z'
filt = "usageStart eq {} and usageEnd eq {}".format(startDate, endDate)
result = c_client.usage_details.list(filter=filt)
...

What am I doing wrong ? Do I have to iterate through pages and calculate the total cost by myself ?

EDIT : Thanks to @Joy Wang answer, I get further. But now I end up with daily usage details from the current month instead of last month - whereas I am sure that this subscription was running last month. Any idea ?


Solution

  • It seems you should use properties/usageStart and properties/usageEnd instead of usageStart and usageEnd.

    See this link:

    enter image description here