Spanner Client Usage

Instantiating a Client

To use the API, theClientclass defines a high-level interface which handles authorizationand creating other objects:

from google.cloud importspannerclient =spanner.Client()

Configuration

  • For an overview of authentication ingoogle.cloud-python,seeAuthentication.

  • In addition to any authentication configuration, you can also set theGCLOUD_PROJECT environment variable for the Google Cloud Consoleproject you’d like to interact with. If your code is running in Google AppEngine or Google Compute Engine the project will be detected automatically.(Setting this environment variable is not required, you may instead pass theproject explicitly when constructing aClient).

  • After configuring your environment, create aClient

>>> from google.cloud importspanner>>> client =spanner.Client()

or pass incredentials andproject explicitly

>>> from google.cloud importspanner>>> client =spanner.Client(project='my-project', credentials=creds)

Using a Cloud Spanner Emulator

There are two ways to use the client with a Cloud Spanner emulator: programmatically or via an environment variable.

To programmatically use an emulator, you must specify the project, the endpoint of the emulator, and use anonymous credentials:

from google.cloud importspannerfrom google.auth.credentials import AnonymousCredentialsclient =spanner.Client(    project='my-project',    client_options={"api_endpoint": "0.0.0.0:9010"},    credentials=AnonymousCredentials())

To use an emulator via an environment variable, set the SPANNER_EMULATOR_HOST environment variable to the emulator endpoint:

export SPANNER_EMULATOR_HOST=0.0.0.0:9010

Next Step

After aClient, the nexthighest-level object is anInstance.You’ll need one before you can interact with databases.

Next, learn about theInstance Admin Usage.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-18 UTC.