Download a Dataset

This page demonstrates a short program that uses the Quantemplate API to download the contents of a dataset

📘

Prerequisites

Before trying this example, make sure you have followed the guide to Setting Up a Connection

Step 1: Configure sharing in-app

In the Quantemplate app, we navigate to the dataset that we want to download. We share the document with the whole organisation in order to give the API access.

Make a note of the IDs in the URL. The important parts are the Organisation ID and the Dataset ID. In the Dataset view, the URL is constructed like so:
app.quantemplate.com/organisation id/dataset/dataset id/preview

2268

Check the URL to get the relevant IDs, and share the dataset with the whole organisation

In this example:

  • Organisation ID is c-examplere-ujm
  • Dataset ID is d-gew-e-k66nb3ptste-5pqfyh

Step 2: Call the API

With our Access Token retrieved from the authentication endpoint, we can then use it to make requests to the API.

In this example, we'll be calling the Download Dataset endpoint. We can see from the API documentation that this URL is parameterised by the Organisation ID and the Dataset ID.

Parameterising the URL with our values above gives us https://fabric.prod.quantemplate.com/external/v1/organisations/c-examplere-ujm/datasets/d-gew-e-k66nb3ptste-5pqfyh

We also need to provide our access token, by setting the Authorization header to Bearer <token>

Code (Python 3)

The code below demonstrates first authenticating with the API, and then using the access token to download a dataset. It requires the requests library.

#!/usr/bin/env python3
import requests

user_id = ... # see Setting Up a Connection
secret = ... # see Setting Up a Connection
org_id = ... # your Organisation ID from Step 1
dataset_id = ... # your Dataset ID from Step 1

def authenticate(user_id, secret):
  return requests.post('https://accounts.prod.quantemplate.com/auth/realms/qt/protocol/openid-connect/token', data = {
    'grant_type': 'client_credentials',
    'client_id': user_id,
    'client_secret': secret
  }).json()

def download(access_token, org_id, dataset_id):
  url = f'https://fabric.prod.quantemplate.com/external/v1/organisations/{org_id}/datasets/{dataset_id}'
  return requests.get(url, headers = {
    'Authorization': f'Bearer {access_token}'
  }).content.decode('utf-8')

auth_result = authenticate(user_id, secret)

csv = download(auth_result['access_token'], org_id, dataset_id)

print(csv)

When we run this program, we get the full contents of the dataset, encoded as CSV, printed to the screen.