BigQuery Connection API Client Libraries

This page shows how to get started with the Cloud Client Libraries for the BigQuery Connection API. Read more about the client libraries for Cloud APIs, including the older Google API Client Libraries, in Client Libraries Explained.

Installing the client library


For more information, see Setting Up a C# Development Environment.

Install-Package Google.Cloud.BigQuery.Connection.V1 -Pre


For more information, see Setting Up a Go Development Environment.

go get


For more information, see Setting Up a Java Development Environment.

If you are using Maven, add the following to your pom.xml file. For more information about BOMs, see The Google Cloud Platform Libraries BOM.


If you are using Gradle, add the following to your dependencies:

implementation ''

If you are using sbt, add the following to your dependencies:

libraryDependencies += "" % "google-cloud-bigqueryconnection" % "2.1.8"

If you're using Visual Studio Code, IntelliJ, or Eclipse, you can add client libraries to your project using the following IDE plugins:

The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.


For more information, see Setting Up a Node.js Development Environment.

npm install @google-cloud/bigquery-connection


For more information, see Using PHP on Google Cloud.

composer require google/cloud-bigquery-connection


For more information, see Setting Up a Python Development Environment.

pip install --upgrade google-cloud-bigquery-connection


For more information, see Setting Up a Ruby Development Environment.

gem install google-cloud-bigquery-connection

Setting up authentication

To run the client library, you must first set up authentication. One way to do that is to create a service account and set an environment variable, as shown in the following steps. For other ways to authenticate, see Authenticating as a service account.

Cloud Console

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 and continue.
  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.

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.

Command line

You can run the following commands using the Cloud SDK on your local machine, or in Cloud Shell.

  1. Create the service account. Replace NAME with a name for the service account.

    gcloud iam service-accounts create NAME
  2. Grant permissions to the service account. Replace PROJECT_ID with your project ID.

    gcloud projects add-iam-policy-binding PROJECT_ID --member="" --role="roles/owner"
  3. Generate the key file. Replace FILE_NAME with a name for the key file.

    gcloud iam service-accounts keys create FILE_NAME.json

Provide authentication credentials to your application code by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS. This variable applies only to your current shell session. If you want the variable to apply to future shell sessions, set the variable in your shell startup file, for example in the ~/.bashrc or ~/.profile file.

Linux or macOS


Replace KEY_PATH with the path of the JSON file that contains your service account key.

For example:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"


For PowerShell:


Replace KEY_PATH with the path of the JSON file that contains your service account key.

For example:


For command prompt:


Replace KEY_PATH with the path of the JSON file that contains your service account key.

Using the client library

The following example demonstrates some basic interactions with the BigQuery Connection API.


To use this sample, prepare your machine for Go development, and complete the BigQuery Connection API quickstart. For more information, see the BigQuery Connection API Go API reference documentation.

// The bigquery_connection_quickstart application demonstrates basic usage of the
// BigQuery connection API.
package main

import (

	connection ""
	connectionpb ""

func main() {

	// Define two command line flags for controlling the behavior of this quickstart.
	projectID := flag.String("project_id", "", "Cloud Project ID, used for session creation.")
	location := flag.String("location", "US", "BigQuery location used for interactions.")

	// Parse flags and do some minimal validation.
	if *projectID == "" {
		log.Fatal("empty --project_id specified, please provide a valid project ID")
	if *location == "" {
		log.Fatal("empty --location specified, please provide a valid location")

	ctx := context.Background()
	connClient, err := connection.NewClient(ctx)
	if err != nil {
		log.Fatalf("NewClient: %v", err)
	defer connClient.Close()

	s, err := reportConnections(ctx, connClient, *projectID, *location)
	if err != nil {
		log.Fatalf("printCapacityCommitments: %v", err)

// reportConnections gathers basic information about existing connections in a given project and location.
func reportConnections(ctx context.Context, client *connection.Client, projectID, location string) (string, error) {
	var buf bytes.Buffer
	fmt.Fprintf(&buf, "Current connections defined in project %s in location %s:\n", projectID, location)

	req := &connectionpb.ListConnectionsRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
	totalConnections := 0
	it := client.ListConnections(ctx, req)
	for {
		conn, err := it.Next()
		if err == iterator.Done {
		if err != nil {
			return "", err
		fmt.Fprintf(&buf, "\tConnection %s was created %s\n", conn.GetName(), unixMillisToTime(conn.GetCreationTime()).Format(time.RFC822Z))
	fmt.Fprintf(&buf, "\n%d connections processed.\n", totalConnections)
	return buf.String(), nil

// unixMillisToTime converts epoch-millisecond representations used by the API into a time.Time representation.
func unixMillisToTime(m int64) time.Time {
	if m == 0 {
		return time.Time{}
	return time.Unix(0, m*1e6)


Before trying this sample, follow the Java setup instructions in Setting up a Java development environment. For more information, see the BigQuery Connection API Java API reference documentation.


// Sample to demonstrates basic usage of the BigQuery connection API.
public class QuickstartSample {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "MY_PROJECT_ID";
    String location = "MY_LOCATION";
    listConnections(projectId, location);

  public static void listConnections(String projectId, String location) throws IOException {
    try (ConnectionServiceClient connectionServiceClient = ConnectionServiceClient.create()) {
      LocationName parent = LocationName.of(projectId, location);
      int pageSize = 10;
      ListConnectionsRequest request =
      ConnectionServiceClient.ListConnectionsPagedResponse response =

      // Print the results.
      System.out.println("List of connections:");
          .forEach(connection -> System.out.println("Connection Name: " + connection.getName()));

Additional resources

What's next?

For more background, see Working with connections.