3
votes

can we use any google API which can provide information about the total charges spent using Google Cloud Platform? i have tried few APIs (detail shown in code).

API 1:

var google = require('googleapis');
var cloudbilling = google.cloudbilling('v1');
google.auth.getApplicationDefault(function(err, authClient) {
if (err) {
console.log('Authentication failed because of ', err);
return;
}

if (authClient.createScopedRequired && 
authClient.createScopedRequired()) {
var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
authClient = authClient.createScoped(scopes);
 }
 var request = {
  name: "projects/<your-project-id>", 
// Auth client
  auth: authClient
};
 cloudbilling.projects.getBillingInfo(request, function(err, result) {
  if (err) {
   console.log(err);
}  else {
   console.log(result);
 }
   });
  });

What i am getting in json (about billing information) is:

data:
   { name: 'projects/<your-project-name>/billingInfo',
     projectId: '<project-Id>',
 billingAccountName: 'billingAccounts/<your-billing-id>',
 billingEnabled: true } 
   }

Now i have tried other APIs too, but they are also not providing the total charges spent!. Is there a way i can find the bills?

2

2 Answers

4
votes

The info in the actuall bills is not (or at least not yet) available in any of the Cloud Billing APIs.

But there are other options for accessing the data via other paths:

To access a detailed breakdown of your charges, you can export your daily usage and cost estimates automatically to a CSV or JSON file stored in a Google Cloud Storage bucket you specify. You can then access the data via the Cloud Storage API, CLI tool, or Google Cloud Platform Console.

Tools for monitoring, analyzing and optimizing cost have become an important part of managing development. Billing export to BigQuery enables you to export your daily usage and cost estimates automatically throughout the day to a BigQuery dataset you specify. You can then access your billing data from BigQuery.

0
votes

There are two ways to get the billing data from Google Cloud Platform:

  • Export Billing Data to BigQuery
  • Export Billing Data to a File

For BigQuery:

  1. Enable billing export to BigQuery
  2. Create a BigQuery Client
  3. Run a query to get the billing data query to get daily cost:

For File:

  1. Enable billing export to a file
  2. Write a code to download an object from a bucket
  3. Iterate through a downloaded file and perform the summation of "cost" property

You can refer to the script that I have created here

Hope this helps :).