There are various methods using which we can get data from snowflake. Either it is to satisfy business needs or to get data for custom scripts in CLI, snowflake does a great job. Today we are going to learn how to query data from snowflake using custom curl statements and Postman. Creating Custom API calls in postman are fairly simple. That's why we are going to cover both Postman and Curl.
CURL stands for Client URL. It is a command line tool/library that allows us to transfer data over network protocols. It supports various protocols such as HTTP, HTTPS, FTP, SMB, etc. We can send custom requests such as GET, POST, PUSH, DELETE, etc.
We are going to use Oauth to get data from snowflake. Here is the detailed Guide on how to set up Oauth in Snowflake. After successful configuration of Oauth in Snowflake we should be having the following parameters:
CLIENT_ID
CLEINT_SECRET
REDIRECT_URI
ACCESS_TOKEN_URL
OAUTH_REDIRECT_URI
Next we need a code to get our refresh token. Please use this link and follow the steps till Step - 2 to get code.
Before proceeding further please make sure that we are having all prerequisites mentioned above along with the code.
Curl uses fairly simple syntax. We will be using the following arguments:
--user <client_id:client_secret> (used for Basic AUTH)
-H (to specify header)
--data-urlencode (to specify additional arguments)
and finally we required a URL to send a request i.e. ACCESS_TOKEN_URL.
curl -X POST -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" \
--user "CLIENT_ID not encoded>:<CLIENT_SECRET>" \
--data-urlencode "grant_type=authorization_code" \
--data-urlencode "code=<code from above step>" \
--data-urlencode "redirect_uri=<OAUTH_REDIRECT_URI not encoded>" \
<ACCESS_TOKEN_URL>
In our case, the request will look something like this:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" \
--user "XXXXXXXXXXXXXXXXXXXLBQh8=:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXT7+Yw=" \
--data-urlencode "grant_type=authorization_code" \
--data-urlencode "code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXB92A019F1EA" \
--data-urlencode "redirect_uri=http://localhost/" \
https://XXXXXXX.ap-southeast-1.snowflakecomputing.com/oauth/token-request
NOTE: If you get a curl error in the above syntax in command promprt/powershell then remove all the '\' and write the entire command in a single line like this
curl -X POST -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" --user "XXXXXXXXXXXXXXXXXXXLBQh8=:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXT7+Yw=" --data-urlencode "grant_type=authorization_code" --data-urlencode "code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXB92A019F1EA" --data-urlencode "redirect_uri=http://localhost/" https://XXXXXXX.ap-southeast-1.snowflakecomputing.com/oauth/token-request
We will get output something like this
{
"access_token" : "ver:1-hint:1111XXXXXXXXXXXXXXXXXX",
"refresh_token" : "ver:2-hint:1111320986XXXXX VERY LONG REFRSH TOKEN HERE",
"token_type" : "Bearer",
"username" : "TOMAR",
"scope" : "refresh_token session:role:SYSADMIN",
"refresh_token_expires_in" : 7775999,
"expires_in" : 599,
"idpInitiated" : false
}
NOTE : Refresh token has a lifespan of 7775999 seconds (90 days approx). After 90 days we need to repeat the above steps again to get a new refresh token. We can specify "refresh_token_expires_in" while configuring Oauth in Snowflake. Minimum value can be 1 Hour and Maximum value can be 90 days.
In Oauth we need access token so that we can query data from snowflake. But we got access token with refresh token in step above . Can't we use that access token? Well, technically we can use that but snowflake's access token has a lifespan of just 599 secs (10 mins approx). So instead of repeating the entire process every 10 mins, we can use a refresh token. We can generate a new access token via refresh token.
We can create a custom API call to generate a new access token via refresh token.
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \
--user "CLIENT_ID not encoded>:<CLIENT_SECRET>" \
--data-urlencode "grant_type=refresh_token" \
--data-urlencode "refresh_token="<REFRESH_TOKEN>" \
<ACCESS_TOKEN_URL>
In our case, the request will look something like this:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \
--user "XXXXXXXXXXXXXXXXXXXLBQh8=:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXT7+Yw=" \
--data-urlencode "grant_type=refresh_token" \
--data-urlencode "refresh_token=ver:2-hint:1111320986XXXXX VERY LONG REFRSH TOKEN HERE" \
https://XXXXXXX.ap-southeast-1.snowflakecomputing.com/oauth/token-request
NOTE: If you get curl error in above syntax in cmd then remove all '\' and write entire command in one line like this
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" --user "XXXXXXXXXXXXXXXXXXXLBQh8=:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXT7+Yw=" --data-urlencode "grant_type=refresh_token" --data-urlencode "refresh_token=ver:2-hint:1111320986XXXXX VERY LONG REFRSH TOKEN HERE" https://XXXXXXX.ap-southeast-1.snowflakecomputing.com/oauth/token-request
We will get output something like this
{
"access_token" : "ver:1-hint:XXXXX ACCESS TOKEN",
"token_type" : "Bearer",
"expires_in" : 600,
"idpInitiated" : false
}
We can generate curl statement that can query data from snowflake. If our Oauth access token expires, we need to get a new access token again before proceeding. I recommend using a Linux shell to run this request.
curl --location --request POST 'https://XXXXXX.ap-southeast-1.snowflakecomputing.com/api/v2/statements/' \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
--data-raw '{
"statement":"SQL QUERY HERE",
"timeout": 60,
"resultSetMetaData": {"format":"json"},
"database":"DATABASE",
"schema":"SCHEMA",
"warehouse":"COMPUTE_WH"
}'
In our case, the request will look something like this:
curl --location --request POST 'https://XXXXXXX.ap-southeast-1.snowflakecomputing.com/api/v2/statements/' \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ver:1-hint:<Access token>" \
--data-raw '{
"statement":"select * from customer limit 5",
"timeout": 60,
"resultSetMetaData": {"format":"json"},
"database":"DB",
"schema":"RAW",
"warehouse":"COMPUTE_WH"
}'
We will get the output of the query in JSON format as a result.
Postman is a platform for API Development. It enables us to explore, debug and test our API endpoints. It supports various types of HTTP requests such as GET, POST, PUT, PATCH. It can also convert the API to code for languages like Python, JS etc.
We are going to use Oauth to get data from snowflake. Here is the detailed Guide on how to setup Oauth in Snowflake. After successful configuration of Oauth in Snowflake we should be having the following parameters:
CLIENT_ID
CLEINT_SECRET
REDIRECT_URI
ACCESS_TOKEN_URL
OAUTH_REDIRECT_URI
The entire process in Postman is very simple. Open a new tab in Postman and follow the steps below:
Click on "Authorization"
Select OAuth 2.0
Fill all necessary details (find here how to get those details)
Click "Get New Access Token"
A browser pop-up will open. Fill in your credentials and click "Sign in"
After successful sign in you will be propted in postman like this.
NOTE : If you get an "Invalid consent request" error while logging in, make sure that user has permission to operate on the Storage Integration Object. Also the user must not be an ACCOUNTADMIN, ORGADMIN or SECURITYADMIN. These roles are by default added to BLOCKED_ROLES_LIST.
After successfully signing in, you will be prompted in postman as given below
We will get our Access token and refresh token. Click on Use Token.
After clicking on "Use Token". Fill all necessary details as shown in Picture and Click "Send".
We will get the response as JSON format where the entire data will be in the "data" array.
We may also get response like this:
If we get the above error we need to generate a new access token by using refresh token.
After copying Refresh token paste it somewhere safe. Then we need to open a new tab in postman again and follow steps as given below:
After clicking "Send", we will get our access token.
Copy that access token and go to the Previous tab(where we are querying data from SF) . Copy access token in Authorization field and click "Send".
NOTE: Access Token will expire in 10 mins so if we need to get data after 10 mins then we will require to get a new access token every time via refresh token.