Send Requests

This topic shows how to send a few simple requests to the Google 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 account.

    If you don't already have one, sign up for a new account.

  2. Select or create a Cloud Platform project.

    Go to the Manage resources page

  3. Enable billing for your project.

    Enable billing

  4. Enable the Cloud Billing API.

    Enable the API

  5. Set up authentication:
    1. Go to the Create service account key page in the GCP Console.

      Go to the Create Service Account Key page
    2. From the Service account drop-down list, select New service account.
    3. Input a name into the Service account name field.
    4. From the Role drop-down list, select Project > Owner.

      Note: The Role field authorizes your service account to access resources. You can view and change this field later using GCP Console. If you are developing a production application, specify more granular permissions than Project > Owner. For more information, see granting roles to service accounts.
    5. Click Create. A JSON file that contains your key downloads to your computer.
  6. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key.

Get a list of billing accounts

The API method to list all 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.




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

Retrieve a particular 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.




    BillingAccount result = service.billingAccounts()

Enable billing on a project

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



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


            new ProjectBillingInfo().setBillingAccountName("billingAccounts/012345-567890-ABCDEF"))

Clean up

  1. In the Cloud Platform Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.