Reports

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

.../{workspace}/reports/{report_token}/queries/{query_token}/runs

Supported method:

  • GET: returns a query’s historic runs

.../{workspace}/reports/{report_token}/runs/{run_token}/query_runs/{query_run_token}/results/content

  • 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 app.mode.com/api/{workspace}/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 app.mode.com/api/{workspace}/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 the API token, API secret, Workspace name, and other custom information as needed. Follow these steps to create an API token.
  • 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 = 'https://modeanalytics.com'
ws = 'workspace_name' # Note: workspace_name value should be all lowercase
un = 'api_token'
pw = 'api_secret'

def get_query_result(report_token, query_token):
  url = '%s/api/%s/reports/%s/queries/%s/runs' % (host, ws, 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']
    return get_run_result(query_run_result_endpoint)
  else:
    print ('There is no results, because query run failed.')

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

#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 = 'https://modeanalytics.com';
const ws = 'workspaceName'; // Note: workspaceName value should be all lowercase
const username = 'apiToken';
const password = 'apiSecret';

const getQueryResult = (reportToken, queryToken) => {
  request({
    url: `${host}/api/${ws}/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') {
      request({
        url: `${host}${queryRunResultEndpoint}/content`,
        auth: { username, password },
        gzip: true,
      }, (err, res, body) => body);
    } else console.error('There is no result, because the 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');