Send Requests

This topic shows how to send a few simple requests to the Cloud Billing API.

For a full list of methods, see the REST or RPC reference documentation.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Enable the Cloud Billing API.

    Enable the API

  5. Create a service account:

    1. In the Cloud Console, go to the Create service account page.

      Go to Create service account
    2. Select a project.
    3. In the Service account name field, enter a name. The Cloud Console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create.
    5. Click the Select a role field.

      Under Quick access, click Basic, then click Owner.

    6. Click Continue.
    7. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  6. Create a service account key:

    1. In the Cloud Console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  7. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

Get a list of Cloud Billing Accounts

The API method to list all Cloud Billing Accounts (REST, RPC) is the simpest method in the API, as it has no required parameters, so it's a good place to start. The method simply returns all billing accounts that you have permission to view.

Protocol

GET https://cloudbilling.googleapis.com/v1/billingAccounts

Java

    ListBillingAccountsResponse result = service.billingAccounts().list().execute();

Retrieve a particular Cloud Billing Account

To retrieve a particular billing account (REST, RPC), you'll need to know the billing account ID. This example uses the sample ID 012345-567890-ABCDEF.

Protocol

GET https://cloudbilling.googleapis.com/v1/billingAccounts/012345-567890-ABCDEF

Java

    BillingAccount result = service.billingAccounts()
        .get("billingAccounts/00C5EA-61187E-D842F2").execute();

Enable Cloud Billing on a Google Cloud project

To enable Cloud Billing on a Cloud project, you need to call the Cloud Billing API to associate the Cloud project with an existing Cloud Billing Account (REST, RPC). The following sample code associates Cloud project tokyo-rain-123 with Cloud Billing Account 012345-567890-ABCDEF. Incidentally, the same API method can be used to disable Cloud Billing on a Cloud project, by setting billingAccountName to empty.

Protocol

PUT https://cloudbilling.googleapis.com/v1/projects/tokyo-rain-123/billingInfo

{
  "billingAccountName": "billingAccounts/012345-567890-ABCDEF"
}

Java

    service.projects().updateBillingInfo(
            "projects/tokyo-rain-123",
            new ProjectBillingInfo().setBillingAccountName("billingAccounts/012345-567890-ABCDEF"))
        .execute();

Clean up

  1. In the Cloud Console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.