Tutorial: Get started with Named Channels using the REST API¶
Note
Where possible, use the Snowpipe Streaming SDK instead of the REST API to benefit from automatic batching and simpler integration. Use direct REST when an SDK isn’t suitable for your environment.
This guide shows you how to stream data into Snowflake using the Snowpipe Streaming REST API and a JSON Web Token (JWT) generated with Snowflake CLI. It covers Named Channel REST ingestion for ordered, exactly-once workloads.
For the Elastic Channel REST path (simpler, recommended for most new applications), see Tutorial: Get started with Elastic Channels (REST).
Prerequisites¶
Before you begin, ensure you have the following items:
Snowflake User and Objects:
A Snowflake user that is configured for key-pair authentication. Register your public key by using the following SQL command:
A Snowflake database, schema, and a target table for streaming ingestion. You can create them by using the following SQL commands and replacing placeholders like MY_DATABASE, MY_SCHEMA, MY_TABLE with the names that you want:
ACCOUNT_IDENTIFIER:
We suggest that you use Format 1 for the ACCOUNT_IDENTIFIER, which uses the account name within your organization; for example, myorg-account123. For more information on the format, see Account identifiers.
Installed tools:
curl: For making HTTP requests.jq: For parsing JSON responses.- Snowflake CLI: For generating a JWT. The REST API streams the rows. It doesn’t generate that token, and it doesn’t develop or deploy Snowpark code, Streamlit in Snowflake apps, Snowflake Native Apps, Snowpark Container Services services, notebooks, Git repositories, or dbt and DCM Projects projects.
Generated JWT:
Generate your JWT by using Snowflake CLI:
Caution
Store your JWT securely. Avoid exposing it in logs or scripts.
Prerequisites and setup¶
The following steps configure the environment variables, ingest host, and sample rows required for Named Channel REST ingestion.
Step 1: Set environment variables¶
Set up the necessary environment variables for your Snowflake account and the streaming operation. Note that the PIPE variable targets the default streaming pipe associated with your table.
Step 2: Discover and configure the ingest host¶
Snowpipe Streaming REST uses two hostnames: the Snowflake account endpoint (CONTROL_HOST) and an account-specific ingest endpoint (INGEST_HOST) returned by GET /v2/streaming/hostname.
If you use AWS PrivateLink, Azure Private Link, or Google Cloud Private Service Connect, set CONTROL_HOST to the hostname from the privatelink-account-url value returned by SYSTEM$GET_PRIVATELINK_CONFIG. Don’t include https:// or a trailing slash.
Important
If your Snowflake account name contains underscores (e.g., MY_ACCOUNT), a known issue can cause an internal error when calling the ingestion service.
You must replace all underscores with dashes in the INGEST_HOST before generating the scoped token. This converted format (with dashes) must be used for all subsequent REST API calls, including the generation of the scoped token itself.
For example, if the hostname returned is my_account.region.ingest.snowflakecomputing.com, you must change it to my-account.region.ingest.snowflakecomputing.com for all subsequent REST API calls.
The ingest host is the endpoint for streaming data. Discover the ingest host by using your JWT:
Configure private DNS for the ingest host¶
Complete the shared network connectivity prerequisites before continuing. They cover public and private connectivity, private DNS for the discovered ingest hostname, firewall inventory, and checks from the application runtime. For private connectivity, route the normalized INGEST_HOST through the existing Snowflake private endpoint; a second Snowflake service private endpoint isn’t required.
Obtain a scoped token to authorize operations on the ingest host:
Step 3: Create sample rows¶
Create a batch in newline-delimited JSON (NDJSON) format:
Open and use a Named Channel¶
Use a Named Channel when you require ordering or exactly-once recovery. Complete Steps 1 through 3 in Prerequisites and setup, then set the Named Channel variables:
Step 4: Open the Named Channel¶
Step 5: Append rows with offset and continuation tokens¶
Use the continuation token returned by the open operation and an application-managed source offset:
Use the next_continuation_token from each append response in the next append request.
Step 6: Verify committed progress¶
Wait until last_committed_offset_token is 1 before advancing the source offset.
Step 7: Verify the data¶
After last_committed_offset_token advances, query the target table:
(Optional) Step 8: Clean up¶
Troubleshooting¶
- HTTP 401 (Unauthorized): Verify that your JWT token is valid and not expired. If needed, regenerate it.
- HTTP 404 (Not Found): Double-check that the database, schema, table, or pipe names are spelled correctly and exist in your Snowflake account.
- HTTP 429 (Too Many Requests): Retry using exponential backoff with random variation in retry delays (jitter). Don’t assume a fixed reserved request rate.
- No Ingest Host: Ensure your control plane host URL is correct and accessible.
Private connectivity troubleshooting¶
For unresolved account or ingest hostnames, HTTPS timeouts, or TLS hostname mismatches, use the commands and symptom table in Network connectivity prerequisites. Test from the application runtime, not only a developer laptop.