Get Query Results

This example demonstrates how to get a query’s results in JSON format. This can be useful when you’ve got multiple queries in a report, but you want to work with the results of just one of them, whether to do further analysis, build custom visualizations, or just generally access them outside of Mode.

Endpoints used


Supported method:

  • GET: returns a query’s historic runs


  • GET: returns a query run’s results in JSON

How this example works

In this example, the get_query_result method first makes a GET request to{organization}/reports/{report_token}/queries/{query_token}/runs to return all of a query’s historic runs. It then checks the first run on the returned list to see if the latest run succeeded. If so, we call the get_run_result method to get that run’s results by sending a GET request to{organization}/reports/{report_token}/runs/{run_token}/query_runs/{query_run_token}/results/content. If the run failed, it prints out a message indicating the failed state.

To try it yourself

  • Download the example code to a .py file
  • Input your API token, API secret, organization name, and other custom information as needed. An API token and secret can be generated here
  • Save the .py file in your desired directory
  • Open the terminal and navigate to that directory
  • Run the command python3 {file_name}.py
  • All of our Python recipes require Python 3
import json
import requests

from requests.auth import HTTPBasicAuth

host = ''
org = 'org_name'
un = 'api_token'
pw = 'api_secret'

def get_query_result(report_token, query_token):
  url = '%s/api/%s/reports/%s/queries/%s/runs' % (host, org, report_token, query_token)
  r = requests.get(url, auth=HTTPBasicAuth(un, pw))
  result = r.json()
  if result['_embedded']['query_runs'][0]['state'] == 'succeeded':
    query_run_result_endpoint = result['_embedded']['query_runs'][0]['_links']['result']['href']
    print ('There is no results, because query run failed.')

def get_run_result(query_run_result_endpoint):
  url = '%s%s/content' % (host, query_run_result_endpoint)
  r = requests.get(url, auth=HTTPBasicAuth(un, pw))
  return r.contents

#report token is the string at the end of a report view URL
#query token is the string at the end of the URL at report view -> View Details -> SQL
get_query_result('report_token', 'query_token')
const request = require('request');

const host = '';
const org = 'orgName';
const username = 'apiToken';
const password = 'apiSecret';

const getQueryResult = (reportToken, queryToken) => {
    url: `${host}/api/${org}/reports/${reportToken}/queries/${queryToken}/runs`,
    auth: { username, password },
    json: true,
  }, (err, res, body) => {
    const queryRun = body._embedded.query_runs[0];
    const queryRunResultEndpoint = queryRun._links.result.href;

    if (queryRun.state === 'succeeded') {
        url: `${host}${queryRunResultEndpoint}/content`,
        auth: { username, password },
        gzip: true,
      }, (err, res, body) => body);
    } else console.error('There is no results, because query run failed.');

// report token is the string at the end of a report view URL
// query token is the string at the end of the URL at report view -> View Details -> SQL
getQueryResult('reportToken', 'queryToken');