Use these examples to define an export or import inside a JavaScript API script and run it, without saving the resource in your account. A virtual resource lets the request decide what to export or import each time. They are for account owners and administrators who have already built a JavaScript API. To run a resource that already exists in your account, see Run existing exports and imports with a JavaScript API.
For every example, Build a JavaScript API to create the script, the JavaScript API, and the API token, and then Test a JavaScript API. Each script's functions are described in integrator.io API and JavaScript runtime objects. To let consumers call your JavaScript API through API Management, see Push and publish your resource or API.
Run a virtual export
This example defines a NetSuite export in the script and runs it with exports.runVirtual(). The export returns invoices with an amount greater than the amount in the request.
Create a script named exportVirtual_Script with this code, replace YOUR_CONNECTION_ID with the ID of your NetSuite connection, and enter main_exportVirtual in the Function field of the JavaScript API:
import { exports } from 'integrator-api'
function main_exportVirtual (options) {
let invokeExportResponse;
let response = {};
let NSObject = return_NS_Export_Obj(options);
// Execute the export
try {
invokeExportResponse = exports.runVirtual({ export: NSObject });
response.statusCode = 200;
} catch (e) {
invokeExportResponse = JSON.stringify(e);
response.statusCode = 400;
}
// Create body response
response.body = invokeExportResponse;
return {
statusCode: response.statusCode,
headers: { },
body: response.body
};
}
function return_NS_Export_Obj (options) {
// Use the amount received in the request
let amount = options.body.records[0].amount;
return {
"_connectionId": "YOUR_CONNECTION_ID",
"netsuite": {
"type": "restlet",
"skipGrouping": true,
"statsOnly": false,
"restlet": {
"recordType": "invoice",
"searchId": "6763",
"criteria": [
{
"field": "amount",
"operator": "greaterthan",
"searchValue": amount
}
]
},
"distributed": {
"disabled": false,
"forceReload": false,
"executionContext": ["userinterface", "webstore"],
"executionType": ["create", "edit", "xedit"]
}
},
"adaptorType": "NetSuiteExport"
};
}Send this request body:
{
"records": [
{ "amount": 1000 }
]
}The response contains the matching records and links to them in NetSuite:
{
"data": [
{
"id": "313097",
"recordType": "invoice",
"Type": "Invoice",
"Document Number": "INV73910366",
"Name": "Ariba Customer",
"Amount": "2000.00"
}
],
"dataURIs": [
"https://tstdrv1934805.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=313097&compid=TSTDRV1934805"
]
}The export definition in this example is for NetSuite. For other applications, get the definition from the export's REST API request, as described in Get the export object definition from an integrator.io REST API request.
Pass data through query parameters
This example reads a value from the query string of the request and uses it in a virtual export. The script gets the details of a Zendesk ticket by the ticketId in the URL.
Create a script with this code, replace YOUR_CONNECTION_ID with the ID of your Zendesk connection, and enter handleRequest in the Function field of the JavaScript API:
import { exports } from 'integrator-api'
// Sample URL: https://api.integrator.io/v1/apis/<apiId>/request?ticketId=123
function handleRequest (options) {
// Replace with the ID of your Zendesk connection
const connId = 'YOUR_CONNECTION_ID';
// Get the ticketId from the query string, for example { ticketId: '123' }
const queryParams = options.queryString;
const ticketId = queryParams ? queryParams.ticketId : undefined;
try {
if (!ticketId) {
throw new Error('ticketId is required');
}
// Run the virtual export to get the ticket details
const exportResponse = exports.runVirtual({
export: {
name: 'zendesk export',
_connectionId: connId,
asynchronous: true,
adaptorType: 'HTTPExport',
http: {
relativeURI: '/api/v2/tickets/' + ticketId,
method: 'GET'
}
}
});
return {
statusCode: 200,
body: JSON.stringify(exportResponse)
};
} catch (e) {
return {
statusCode: 400,
body: JSON.stringify({ error: e.message })
};
}
}Call the JavaScript API with the query parameter at the end of the URL. This request uses the GET method:
GET https://api.integrator.io/v1/apis/<apiId>/request?ticketId=1
If the request has no ticketId, the script returns the status code 400 and the message ticketId is required.
Page through a virtual export
This example runs a virtual export with exports.runVirtualWithPaging(), which returns the records one page at a time. Set pageSize so each response stays well under the 5 MB limit per page. A virtual export supports a maximum of 1,000 pages.
Create a script named exportVirtualPaging_Script with this code, replace YOUR_CONNECTION_ID with the ID of your NetSuite connection, and enter handleRequest in the Function field of the JavaScript API:
import { exports } from 'integrator-api'
function handleRequest (options) {
let invokeExportResponse;
let response = {};
try {
const pagingObj = { export: return_NetSuite_Export_Obj() };
// For pages after the first, pass back the pagedExportState from the previous response
if (options.body && options.body.pagedExportState) {
pagingObj.pagedExportState = options.body.pagedExportState;
}
invokeExportResponse = exports.runVirtualWithPaging(pagingObj);
response.statusCode = 200;
} catch (e) {
invokeExportResponse = JSON.stringify(e);
console.log(JSON.stringify(e), e.code, e.message);
response.statusCode = 400;
}
response.body = invokeExportResponse;
return {
statusCode: response.statusCode,
headers: { },
body: response.body
};
}
function return_NetSuite_Export_Obj () {
return {
"_connectionId": "YOUR_CONNECTION_ID",
"asynchronous": true,
"pageSize": 5,
"netsuite": {
"type": "restlet",
"skipGrouping": true,
"statsOnly": false,
"restlet": {
"recordType": "item",
"searchId": "69",
"restletVersion": "suiteapp2.0",
"markExportedBatchSize": 100
},
"distributed": {}
},
"adaptorType": "NetSuiteExport"
};
}To get every page:
- Send the first request with an empty body. The response contains the first page of records.
- Copy
pagedExportStatefrom the response, and send it as the body of the next request to get the second page. - Repeat step 2 with the
pagedExportStatefrom each new response until you have all the pages.
Run a virtual import
This example defines an import in the script and runs it with imports.runVirtual(), sending the records in the request to the destination application.
- Create the import in a new or existing flow, and build the HTTP request body with the available resources and the Advanced field editor.
- Download the flow, open the flow JSON in a text editor, and copy the import definition.
-
Create a script named importVirtual_Script with this code, paste the import definition you copied into
importDefinition, and entermain_importVirtualin the Function field of the JavaScript API:import { imports } from 'integrator-api' function main_importVirtual (options) { let importResponse; let response = {}; // Paste the import definition you copied from the flow JSON const importDefinition = { // ... }; // The records to send to the destination application const importData = options.body.data; // Execute the import try { importResponse = imports.runVirtual({ import: importDefinition, data: importData }); response.statusCode = 200; } catch (e) { importResponse = JSON.stringify(e); response.statusCode = 400; } response.body = importResponse; return { statusCode: response.statusCode, headers: { }, body: response.body }; } -
Send a request with the records in a
dataarray:{ "data": [ { "orderId": "1001", "status": "shipped" } ] }
The field names in data must match the fields your import definition uses.