How can I retrieve usage and cost data for my IBM Cloud account using a REST API? I found that there are billing related commands and I can export some data as JSON. Is there an API or SDK I can use, ideally Python?
Here are some of the IBM Cloud billing commands I use:
ibmcloud billing resource-instances-usage --json
ibmcloud billing account-usage --json
Are there equivalent APIs for that?
I couldn't find a documented API but used the tracing to see how the above commands are executed. Using a valid access_token a program can call the metering host and obtain usage data for an account, resource group or all resource instances:
A GET on the following URL with an account ID and month as YYYY-MM returns a JSON object with all resource usage and related cost:
I coded up a small Python script that dumps that data or prints it as CSV.
The GitHub repo openwhisk-cloud-usage-samples uses a serverless approach to getting data via APIs. Examples are included in the repo. It's written in Javascript, but a package it uses openwhisk-jsonetl was designed so that you could declare the URLs and parameters in YAML (rather than writing code) to request and transform JSON.