How to Build Data APIs on Snowflake
Tags: snowflake, graphql, restapi
Published: 2023-03-06 11:24:53

---
title: How to Build Data APIs on Snowflake
published: true
date: 2023-02-24 10:28:54 UTC
tags: Snowflake,GraphQL,RESTAPI
canonical_url: https://hasura.io/blog/how-to-build-data-apis-on-snowflake/
---

 ![How to Build Data APIs on Snowflake](https://hasura.io/blog/content/images/2023/02/build-data-apis-snowflake.png)

In this post, we will learn how to use Hasura to rapidly build APIs on top of the Snowflake data to power embedded analytics in mobile/web apps. Using Hasura, we can quickly create secure and performant GraphQL/REST APIs on Snowflake, and then query the API from a React frontend.

## Why APIs?

Snowflake is typically a central data source for the organization that aggregates data from different data silos, internally and externally. With all this rich and valuable information stored in a single place, businesses are looking for ways to directly use this data to power more intelligent and personalized experiences. For example, it is an in-product analytics widget showing users their usage patterns.

Embedding analytics and dashboards powered by your data warehouse directly into user-facing applications requires a secure and performant data serving layer with low latency and high concurrency. Low latency is critical for delivering a snappy user experience. Fine-grained access control is crucial when exposing a data warehouse with sensitive information to user-facing apps. Analytical APIs are the perfect tool to meet these requirements because they provide a decoupled layer to implement your security and performance needs.

Besides surfacing real-time analytics in apps, data APIs are an efficient way of sharing specific controlled views on the data warehouse with internal and external consumers. Employees, customers, and partners can hit a particular API endpoint to get the data they need in a self-serve way without being blocked on manual data-sharing workflows.

While APIs are an excellent method to implement this secure and low-latency serving layer, building APIs the traditional way can be tedious, time-consuming, and require substantial backend service logic. This post shows how to create a performant data API on Snowflake in minutes with Hasura.

## Does Snowflake have an API?

Snowflake's SQL API allows users to execute SQL statements against their data warehouse through a REST API. However, this API doesn't provide additional optimization, abstraction, or security features to support the development of performant, low-latency applications that require complex authorization use cases.

## How do I build an API on top of Snowflake?

Follow these 5 steps to build your first Snowflake API and use it from a React frontend app.

### 0. Deploy Hasura

[Sign up for a forever-free Hasura Cloud account](https://cloud.hasura.io) and spin up a new project.

![How to Build Data APIs on Snowflake](https://lh3.googleusercontent.com/X5Z0cqCEiyz2trD3EOd5EBfXTLJmNXlTSYXrDVilkAZffR1MgwJQz_4HBqYZ1qCQbz7ms0GfE7P2Y2jT2UQpddeGGK4uVIsIS0UNVAR7av2_6d1I4cS_h-YiNnfOw709D9ot_3FEszil9Q2e__975Kc)

### 1. Connect your Snowflake Data Source:

To set up a Snowflake Data Source for Hasura, follow our [Snowflake database guide](https://hasura.io/docs/latest/databases/snowflake/getting-started/cloud/). Enter your Snowflake JDBC connection details in Hasura’s console as shown below,

![How to Build Data APIs on Snowflake](https://lh5.googleusercontent.com/LGpTh9dX8WRe7BouGMBFslbUjbT_L_nWfxOrBUplm9eeMyZZdEnBi30qen6cfKIJ8JaEISqkkIQirDf03eKllgTZYPYV-Al1afcZahI0IDqQUanyCzyFaq39uJXYZEihJyGFwvl6YrBWDuhx9abqTlE)

Make sure to use CLIENT\_SESSION\_KEEP\_ALIVE=TRUE param in JDBC connection URL. Snowflake closes JDBC connections after a while (max: 4 hours). This param will ensure the connection is kept alive and bypass the restriction.

### 2. Track tables from Snowflake

Once connected, you can see your Snowflake tables in Hasura. You can select all the tables you want to be able to query using Hasura and click on Track Selected.

![How to Build Data APIs on Snowflake](https://lh5.googleusercontent.com/3w2befByPwviJkMU2zb-WPD5zYk8e64mB5b7fd1nWKCINDxyh-cx7zMoNmjGldsTHHaWSPCQtPo9fQAldG87Xz9C8vFsL8BjuPn_-cxbbAIJpZb6JuSaoCu1PImg8D3KznZD7-UVtY_5Bmpnm9KfErY)

### 3. Query your Snowflake API

Once you connect and track tables, Hasura instantly gives you a GraphQL API on top of Snowflake that you can immediately use to start querying the data.

At the bottom-right of the GraphQL API tab, you'll see the quick response times Hasura' 's high-performance GraphQL API provides. In this example, computing the average temperature over a few thousand records takes about 530 milliseconds.

Note these queries are running on Snowflake directly without replicating the data in a different datastore.

![How to Build Data APIs on Snowflake](https://lh5.googleusercontent.com/meO5RHcLQzNVNACn84-UiYW5iWHfUbnTY5eG58_rlQJQqWRUfZyMBXxYpV8bCFzrIsNcDQD2nIbefd-rstWDG6XpFfpr_AzuDFtP7Kz-4CDsyEUiZSbvuw2XONpxNbfXpvfNsJoghVHvZkUuH_j4qXQ)

### 4. (Optional) Cache your Snowflake API

You can use the @cached directive to your query to decrease your response time. The default TTL is 60 seconds, reducing latency by about 200 milliseconds.

Caching is useful when querying Snowflake, as latencies could be a bottleneck, primarily when serving customers on web/mobile applications. If the same data is frequently fetched, caching can improve latencies and reduce the load on data warehouse resources.

![How to Build Data APIs on Snowflake](https://lh4.googleusercontent.com/KVhulwpdQzhjTV3fkPspFkpdHDzEGPcA6LxYf6oAETypWO8bS9FsavET4Cy67dUnxAlAam8pUKzJW5s7_G4PlP3jvOoDmOaB4D___IfCaPfREPUtv_gIcDUR5-lCp0GiofGK9shurqrJhK2ArNhImCs)

### 5. Query your Snowflake API with React  

We will make a chart using **graphql2chartjs** to reshape your GraphQL data per the ChartJS API. This makes it easy to query a GraphQL API and render the output as a ChartJS chart.

You can install it via:

<!--kg-card-begin: markdown-->

`npm install --save graphql2chartjs`

<!--kg-card-end: markdown-->

Post that, you can create a bar chart component as an example,

<!--kg-card-begin: markdown-->
```
import {Query} from 'react-apollo';
import gql from 'graphql-tag';
import graphql2chartjs from 'graphql2chartjs';
import {Bar} from 'react-chartjs-2';
const Chart = () => (
  <Query
    query={gql`
      query {
        WEEKLY_TEMPRATURE(order_by: {week: asc}) {
          label: week
          data: TEMPRATURE
        }
      }`}
    }>
    {({data} => {
 if (data) {
        const g2c = new graphql2chartjs(data, 'bar');
        return (<Bar data={g2c.data} />);
      }
      return null;
    }
  </Query>
);

```
<!--kg-card-end: markdown-->

You can use this component in your react application to render charts like the one below; it uses NYC weather data over the last six years.

![How to Build Data APIs on Snowflake](https://lh5.googleusercontent.com/aMpsJiY2i8LvVFNB7nFEzKM_9GEfSrzAd7-d0dUGvUrDrOZFG7WQqAf-_iw_Z5eNnFbC-8reOivw8Uj0b4GgSQPXWqbMYNdzRLxNTpAWswaeWWi84_pN6RYKyTvAAFxtqM2HZ49XK8KXCJHJlYnHK5w)

## Conclusion

This post taught us three easy steps to create an API over Snowflake and then call that API from a React frontend. In a follow-up post, we will learn how easy it is to add access control to your APIs using Hasura.

Hasura, unlike other solutions, doesn’t require you to maintain a copy of data, and it generates SQL queries based on your request; it also provides a way to cache and rate limit same time while providing authentication and authorization options to safeguard your data.

You can try out our Snowflake (beta) support on Hasura [Cloud](https://cloud.hasura.io/).