I am writing a program in .NET to retrieve the usage cost of a subscription. The structure of the HTTP request is this:
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.
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:
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:
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:
Reference:
Manage Azure costs with automation - Microsoft Cost Management