1

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

Yashas .M
  • 17
  • 5

1 Answers1

0

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.

Anna Gevel
  • 1,103
  • 1
  • 11
  • 20
  • %20and%20properties%2FusageEnd%20le%20 checking if this filter query is right. I did try adding the usageStart and usageEnd dates but the query always stops and gives the following error:- unrecognized arguments: 2022-11-01%20and%20properties%2FusageEnd%20le%202022-11-30. Maybe it has got something to do with the fact that the subscription might belong to a CSP plan? – Yashas .M Dec 08 '22 at 23:59
  • It looks like the problem is with quote characters. If you wrap `--url` parameter in single quotes, it treats quotes inside `$filter` parameter as the end of URL. Please try wrapping your URL in double quotes instead of single quotes like this: `az rest --method GET --url "https://management.azure.com/..."`. If it does not help, please share the full az rest URL and I will check what may be wrong with it. – Anna Gevel Dec 09 '22 at 08:44
  • This is my current command. az rest --method GET --url "https://management.azure.com/subscriptions/{subId}/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" – Yashas .M Dec 10 '22 at 01:11
  • The error message i get is the following. Bad Request({"error":{"code":"MissingApiVersionParameter","message":"The api-version query parameter (?api-version=) is required for all requests."}}) – Yashas .M Dec 10 '22 at 01:12
  • @Yashas.M thanks for providing these details, I've just tried your command and it worked correctly for me. The error "MissingApiVersionParameter" means that Azure API did not receive your full URL and was not able to read the `api-version` parameter. Please make sure that there are no line breaks or other whitespace characters when you copy the subscription ID. I occasionally get a line break character when copying subscription guid from the Azure portal and it causes the error "MissingApiVersionParameter". – Anna Gevel Dec 10 '22 at 22:03