0

I am writing a program in .NET to retrieve the usage cost of a subscription. The structure of the HTTP request is this:

https://management.azure.com/subscriptions/(here_goes_the_subscription_number)/providers/Microsoft.Consumption/usageDetails?startDate=2023-06-01&endDate=2023-06-02&api-version=2021-10-01

The problem is: No matter what dates I put in the "startDate" and "endDate", it always retrieves all the cost data of the current month from day 1 of the month. For example: If today is July 15, the request brings to me all the cost data from July 1 to July 15, regardless of the date interval I specified.

What am I doing wrong? Thank you very much for any feedback you could provide to this tortured soul in disguise.

  • 1
    Have a look at this https://learn.microsoft.com/en-us/azure/cost-management-billing/costs/manage-automation#get-usage-details-for-a-scope-during-specific-date-range. The API is different if you are using an Enterprise agreement/payasyougo or a Microsoft customer agreement. Which one do you have? – Anupam Chand Jul 04 '23 at 18:01

1 Answers1

1

I agree with @Anupam Chand, API response differs based on the type of subscription that you are currently using.

To retrieve the usage cost of a subscription within specific duration, you need to include $filter in query that works with only Enterprise Agreement (EA) or a pay-as-you-go subscriptions and not supported by Microsoft Customer Agreements subscription.

If you have Enterprise Agreement (EA) or a pay-as-you-go subscription, you can make use of below query by including $filter to retrieve the usage cost of a subscription within specific duration:

GET https://management.azure.com/subscriptions/<subID>/providers/Microsoft.Consumption/usageDetails?$filter=properties/usageStart ge '2023-06-01' and properties/usageEnd le '2023-06-02'&$top=1000&api-version=2019-10-01

I have one Azure account with pay-as-you-go subscription like below:

enter image description here

When I ran below query by including $filter with different dates, I got different results successfully like this:

QUERY 1:

GET https://management.azure.com/subscriptions/<subID>/providers/Microsoft.Consumption/usageDetails?$filter=properties/usageStart ge '2023-07-01' and properties/usageEnd le '2023-07-02'&$top=1000&api-version=2019-10-01

Response:

POSTMAN SNAP

QUERY 2:

GET https://management.azure.com/subscriptions/<subID>/providers/Microsoft.Consumption/usageDetails?$filter=properties/usageStart ge '2023-07-01' and properties/usageEnd le '2023-07-04'&$top=1000&api-version=2019-10-01

Response:

POSTMAN SNAP

Reference:

Manage Azure costs with automation - Microsoft Cost Management

Sridevi
  • 10,599
  • 1
  • 4
  • 17