Overview of Libraries and Samples for Google Cloud SQL

The Google Cloud SQL API is built on HTTP and JSON, so any standard HTTP client can send requests to it and parse the responses. However, instead of creating HTTP requests and parsing responses manually, you may want to use the Google APIs client libraries. The client libraries provide better language integration, improved security, and support for making calls that require user authorization.

Client libraries can use Application Default Credentials to easily authenticate with Google APIs and send requests to those APIs. With Application Default Credentials, you can test your application locally and deploy it without changing the underlying code. For more information, including code samples, see Google Cloud Platform Auth Guide.

Application Default Credentials are part of the client libraries you can use to access Google Cloud SQL. Default credentials identify your application with either a user credential or a default service account. When working with a library, it's typical that you'll choose credentials based on the type of environment where you run your code.

Accessing the service

Depending on the Google Cloud SQL API client library you use, you may need to configure how the library discovers the default service path. For client libraries that use the Google APIs Discovery Service, use the API name sqladmin to build a client. This includes libraries for Python and JavaScript.

The following code snippets show how to create a client and list Cloud SQL instances in a project.

Go

For the Client Library for Go, import the sqladmin package.

func ListInstances(projectId string) ([]*sqladmin.DatabaseInstance, error) {
	ctx := context.Background()

	// Create an http.Client that uses Application Default Credentials.
	hc, err := google.DefaultClient(ctx, sqladmin.SqlserviceAdminScope)
	if err != nil {
		return nil, err
	}

	// Create the Google Cloud SQL service.
	service, err := sqladmin.New(hc)
	if err != nil {
		return nil, err
	}

	// List instances for the project ID.
	instances, err := service.Instances.List(projectId).Do()
	if err != nil {
		return nil, err
	}
	return instances.Items, nil
}

Java

For the Client Library for Java, you can optionally specify the service path directly.

// Set up global SQLAdmin instance.
client = new SQLAdmin.Builder(httpTransport, JSON_FACTORY, credential)
    .setServicePath("sql/v1beta4/")
    .setApplicationName(APPLICATION_NAME).build();
InstancesListResponse resp = client.instances().list("PROJECT_ID").execute();
List<DatabaseInstance> list = resp.getItems();
for (DatabaseInstance d : list) {
    System.out.println(d.getName());
}

JavaScript

For the Client Library for JavaScript, specify sqladmin to build a client.

gapi.client.load('sqladmin', 'v1beta4', function() { console.log('loaded');});
gapi.client.sql.instances.list({'project': PROJECT_ID}).execute(showResult);
function showResult(result) {
  // Process the result.
};

Python

For the Client Library for Python, specify sqladmin to build a client.

# Construct the service object for the interacting with the Cloud SQL Administration API.
service = discovery.build('sqladmin', 'v1beta4', http=http)

req = service.instances().list(project="PROJECT_ID")
resp = req.execute()
print json.dumps(resp, indent=2)

The service object queries the discovery document and use the correct service path, in this case, "sql/v1beta4/projects/".

If you are looking for code samples that show how applications can connect to Cloud SQL, see Connecting from External Applications. For examples of connecting from App Engine, see Connecting from Google App Engine.

Client library authentication

Client libraries can use Application Default Credentials to easily authenticate with Google APIs and send requests to those APIs. With Application Default Credentials, you can test your application locally and deploy it without changing the underlying code. For more information, including code samples, see Google Cloud Platform Auth Guide.

Application Default Credentials are part of the client libraries you can use to access Google Cloud SQL. Default credentials identify your application with either a user credential or a default service account. When working with a library, it's typical that you'll choose credentials based on the type of environment where you run your code. For example, in a developer environment, you can authenticate with the gcloud auth command and the client libraries will use those credentials. For more information about environments, see Developer Workflow in the Google Cloud Platform Auth Guide.

List of libraries and samples

In the tables below, the first column shows each library's stage of development (note that some are in early stages), and links to documentation for the library. The second column links to available samples for each library.

Documentation Samples
Google API Client Library for Java Java samples
Google API Client Library for JavaScript JavaScript samples
Google API Client Library for .NET .NET samples
Google API Client Library for Objective-C for REST Objective-C samples
Google API Client Library for PHP (beta) PHP samples
Google API Client Library for Python Python samples

These early-stage libraries are also available:

Documentation Samples
Google APIs Client Libraries for Dart (beta) Dart samples
Google API Client Library for Go (alpha) Go samples
Google API Client Library for Node.js (alpha) Node.js samples
Google API Client Library for Ruby (alpha) Ruby samples

Send feedback about...

Cloud SQL for MySQL