Articles in this section

Set up JavaScript APIs to run an export

This article demonstrates creating a public integrator.io API to run an existing export.

A. Write the script

  1. From the Resources menu, select Scripts.

  2. At the right, click + Create script.

  3. Name your new script (ex. JavaScript API – export Example).

  4. Write the JavaScript code in the Edit content field.

    • The script must have at least one entry-point function – _mainExport() below.

    • It must handle any errors.

    • In this case, it calls exports.run(), passing it the export’s ID, as shown below:

import { exports } from 'integrator-api'

function _mainExport (options) {
  let response = {};
  let invokeExportResponse;
  let exportResponse;
  
  // Execute the export
  try {
    invokeExportResponse = exports.run({_id:'5ef••••••••••••••••••b01'});
    response.statusCode = 200;
  }catch(e) {
    invokeExportResponse = JSON.stringify(e);
    response.statusCode = 400;
  }

  // Create body response
  response.body = {
    exportResponse: invokeExportResponse
  }
 
  return {
    statusCode: response.statusCode,
    headers: { },
    body: response.body
  }
}
  • Save the script.

B. Create the JavaScript API

  1. Navigate to ResourcesAPIsJavaScript.

  2. Click + Create API at the upper right.

  3. Enter a name (ex. Demo – export).

  4. Select the script saved in step A (JavaScript API – export).

  5. Click pencil.svg to open the Script editor and copy the function name ( _mainExport ). Paste the name into the Function field.

  6. Click Save & close.

C. Get an API token

  1. Navigate to ResourcesAPI token.

  2. Click + Create API token at the upper right.

  3. Enter a name (ex. Postman JavaScript API).

  4. In the Token permissions section, select Custom scopes.

  5. From the JavaScript APIs list, select the JavaScript API you created in step B, Demo – export.

  6. Click Save & close.

See full example