Connect to Snowflake from the SDK¶
There are two distinct connection problems in Cortex Sandboxes, and they have different answers:
- Your driver, outside the sandbox, needs credentials to call the Cortex Sandboxes API and create sandboxes. That is the first half of this page.
- Your code, inside the sandbox, needs to connect back to Snowflake to query data. That is In-sandbox connections at the end, and it works differently: you supply nothing, because the platform provides the credential.
For a first run, the quickstart sets up a named connection with snow connection add. Come here when you need a specific connection, several connections in one process, or raw credentials.
How the SDK resolves a connection¶
If you name nothing, the SDK resolves a default from, in order:
- The default connection in
~/.snowflake/connections.toml, the same file thesnowCLI uses. SNOWFLAKE_DEFAULT_CONNECTION_NAME, ordefault_connection_namein~/.snowflake/config.toml, to choose which entry is the default.- The
SNOWFLAKE_ACCOUNTandSNOWFLAKE_PATenvironment variables.SNOWFLAKE_PATis read first;SNOWFLAKE_TOKENis accepted as a fallback for an OAuth session token.
The SDK also reads SNOWFLAKE_HOST, in place of SNOWFLAKE_ACCOUNT when you have a full hostname, and SNOWFLAKE_HOME when resolving connections.toml.
With nothing set, the error names what to do:
If you name a connection that cannot be resolved, the error names that instead: snowflake.sandbox could not resolve connection '<name>' from ~/.snowflake/connections.toml.
Supply credentials¶
Use any one of the following.
With the Snowflake CLI¶
Run snow connection add, then snow connection set-default <name>. This writes ~/.snowflake/connections.toml, which the SDK reads for the default connection. For a walkthrough, see the Cortex Sandboxes quickstart.
With environment variables¶
No CLI required:
A programmatic access token is what most callers have.
Per call, with connection=¶
Pass connection= to use a specific entry from ~/.snowflake/connections.toml:
connection= is accepted by every entry point that talks to the API, not just create(): Sandbox.connect(), Sandbox.list(), get_sandbox(), and destroy_sandbox() all take it. The handle carries it onward, so calls on a sandbox you created or reconnected to use the same connection without naming it again.
For a block, with using()¶
When several calls should share one connection, name it once for the block:
using() is a convenience, not a requirement: each of those calls would also accept connection="my-conn" directly. It binds the connection to the block it wraps, so two threads or async tasks can hold different connections at once.
By default it leaves the session pooled for the next call. Pass close=True to release it at block exit instead, which is what you want for a one-off connection a long-lived process will not touch again:
close=True only closes a connection the SDK itself opened. A live connection or Snowpark Session you passed in stays yours.
With raw credentials¶
Pass a Config when you hold an account and token directly rather than a named entry:
From inside Snowflake¶
In a Streamlit in Snowflake app, a notebook, or a stored procedure you hold a Snowpark Session rather than a connection. Pass it directly:
Note
An earlier global configure() function has been removed. It stored credentials in a process-wide slot and its per-role slots could silently collide. Calling it now raises NotImplementedError naming the replacement.
In-sandbox connections¶
Code running inside a sandbox does not need credentials configured. The platform provides them, and get_snowflake_connection() resolves whichever mechanism your deployment used:
Prefer this for in-sandbox code, production included. It resolves the credential in order: SNOWFLAKE_PAT or SNOWFLAKE_TOKEN in the environment; a token file named by SNOWFLAKE_TOKEN_FILE_PATH, else /snowflake/session/token, together with SNOWFLAKE_ACCOUNT; then a [default] entry in the platform-rendered $SNOWFLAKE_HOME/connections.toml. Deployments differ in which of those they set, so code that reads only one of them fails where a credential is in fact present.
It also supplies three things that are easy to miss:
SNOWFLAKE_ACCOUNT_URLas a bare host. That variable carries a URL scheme and the connector wants a hostname, so passing it through unchanged fails.rolefromSNOWFLAKE_ROLEandwarehousefromSNOWFLAKE_WAREHOUSE. Withoutrole, the connector falls back to your default role, which may not be the role the sandbox was created with.- A default
login_timeout, so a sandbox with closed egress fails fast instead of hanging.
The helper is for in-sandbox use only, because the credential it reads exists only inside a sandbox. It also refuses keyword overrides that would redirect the credential (account, host, token), raising ValueError. Non-credential options pass through:
The token the platform provides is a placeholder, not your account token, so the raw credential is not exposed to your code. Code in the sandbox can still make any Snowflake request your identity is authorized to make.
When you need to customize the connection¶
Most customization does not require building the connection yourself. get_snowflake_connection() forwards any non-credential option to snowflake.connector.connect(), so reach for the helper first:
If you must construct the connection directly, strip the scheme from the host. SNOWFLAKE_ACCOUNT_URL carries a URL scheme, and the connector wants a bare hostname:
You don’t need to set up TLS trust. The sandbox already trusts the egress-proxy CA, so a connection you open with snowflake.connector.connect() verifies without extra configuration. The CA variables, such as REQUESTS_CA_BUNDLE and SSL_CERT_FILE, are reserved, so you can’t set them with env= when you create the sandbox.
Pass role=os.environ.get("SNOWFLAKE_ROLE") as well, or the connector uses your default role rather than the role the sandbox was created with.
Read every injected variable with .get() unless you know your deployment sets it. The platform skips an empty value rather than injecting KEY=, which reads as “set to empty” to in-sandbox code, so any of them can be absent.
Because the helper handles these details for you, prefer get_snowflake_connection() and pass your options to it.
Note
/snowflake/session/token is a backwards-compatibility symlink rather than the bind mount itself. The platform bind-mounts /snowflake/secrets and the token lands at /snowflake/secrets/snowflake-session/token. Reading either path is fine, and the platform points connections.toml at the symlink.