Distribution

Scheduled Report Runs

This example shows how to use the API to configure a report to run on a regularly occurring schedule.

Endpoints used

.../{workspace}/reports/{report_token}/schedules
Supported methods:

  • GET: returns all schedules for a given report
  • POST: creates a new schedule

How this example works

In this example, the create_scheduled_report_run method makes a POST request to app.mode.com/api/{workspace}/reports/{report_token}/schedules, creating a schedule. You can specify the time interval and parameter values in the payload.

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 create_scheduled_report_run(report_token):
  #create schedule and set up parameters the schedule should be run with
  url = '%s/api/%s/reports/%s/schedules' % (host, ws, report_token)
  headers = {'Content-Type': 'application/json'}
  payload = {'report_schedule': {
              'name': 'Qui Officia',
              'cron':{
                'freq': 'daily',
                'hour': 14,
                'time_zone': 'UTC',
                'day_of_week': 1,
                'day_of_month': 1,
                'minute': 0
              },
              # params optional
              'params':{
                'foo': 'bar'
               },
              'timeout':900
             },
             'trk_source':'report'
            }
  r = requests.post(url, headers=headers, auth=HTTPBasicAuth(un, pw), json=payload)
  return r.status_code


create_scheduled_report_run('report_token')
const request = require('request-promise');

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

const createScheduledReportRun = async reportToken => {
  // create schedule and set up parameters the schedule should be run with
  const payload = {
    report_schedule: {
      name: 'Qui Officia',
      cron: {
        freq: 'daily',
        hour: 14,
        time_zone: 'UTC',
        day_of_week: 1,
        day_of_month: 1,
        minute: 0,
      },
      // params optional
      params: {
        foo: 'bar',
      },
      timeout: 900,
    },
    trk_source: 'report',
  };

  return await request({
    method: 'POST',
    url: `${host}/api/${ws}/reports/${reportToken}/schedules`,
    auth: { username, password },
    json: true,
    body: payload,
  });
};

createScheduledReportRun('reportToken');