# Snowflake

> Source: https://truto.one/docs/integration-guides/snowflake/

## Install Snowflake integration

The Snowflake integration can be found [here](https://app.truto.one/integrations/available/60fabe44-0c0a-4647-8ffa-c5c3f0e09952). Please install it in your environments before proceeding with the steps below.

## Connecting a Snowflake account

1. Click on `Connect Account`
   !['Click connect account'](https://docs-assets.truto.one/truto-connect-account.png)
2. Type in a value for the Tenant ID. This can be an ID of your choosing. And then click on `Get connection link`
   !['Get connection link'](https://docs-assets.truto.one/truto-connect-account-modal.png)
3. A new link will be generated. Clicking on the link will open a new tab with the list of integrations. You can share this link with your customers too and they will be able to connect their Snowflake account.
   !['Share connection link'](https://docs-assets.truto.one/truto-connect-account-link.png)
4. Select Snowflake
   !['Select Snowflake'](https://docs-assets.truto.one/select-snowflake.png)

### Finding your Account URL, Account Locator, Client ID and Client Secret

#### Prerequisites for Snowflake Integration

   ::: warning
   - You need to be an `ACCOUNTADMIN` for creating an OAuth application in Snowflake.
   :::

You'll now be prompted to add the Account URL, Account Locator, Client ID and Client Secret.

#### Finding Account URL and Account Locator

1. Sign in to your Snowflake Account.
2. In the left navigation bar at bottom, you will find your `Account URL` and `Account Locator` as shown below.
   !['Account URL and Account Locator'](https://docs-assets.truto.one/account-url-snowflake.png)
3. Copy and enter it in Truto interface.

#### Create your Client ID and Client Secret

1. To create your Client ID and Client Secret, Go to `Worksheets` from left navigation bar.
   !['Snowflake worksheet'](https://docs-assets.truto.one/worksheet-snowflake.png)
2. Click on the `Add` button and select `SQL Worksheet` as shown below.
   !['Add SQL worksheet'](https://docs-assets.truto.one/add-worksheet-snowflake.png)
3. Make sure that the `Database` is `Snowflake` and the `Schema` is `Account_Usage`.
   !['Select DataBase and Schema'](https://docs-assets.truto.one/select-db-snowflake.png)
4. Now execute below commands in Worksheet to create your OAuth application.
```
CREATE ROLE TRUTO_ROLE;
```
   ::: tip
   - You can create any Role name by replacing `TRUTO_ROLE` above. But it should be replaced in all the following commands as well.
   :::
```
CREATE SECURITY INTEGRATION
         Truto
         TYPE = OAUTH
         OAUTH_CLIENT = CUSTOM
         OAUTH_CLIENT_TYPE = 'CONFIDENTIAL'
         OAUTH_REDIRECT_URI = 'https://api.truto.one/connect/snowflake/callback'
         ENABLED = TRUE
         PRE_AUTHORIZED_ROLES_LIST = ( 'TRUTO_ROLE' )
         OAUTH_ISSUE_REFRESH_TOKENS = TRUE
         OAUTH_REFRESH_TOKEN_VALIDITY = 7776000
```
   ::: warning
   - `OAUTH_REFRESH_TOKEN_VALIDITY` can be set to maximum value of 7776000 seconds (90 Days). After this the integration should be re-authenticated.
   :::
   - If you need to raise the maximum value for Refresh Token's validity upto one year, refer to this [link](https://community.snowflake.com/s/article/FAQs-Snowflake-OAuth#:~:text=Could%C2%A0the%20Refresh%20Tokens%27%20validity%20be%C2%A0increased%20beyond%20the%20Maximum%20limit%20in%20special%20circumstances%3F).
5. To view `Client ID` and `Client Secret` run the below command in worksheet.
```
SELECT SYSTEM$SHOW_OAUTH_CLIENT_SECRETS('TRUTO');
```
6. Copy your `OAUTH_CLIENT_ID` and `OAUTH_CLIENT_SECRET` and enter it in Truto interface as `Client ID` and `Client Secret` respectively.
   !['Client ID and Client Secret in Snowflake'](https://docs-assets.truto.one/worksheet-credential-snowflake.png)
7. Now go to `Users & Roles` from left navigation bar as shown below.
   !['Go to Users & Roles'](https://docs-assets.truto.one/user-role-snowflake.png)
8. Go to `Roles` and select `TRUTO_ROLE`.
   !['Select TRUTO_ROLE'](https://docs-assets.truto.one/truto-role-snowflake.png)
9. Now click on more options in `TRUTO_ROLE` and then `Grant to user` as shown below.
   !['Select Grant to user'](https://docs-assets.truto.one/grant-user-snowflake.png)
10. Select a user to grant this role and click on `Grant`.
   !['Grant the role to user'](https://docs-assets.truto.one/user-role-grant-snowflake.png)
11. Now click on more options in `TRUTO_ROLE` again and then `Grant role` as shown below.
    !['Select Grant role'](https://docs-assets.truto.one/assign-role-snowflake.png)
12. Select `ACCOUNTADMIN` and click on `Grant`.
    !['Grant the role to TRUTO_ROLE'](https://docs-assets.truto.one/grant-role-snowflake.png)
13. Now click on name tab on the top left and then `Switch Role` and under `TRUTO_ROLE` select `Set as default` for now.
    ::: tip
    - It is required to complete the OAuth flow with allowed role.You can change it after OAuth flow is completed.
    :::
    !['Change default the role to TRUTO_ROLE'](https://docs-assets.truto.one/change-role-snowflake.png)
14. After filling the data in Truto, click on `Connect`. You'll now be asked to log in to your Snowflake account and allow access to Truto. Go through the steps.

    !['Enter details in Truto and connect'](https://docs-assets.truto.one/details-snowflake.png)
---
5. _Voila!_ You should now see a connection successful message. You can now close this tab.

6. Go to your Truto account and you should see the integration under Integrated Accounts. Click on the Account ID to change or edit the integration to suit your needs.

!['Snowflake Integrated Account'](https://docs-assets.truto.one/account-snowflake.png)

## Uninstalling the Snowflake integration

To uninstall the Snowflake integration from your Truto account, select the Snowflake [integration](https://app.truto.one/integrations/installed?search=Snowflake) from the Integrations tab.

Click on the `Settings` tab in Snowflake Integration page

And then click on Uninstall. Uninstalling the integration will also remove all the Snowflake integrated accounts from your Truto account.

!['Uninstall Snowflake'](https://docs-assets.truto.one/uninstall-snowflake.png)
