Just the Gist: Connect to API, hosted in Snowpark Containers

Venkat Sekar
2 min readJan 29, 2024

Dated: Jan-2024

Key Takeaways:

  • Snowpark Container Services(SPCS) can be used to host data-intensive applications.
  • Rest API-like services can be hosted in SPCS.
  • Demonstrate the gist of connecting to the API endpoint using Snowpark Session.

Overview

Snowpark Container Services(SPCS) provides a managed environment to run containerized applications natively in Snowflake. In my own opinion, the type of applications that are meant to be run in this environment should be primarily focused on data-intensive, where it makes sense to keep the compute / operations close to the data.

Of the different patterns, “Services” is one category which essentially are long-running application. These applications typically have an endpoint like UI or an API via which users or other applications would need to interact.

With SPCS, the authentication is managed by Snowflake. Let’s say a data UI application is running in the SPCS environment, when the user tries to access the application they would be faced with a Snowflake login page. Only after a successful login would the user be able to access the data/content.

This scenario would exist for a REST-API service also, for example, you have an application that is implemented using Fast API that returns data based on query parameters. In such a scenario you would need to write a client program that would need to communicate with the API programmatically.

Gist of connecting to API

The below gist is an example of how to communicate with the API, using Snowpark session. The high-level steps are:

  • Connect to Snowflake, using Snowpark.
  • Set the session parameter for ‘python_connector_query_result_format’ to JSON.
  • Extract the JWT token embedded in the response.
  • Create a request header and add this JWT token with the ‘Authorization’ key
  • Invoke the endpoint.

NOTE: The approach reflected here is a hack/temporary approach and might change in the future. Since there is no official documentation there is always a possibility that Snowflake would come up with a better solution in the future.

Gist at: spcs_api_connect_with_snowpark_session.ipynb

Conclusion

While in the gist I had demonstrated using Snowpark, the approach can be done with Python connector also. I am sure this approach is possible with Java or other languages too, but had not explored them.

--

--