Google Cloud Billing API

1.19.0 · active · verified Mon Apr 13

The `google-cloud-billing` Python client library provides programmatic access to the Google Cloud Billing API, allowing developers to manage their billing accounts, link projects, or browse the catalog of SKUs and pricing information. It offers a high-level interface for interacting with Google Cloud Billing services. This library is actively maintained by Google, with regular updates to support new features and maintain compatibility with the latest Python versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the `CloudBillingClient` and list billing accounts accessible to the authenticated user. It relies on Application Default Credentials (ADC) for authentication. Ensure your local environment is authenticated (e.g., via `gcloud auth application-default login`) and that the service account or user account has the necessary IAM permissions (e.g., `billing.accounts.get`).

import os
from google.cloud import billing_v1

try:
    # Initialize the client using Application Default Credentials
    # Ensure 'gcloud auth application-default login' has been run or
    # GOOGLE_APPLICATION_CREDENTIALS environment variable is set.
    client = billing_v1.CloudBillingClient()

    # List billing accounts accessible by the authenticated user
    # Requires 'billing.accounts.get' permission on the billing account resource.
    print("Listing billing accounts:")
    for account in client.list_billing_accounts():
        print(f"  Name: {account.name}")
        print(f"  Display Name: {account.display_name}")
        print(f"  Open: {account.open}")

    # Example: Get a specific billing account (replace with a valid account ID)
    # billing_account_id = "012345-ABCDEF-789012"
    # billing_account_name = f"billingAccounts/{billing_account_id}"
    # account = client.get_billing_account(name=billing_account_name)
    # print(f"\nDetails for {account.display_name}: {account.name}")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure you have set up Application Default Credentials and have sufficient permissions.")
    print("Run 'gcloud auth application-default login' for local development or set the GOOGLE_APPLICATION_CREDENTIALS environment variable.")

view raw JSON →