The Cloud SQL Admin 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, see
Authenticate for using client libraries.
Application Default Credentials are part of the client libraries you can use to
access 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 to choose credentials based on the type of environment where
you run code. For example, in a development environment, you can authenticate
with the gcloud auth command and the client libraries will use those
credentials. For more information about environments, see
Authentication overview in the Google Cloud Platform Auth Guide.
Access the service
Depending on the Cloud SQL Admin 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.
// 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());
}
funcListInstances(projectIdstring)([]*sqladmin.DatabaseInstance,error){ctx:=context.Background()// Create an http.Client that uses Application Default Credentials.hc,err:=google.DefaultClient(ctx,sqladmin.SqlserviceAdminScope)iferr!=nil{returnnil,err}// Create the Google Cloud SQL service.service,err:=sqladmin.New(hc)iferr!=nil{returnnil,err}// List instances for the project ID.instances,err:=service.Instances.List(projectId).Do()iferr!=nil{returnnil,err}returninstances.Items,nil}
// Set up global SQLAdmin instance.
client = new SQLAdmin.Builder(httpTransport, JSON_FACTORY, credential)
.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());
}
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.
};
from googleapiclient import discovery
# Construct the service object for the interacting with the Cloud SQL Admin 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 uses 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 the Connecting overview page.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-21 UTC."],[],[],null,["# Client libraries and sample code for Cloud SQL\n\n\u003cbr /\u003e\n\n[MySQL](/sql/docs/mysql/admin-api/libraries \"View this page for the MySQL database engine\") \\| PostgreSQL \\| [SQL Server](/sql/docs/sqlserver/admin-api/libraries \"View this page for the SQL Server database engine\")\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThe Cloud SQL Admin API is built on HTTP and JSON, so any standard HTTP client can\nsend requests to it and parse the responses. However, instead of creating HTTP\nrequests and parsing responses manually, you may want to use the Google APIs\nclient libraries. The client libraries provide better language integration,\nimproved security, and support for making calls that require user authorization.\n\n\nClient libraries can use\n[Application Default Credentials](/docs/authentication/application-default-credentials)\nto easily authenticate with Google APIs and send requests to those APIs. With\nApplication Default Credentials, you can test your application locally and\ndeploy it without changing the underlying code. For more information, see\n[Authenticate for using client libraries](/docs/authentication/client-libraries).\n\nApplication Default Credentials are part of the client libraries you can use to\naccess Cloud SQL. Default credentials identify your application with either a\nuser credential or a default service account. When working with a library, it's\ntypical to choose credentials based on the type of environment where\nyou run code. For example, in a development environment, you can authenticate\nwith the [`gcloud auth`](/sdk/gcloud/reference/auth) command and the client libraries will use those\ncredentials. For more information about environments, see\n[Authentication overview](/docs/authentication) in the *Google Cloud Platform Auth Guide*.\n\nAccess the service\n------------------\n\nDepending on the Cloud SQL Admin API client library you use, you may need to\nconfigure how the library discovers the default service path. For client\nlibraries that use the Google APIs Discovery Service, use the API name\n`sqladmin` to build a client. This includes libraries for Python and JavaScript.\n\nThe following code snippets show how to create a client and list\nCloud SQL instances in a project. \n\n### C++\n\nFor the [C++ Client Library](https://github.com/google/google-cloud-cpp),\nfollow [Setting up a C++ development environment](/cpp/docs/setup) to install the\nlibrary.\n\n\n\n #include \"google/cloud/sql/v1/sql_instances_client.h\"\n #include \"google/cloud/project.h\"\n #include \u003ciostream\u003e\n\n int main(int argc, char* argv[]) try {\n if (argc != 2) {\n std::cerr \u003c\u003c \"Usage: \" \u003c\u003c argv[0] \u003c\u003c \" project-id\\n\";\n return 1;\n }\n\n namespace sql = ::google::cloud::sql_v1;\n auto client = sql::SqlInstancesServiceClient(\n sql::MakeSqlInstancesServiceConnectionRest());\n\n google::cloud::sql::v1::SqlInstancesListRequest request;\n request.set_project(argv[1]);\n for (auto database : client.List(request)) {\n if (!database) throw std::move(database).status();\n std::cout \u003c\u003c database-\u003eDebugString() \u003c\u003c \"\\n\";\n }\n\n return 0;\n } catch (google::cloud::Status const& status) {\n std::cerr \u003c\u003c \"google::cloud::Status thrown: \" \u003c\u003c status \u003c\u003c \"\\n\";\n return 1;\n }\n\n### Java\n\nFor the [Client Library for Java](https://developers.google.com/api-client-library/java/google-api-java-client/dev-guide),\nyou can optionally specify the service path directly. \n\n```\n// Set up global SQLAdmin instance.\nclient = new SQLAdmin.Builder(httpTransport, JSON_FACTORY, credential)\n .setServicePath(\"sql/v1beta4/\")\n .setApplicationName(APPLICATION_NAME).build();\nInstancesListResponse resp = client.instances().list(\"PROJECT_ID\").execute();\nList\u003cDatabaseInstance\u003e list = resp.getItems();\nfor (DatabaseInstance d : list) {\n System.out.println(d.getName());\n}\n```\n\n### Go\n\nFor the [Client Library for Go](https://github.com/google/google-api-go-client),\nimport the [`sqladmin`](https://godoc.org/google.golang.org/api/sqladmin) package.\n\n\n\n func ListInstances(projectId string) ([]*sqladmin.DatabaseInstance, error) {\n \tctx := context.Background()\n\n \t// Create an http.Client that uses Application Default Credentials.\n \thc, err := google.DefaultClient(ctx, sqladmin.SqlserviceAdminScope)\n \tif err != nil {\n \t\treturn nil, err\n \t}\n\n \t// Create the Google Cloud SQL service.\n \tservice, err := sqladmin.New(hc)\n \tif err != nil {\n \t\treturn nil, err\n \t}\n\n \t// List instances for the project ID.\n \tinstances, err := service.Instances.List(projectId).Do()\n \tif err != nil {\n \t\treturn nil, err\n \t}\n \treturn instances.Items, nil\n }\n\n### Java\n\nFor the [Client Library for Java](https://developers.google.com/api-client-library/java/google-api-java-client/dev-guide),\nyou can optionally specify the service path directly. \n\n```\n// Set up global SQLAdmin instance.\nclient = new SQLAdmin.Builder(httpTransport, JSON_FACTORY, credential)\n .setApplicationName(APPLICATION_NAME).build();\nInstancesListResponse resp = client.instances().list(\"PROJECT_ID\").execute();\nList\u003cDatabaseInstance\u003e list = resp.getItems();\nfor (DatabaseInstance d : list) {\n System.out.println(d.getName());\n}\n```\n\n### JavaScript\n\nFor the [Client Library for JavaScript](https://developers.google.com/api-client-library/javascript/start/start-js),\nspecify `sqladmin` to build a client. \n\n```\ngapi.client.load('sqladmin', 'v1beta4', function() { console.log('loaded');});\ngapi.client.sql.instances.list({'project': PROJECT_ID}).execute(showResult);\nfunction showResult(result) {\n // Process the result.\n};\n```\n\n### Python\n\nFor the [Client Library for Python](https://developers.google.com/api-client-library/python/),\nspecify `sqladmin` to build a client. \n\n```\nfrom googleapiclient import discovery\n\n# Construct the service object for the interacting with the Cloud SQL Admin API.\nservice = discovery.build('sqladmin', 'v1beta4', http=http)\n\nreq = service.instances().list(project=\"PROJECT_ID\")\nresp = req.execute()\nprint(json.dumps(resp, indent=2))\n```\n\nThe `service` object queries the discovery document and uses the correct\nservice path, in this case, \"sql/v1beta4/projects/\".\n\nIf you are looking for code samples that show how applications can connect to\nCloud SQL, see the [Connecting overview](/sql/docs/postgres/connect-overview#languages) page.\n\nLibraries and sample code\n-------------------------"]]