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.
-
Create an import step in the Celigo platform in a new or existing flow.
-
Build your HTTP request body using the available resources and the Advanced Field Editor.
-
Download the flow that includes the import step.
-
Open the flow JSON in a text editor and copy the HTTP request body.
-
From the Resources menu, select Scripts.
-
At the right, click + Create script.
-
Name your new script (ex. importVirtual_Script).
-
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
}
}
-
Navigate to Resources → My APIs.
-
Click + Create My API at the upper right.
-
Enter a name (ex. Demo – imports.runVirtual(options)).
-
Select the script saved in step B, importVirtual_Script.
-
Click to open the Script editor and copy the function name (main_importVirtual). Paste the name into the Function field.
-
Click Save & close.
-
Navigate to Resources → API token
-
Click + Create API token at the upper right.
-
Enter a name (ex. Run virtual import).
-
In the Token permissions section, select Custom scopes.
-
From the My APIs list, select the My API you created in step C, (ex. Demo – imports.runVirtual(options)).
-
Click Save & close.
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> ] }
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.
Comments
Article is closed for comments.