Connect to Snowflake with the Snowflake Python API

Before you can perform actions with the Snowflake Python API, you must define a connection to Snowflake. With the connection, you can create a Root object for access to resources modeled by the API.

Specify connection properties

You can define a connection to Snowflake using one of the following mechanisms:

Connect using a Python dictionary

You can specify the values needed to connect to Snowflake by using a Python dictionary. When you connect, you pass this dictionary as an argument to the function or method you’re using to connect:

import os

CONNECTION_PARAMETERS = {
    "account": os.environ["snowflake_account_demo"],
    "user": os.environ["snowflake_user_demo"],
    "password": os.environ["snowflake_password_demo"],
    "role": "test_role",
    "database": "test_database",
    "warehouse": "test_warehouse",
    "schema": "test_schema",
}
Copy

Connect using a configuration file

You can specify connection definitions in a TOML configuration file. This eliminates the need to explicitly define a connection to Snowflake in your code.

For example, create a configuration file located at ~/.snowflake/connections.toml, and add connection parameters similar to the following:

[myconnection]
account = "test_account"
user = "test_user"
password = "******"
role = "test_role"
warehouse = "test_warehouse"
database = "test_database"
schema = "test_schema"
Copy

In this example, you define a Snowflake connection called myconnection with the account test_account, user test_user, password credentials, and database information.

Connection definitions support the same configuration options available in the Snowflake Python Connector.

Connect and create a Root object

Using the connection properties you’ve specified, you can create a connection to Snowflake. With the connection, you can create a Snowflake Python API Root object with which to begin using the API.

You can connect using one of the following objects:

Connect with a Snowpark Session

If you’re using the Snowpark API for Python, you can create a connection to Snowflake by using its snowflake.snowpark.Session object.

Code in the following example uses connection parameters defined in a configuration file to create a connection to Snowflake. Using the resulting Session object, the code creates a Root object from which to use the API:

from snowflake.core import Root
from snowflake.snowpark import Session

session = Session.builder.config("connection_name", "myconnection").create()
root = Root(session)
Copy

For more information about creating a Session, see Creating a Session for Snowpark Python.

Connect with a Python Connector Connection

If you’re using the Snowflake Connector for Python, you can create a connection to Snowflake by using its snowflake.connector.connect function. The function returns a Connection object.

Code in the following example uses connection parameters defined in a configuration file to create a connection to Snowflake. Using the resulting Connection object, the code creates a Root object from which to use the API:

from snowflake.connector import connect
from snowflake.core import Root

connection = connect(connection_name="myconnection")
root = Root(connection)
Copy

For more information about the Snowflake Connector for Python API, see Python Connector API.

Use the Root object

With a Root object created from your connection to Snowflake, you can access objects and methods of the Snowflake Python API. The Root object is the root of the resource tree modeled by the Snowflake Python API. You use the Root object to interact with Snowflake objects represented by the API.

Code in the following example uses the Root object to access Snowflake objects in order to resume the task named mytask. The task is in the schema named myschema, which is in the database named mydb. The code uses the databases, schemas, and tasks methods to get an object that represents this task:

from snowflake.core import Root
from snowflake.core.task import Task

tasks = root.databases["mydb"].schemas["myschema"].tasks
mytask = tasks["mytask"]
mytask.resume()
Copy