Articles in this section

Invoke a JavaScript API virtual import

This example creates a virtual import—a resource that exists only at runtime and is not added to your integrator.io account—in JavaScript API and runs it to demonstrate the record sent to the destination application.

Create import

  1. Create an import step in the Celigo platform in a new or existing flow.

  2. Build your HTTP request body using the available resources and the Advanced Field Editor.

  3. Download the flow that includes the import step.

  4. Open the flow JSON in a text editor and copy the HTTP request body.

Write the script

  1. From the Resources menu, select Scripts.

  2. At the right, click + Create script.

  3. Name your new script (ex. importVirtual_Script).

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

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

    2. It must handle any errors.

    3. In this case, it calls imports.runVirtual(), passing it the main import function and the import body.

    4. Define the import data as a variable.

    /*
    * handleRequest function stub:
    *
    * The name of the function can be changed to anything you like.
    *
    * The function will be passed one 'options' argument that has the following fields:
    *   'method' - http request method (uppercase string).
    *   'headers' - http request headers (object).  
    *   'queryString' - http request query string (object).
    *   'body' - parsed http request body (object, or undefined if unable to parse).
    *   'rawBody' - raw http request body (string).
    *
    * The function needs to return a response object that has the following fields:
    *   'statusCode' - http response status code (number).
    *   'headers' - http response headers overrides (object, optional).
    *   'body' - http response body (string or object).
    * Throwing an exception will signal an error.
    */
    
    import { imports } from 'integrator-api'
    
    function postImportOrderUpdate (connectionId, updatedImportOrder){
    try{
      let importData = [{
        importOrder: updatedImportOrder
        }]
      let main_importVirtual()= {
        "_id": connectionId,
        "apiIdentifier": "*****",
        "ignoreExisting": false,
        "ignoreMissing": false,
        "oneToMany": false,
        "assistantMetadata": {
            "resource": "orders",
            "version": "v2",
            "operation": "createorder"
        },
        "http": {
            "relativeURI": [
                "v2/orders/{{{record.Order.id}}}"
            ],
            "method": [
            "PUT"
            ],
            "body": [ 
                "{<YOUR BODY HERE>}"
            ],
            "batchSize": 1, 
            "requestedMediaType"
            ...
        }
      
      //Execute the import
      let {
        imports.runVirtual({ import: main_importVirtual, data: importData});
        response.statusCode = 200;
      } catch(e) {
        invokeImportResponse = JSON.stringify(e);
        response.statusCode = 400;
      }
    
      return {
        statusCode: response.statusCode,
        headers: { },
        body: response.body
      }
    }
    

Create the JavaScript API

  1. Navigate toResourcesAPIJavaScript.

  2. Click+ Create APIJavaScript.

  3. Enter a name (ex. Demo – imports.runVirtual(options)).

  4. Select the script saved in step B, importVirtual_Script.

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

  6. Click Save & close.

Get an API token

  1. Navigate to Resources → API token

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

  3. Enter a name (ex. Run virtual import).

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

  5. From the JavaScript API(s) list, select the JavaScript API you created in step C, (ex. Demo – imports.runVirtual(options)).

  6. Click Save & close.

Test the virtual import

Open Postman and connect to the JavaScript API created above at the provided URL, with the following values:

POST /v1/apis/5f3•••••••••••••7d6/request HTTP/1.1
Host: api.integrator.io
content-type: application/json
Authorization: Bearer d56••••••••••••••••••c06
Content-Type: text/plain

{
 "data":[
   <import data body>
 ] 
}

E. Push to API Management (optional)

You can expose this JavaScript API(s) using Celigo's API Management so your API consumers can invoke it. You'll have full control over how the API is authenticated, documented, and managed. Learn to expose a JavaScript API.