azureazure-devopsazure-cliazure-devops-rest-apiazure-billing-api

az rest command to get Usage Details for previous months/dates. (Microsoft.Consumption)


I have the following URL which is used to get the UsageDetails from Microsoft. consumption

az rest --method GET --url 'https://management.azure.com/subscriptions/{subscription}/providers/Microsoft.Consumption/UsageDetails?api-version=2019-10-01'

Now, I want the details for the time periods between 2022-11-01 and 2022-11-30. But so far I have been only able to retrieve current billing cycle details.

Please let me know if I have to use a different API version or how to add the date/timeframe filter to the API call to retrieve the previous month's data other than the current billing cycle.

Also, I am open to suggestions if there is a different command call altogether to get these cost/ usage of resources/subscriptions other than az rest with Microsoft.Consumption


Solution

  • There are two different methods of filtering usage details: one works only for legacy subscriptions and another for modern subscriptions.

    For legacy customers with an Enterprise Agreement (EA) or a pay-as-you-go subscription, use the parameter $filter=properties/usageStart ge '2022-11-01' and properties/usageEnd le '2022-11-30'. So the API request should look like this:

    GET https://management.azure.com/{scope}/providers/Microsoft.Consumption/usageDetails?$filter=properties%2FusageStart%20ge%20'2022-11-01'%20and%20properties%2FusageEnd%20le%20'2022-11-30'&$top=1000&api-version=2019-10-01
    

    For modern customers with a Microsoft Customer Agreement, use the parameters startDate and endDate, for example:

    GET https://management.azure.com/{scope}/providers/Microsoft.Consumption/usageDetails?startDate=2022-11-01&endDate=2022-11-30&$top=1000&api-version=2019-10-01
    

    See documentation for more details. Please note that the data returned by the API corresponds to the date when the usage was received by the billing system and it can include costs from multiple invoices.