Management

Get All Reports

This example shows how to use the Mode API to get information about all the reports in your Workspace. Workspace admins can use the API to retrieve all reports in public Collections and in private Collections of which they’re a member (but not in other members’ personal Collections or private Collections of which they’re not a member). This example can help you run admin analytics on your org’s reports usage, gathering data like report name and creators, schedules, users subscribed to a schedule, and so on.

Note: Spaces have been rebranded as Collections in Mode, but the API will continue to refer to spaces. The spaces object and resources are the same as Collections in the Mode UI.

Endpoints used

.../{workspace}/spaces?filter=all
Supported method:

  • GET: returns all Collections in your org, including the Collections an admin has not joined

.../{workspace}/spaces/{space_token}/reports?page=1
Supported method:

  • GET: returns all reports in a Collection. This result is paginated and shows 30 reports per page

How this example works

In this example, the get_all_reports_tokens method first makes a GET request to app.mode.com/api/{workspace}/spaces?filter=all to return a list of Collections in the Workspace. Workspace admins are able to access all public Collections and private spaces they’ve joined. Once we get the space token for each Collection, we then loop through each space token and make a GET request to app.mode.com/api/{workspace}/spaces/{space_token}/reports?page=1 to get all reports in each Collection. Since this endpoint result is paginated and shows 30 reports per page, we create a while loop to send GET request to each endpoint, breaking the while loop when the page returns less than 30 reports. This example returns report tokens that allow you to make subsequent GET requests to app.mode.com/api/{workspace}/reports/{report_token} to get further information about a report.

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://app.mode.com'
ws = 'workspace_name' # Note: workspace_name value should be all lowercase
un = 'api_token'
pw = 'api_secret'

def get_all_reports_tokens():
  url = '%s/api/%s/spaces?filter=all' % (host, ws)
  r = requests.get(url, auth=HTTPBasicAuth(un, pw))
  result = r.json()
  spaces = result['_embedded']['spaces']
  space_tokens = [s['token'] for s in spaces]
  all_tokens = []
  for s in space_tokens:
    page = 1
    report_tokens = make_request(s, page)
    all_tokens += report_tokens
    while(len(report_tokens)==30):
      page += 1
      report_tokens = make_request(s, page)
      all_tokens += report_tokens

  return all_tokens


def make_request(space_token, page):
  url = '%s/api/%s/spaces/%s/reports?page=%d' % (host, ws, space_token, page)
  r = requests.get(url, auth=HTTPBasicAuth(un, pw))
  results = r.json()
  reports = results['_embedded']['reports']
  report_tokens = [r['token'] for r in reports]
  return report_tokens


get_all_reports_tokens()
const request = require('request-promise');

const host = 'https://app.mode.com';
const ws = 'workspaceName'; // Note: workspaceName value should be all lowercase
const username = 'apiToken';
const password = 'apiSecret';

const getAllReportsTokens = async () => {
  const url = `${host}/api/${ws}/spaces?filter=all`;
  const response = await request({
    url: url,
    auth: { username, password },
    json: true,
  });
  const spaces = response._embedded.spaces;
  const spaceTokens = spaces.map((space) => space.token);
  let allTokens = [];

  let page = 1;
  let tokenIndex = 0;
  do {
    let reportTokens = await makeRequest(spaceTokens[tokenIndex], page);
    allTokens = allTokens.concat(reportTokens);

    while (reportTokens.length === 30) {
      page++;
      reportTokens = await makeRequest(spaceTokens[tokenIndex], page);
      allTokens = allTokens.concat(reportTokens);
    }

    tokenIndex++;
  } while (tokenIndex < spaceTokens.length);
};

const makeRequest = async (spaceToken, page) => {
  url = `${host}/api/${ws}/spaces/${spaceToken}/reports?page=${page}`;
  const response = await request({
    url: url,
    auth: { username, password },
    json: true,
  });
  const reports = response._embedded.reports;
  const reportTokens = reports.map((report) => report.token);

  return reportTokens;
};

getAllReportsTokens();
# README: This script requires that you install the HTTP gem:
#         gem install http

require 'http'
require 'json'

HOST      = 'https://app.mode.com'
WORKSPACE = 'workspace_name' # TODO: change to your Workspace's name
USERNAME  = 'api token'      # TODO: change to your API token
PASSWORD  = 'api secret'     # TODO: change to your API secret

def space_tokens
  url = "#{HOST}/api/#{WORKSPACE}/spaces?filter=all"

  response = HTTP.basic_auth(user: USERNAME, pass: PASSWORD).get(url)
  result   = JSON.parse response

  spaces = result['_embedded']['spaces']
  spaces.map { |space| space['token'] }
end

def report_tokens(space_token: , page:)
  url = "#{HOST}/api/#{WORKSPACE}/spaces/#{space_token}/reports?page=#{page}"

  response = HTTP.basic_auth(user: USERNAME, pass: PASSWORD).get(url)
  result   = JSON.parse response

  reports = result['_embedded']['reports']
  reports.map { |r| r['token'] }
end

def all_reports_tokens
  all_tokens   = []

  # iterate through tokens for all of your spaces
  space_tokens.each do |space_token|
    page = 1
    report_tokens = report_tokens(space_token: space_token, page: page)
    all_tokens += report_tokens

    # iterate through pagination on each
    loop do
      page += 1
      report_tokens = report_tokens(space_token: space_token, page: page)
      all_tokens += report_tokens

      puts all_tokens

      break if report_tokens.length != 30
    end
  end

  all_tokens
end

puts all_reports_tokens