Articles in this section

Build a JavaScript API

Create a JavaScript API when an external application needs to trigger an integrator.io flow, export, or import on demand. To set one up, you write a script, create the JavaScript API object, and generate a scoped API token for authentication.

Prerequisites:

  • Role — You have the Account owner or Administrator role.
  • Script — You have at least one script written and saved in Resources > Scripts.

Write the script

The script contains the entry-point function that runs when a request hits the JavaScript API API endpoint.

  1. Go to Resources > Scripts.
  2. Click + Create script.
  3. Enter a name for the script (ex. JavaScript API – export example).
  4. Write your JavaScript code in the Edit content field. Your script must include at least one entry-point function, handle errors, and return a statusCode, headers, and body. The following is an example that calls exports.run() with an export ID — replace the export ID with your own:

    import { exports } from 'integrator-api'
    
    function _mainExport (options) {
      let response = {};
      let invokeExportResponse;
    
      // Execute the export
      try {
        invokeExportResponse = exports.run({_id:'5ef00000000000000000b01'});
        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
      }
    }
  5. Click Save.

Note: If you modify a script in one location, it is modified everywhere it is used.

Script input and output

The entry-point function receives one options argument and returns a response object.

Object Field Description
options (input) method The HTTP request method, in uppercase.
headers The HTTP request headers, as an object.
queryString The query string parameters, as an object.
body The parsed request body as an object, or undefined if it can't be parsed.
rawBody The unparsed request body, as a string.
Response (output) statusCode The HTTP response status code, as a number.
headers Optional. The HTTP response headers to override, as an object.
body The response body, as a string or an object.

Throwing an exception signals an error.

Create the JavaScript API object

Resources > APIs > JavaScript tab with the + Create API button highlighted.

  1. Go to Resources > APIs.
  2. Select the JavaScript tab.
  3. Click + Create API in the upper right, then select JavaScript.
  4. In the Name field, enter a unique name for the API (ex. Demo – export).
  5. In the Description field, enter a detailed description of how the API works, including any nuances another user should know.
  6. In the Scripts field, select the script you saved in the previous section.
  7. To confirm the exact function name, click the pencil next to the script to open the script editor. Copy the function name (ex. _mainExport), then return to the Create API pane and paste it into the Function field.
  8. If you are receiving requests from ShipWorks, check Enable ShipWorks authentication and complete the Username and Password fields.
  9. Click Save & close.

Get an API token

You must create an API token and scope it to your new JavaScript API before external applications can call it.

  1. Go to Resources > API tokens.
  2. Select Generate token.
  3. Select API Token as the token type.
  4. Under General, enter a name for the token (ex. Postman JavaScript API) and an optional description.
  5. Select an Auto purge token interval.
  6. Under Token permissions, select Custom, and then select the JavaScript API you created in the previous section.
  7. Select Save.

Note: Copy your token value and store it securely — it will not be shown again.

Find your endpoint URI

Your JavaScript API has a unique endpoint URI that external applications use to call it.

JavaScript API detail view with the Copy URL button highlighted.

  1. Go to Resources > APIs > JavaScript.
  2. Click the name of your JavaScript API, or select Actions (...) > Edit JavaScript API.
  3. Click Copy URL to copy the unique endpoint URI to your clipboard.

Field reference

JavaScript API fields

Field Description Default Accepted values Required
Name A unique identifier for the JavaScript API, visible in the APIs list. None Any string Yes
Description Describes how the API works and any nuances relevant to future editors. None Any string No
Scripts The script containing the entry-point function to execute when the API is called. None Any saved script Yes
Function The name of the entry-point function in the selected script. Must match exactly, without parentheses. handleRequest Any valid function name Yes
Enable ShipWorks authentication When checked, integrator.io validates the username and password from incoming ShipWorks HTTP requests. Unchecked Checked / Unchecked No
Username (ShipWorks only) The ShipWorks username provided during store setup. Only available when ShipWorks authentication is enabled. None Any string Yes (if ShipWorks auth enabled)
Password (ShipWorks only) The ShipWorks password provided during store setup. Stored with AES 256 encryption. Must be re-entered each time the form is edited. None Any string Yes (if ShipWorks auth enabled)

API token fields

Field Description Default Accepted values Required
Name A unique identifier for the API token, visible in the API tokens list. None Any string Yes
Auto purge token The interval after which the token is automatically revoked and removed. None Never, 1 hour, 4 hours, 1 day, 4 days, 10 days, 30 days, 90 days Yes
Token permissions Controls the scope of access granted by this token. Select Custom to restrict the token to specific resources, such as your JavaScript API. None Full access, Custom Yes

Next steps

After you build your JavaScript API, test it. For scripts that run imports, virtual exports, and virtual imports, see Run existing exports and imports with a JavaScript API and Run virtual exports and imports with a JavaScript API. To let API consumers call your JavaScript API, see Push and publish your resource or API.