azureazure-billing-apiazure-cost-calculation

Azure cost management API does not respect the desired date filters


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.


Solution

  • 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