Articles in this section

Invoke a My API virtual import

This example creates a virtual import – a resource that exists only at runtime, not added to your integrator.io account – in My APIs and runs it to demonstrate the record sent to the destination application.

A. 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.

B. 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.

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

    • It must handle any errors.

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

    • Define the import data as a variable, as shown below in the JSON structure:

  Below is an example script:

/*
* 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
  }
}

C. Create the My API

  1. Navigate to Resources → My APIs.

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

  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.

D. 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 My APIs list, select the My API you created in step C, (ex. Demo – imports.runVirtual(options)).

  6. Click Save & close.

E. Test the virtual import

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

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

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

E. Expose to API Management (optional)

You can expose this My API 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 MyAPI.

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.