I am looking to get the real-time current amount spent by a given resource (not the whole subscription or a resource group) during the current period on MS Azure through Azure's REST API (e.g.: We have spent X$ so far this month on this resource). I have dug around the Billing API and general REST API documentation (and also their node.js SDK), but I can't find anything that seems to do that.
Has anyone ever done something similar? Any help will be greatly appreciated.
Please note to this doc: Azure consumption API overview , here shows that you can use Usage Details API with filter to get what you want:
- Filtering - Trim your API result set down to a smaller set of usage detail records using the following filters:
- Usage end / usage start
- Resource Group
- Resource Name
Here is the format of this API:
GET https://management.azure.com/{scope}/providers/Microsoft.Consumption/usageDetails?$expand={$expand}&$filter={$filter}&$skiptoken={$skiptoken}&$top={$top}&$apply={$apply}&api-version=2019-01-01
For {scope}
, except can use '/subscriptions/{subscriptionId}/' for subscription scope, it can also use '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}' for Billing Account scope and etc. For more details, reference this doc.
Use $filter={$filter}
can achieve what you want(access specific resource), here provided my API can for your reference:
GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Consumption/usageDetails?$expand=meterDetails,additionalProperties&$filter=properties/usageEnd ge '2019-01-1' AND properties/usageEnd le '2019-05-22' AND properties/instanceName eq '{instanceName}'&$top=30&api-version=2019-01-01
eq
means equals,ge
means greater or equal,le
means less or equal, and the instanceNmae means your specific resource name that the usage is about.