Management

Rename Report

Similar to the “Move a report” example, this example shows how to update a report title using the API. You can use this code to bulk update report titles. For example, you may need to do this when you clone a report used for a White-Label Embed and make some edits or updates to the cloned version. In this case, you can change the report title and move it back to a production Collection using the API.

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}/reports/{report_token}
Supported methods:

  • GET: returns a report
  • POST: creates a new report run
  • PATCH: changes report’s Collection, description, title, or layout

How this example works

In this example, the update_report_title method first makes a GET request to app.mode.com/api/{workspace}/reports/{report_token}. This request returns information about a report (title, description, layout, etc.), which are used in the payload of a PATCH request to the same endpoint later in the method. You can use this same method to update other report elements, such as its description or layout.

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 update_report_title(report_token, report_name):
  url = '%s/api/%s/reports/%s' % (host, ws, report_token)
  headers = {'Content-Type': 'application/json'}
  r = requests.get(url, auth=HTTPBasicAuth(un, pw))
  result = r.json()
  layout = result['layout']
  account_id = result['account_id']
  desc = result['description']
  published = result['_forms']['edit']['input']['report']['published']['value']
  payload = {'report':{'name':report_name,'layout':layout,'description':desc,'account_id':account_id,'published':published}}
  result = requests.patch(url, auth=HTTPBasicAuth(un, pw), json=payload, headers=headers)
  return result

update_report_title('report_token', 'new_report_title')
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 updateReportTitle = async (reportToken, reportName) => {
  const url = `${host}/api/${ws}/reports/${reportToken}`;
  const report = await request({
      url,
      auth: { username, password },
      json: true,
    });

  const {
    layout,
    account_id,
    description,
    _forms,
  } = report;

  const published = _forms.edit.input.report.published.value;
  const payload = {
    report: {
      name: reportName,
      layout,
      description,
      account_id,
      published,
    },
  };

  return await request({
    url,
    method: 'PATCH',
    auth: { username, password },
    json: true,
    body: payload,
  });
};

updateReportTitle('reportToken', 'reportName');