This code allows you to pass data through query parameters. The code simply echoes the query string and body from the request into the response's body. In this example, we're using a Zendesk connection; however, you can configure this as needed. Don't forget to change the connection ID!
-
From the Resources menu, select Scripts.
-
At the right, click + Create script.
-
Name your new script (ex. exportVirtual_Script).
-
Write the JavaScript code in the Edit content field.
-
The script must have at least one entry-point function.
-
It must handle any errors.
-
In this case, it calls exports.runVirtual(), passing it the export’s ID.
-
Define the export with the options object definition.
-
/* This is a sample code for a handler that uses the integrator-api to run a virtual export to get ticket details. this is the sample JavaScript Api url: https://api.integrator.io/v1/apis/<apiId>/request?ticketId=123 */ import { exports } from 'integrator-api' // This function is the entry point for the JavaScript API function handleRequest(options) { // Define the connectionId const connId = '66••••••••••872' // Use the connection id instead of the placeholder <connId> // Get the ticketId from the query string const queryParams = options.queryString //{ ticketId: '123' } const ticketId = queryParams ? queryParams.ticketId : undefined // '123' try { // Check if the ticketId is provided 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 the ticket details return { statusCode: 200, body: JSON.stringify(exportResponse) } } catch (e) { // Return an error response return { statusCode: 400, body: JSON.stringify({ error: e.message }) } } }
-
Navigate to
→ → . -
Click + Create API at the upper right.
-
Enter a name (ex. Demo – exports.runVirtual(options)).
-
Select the script saved in step A, exportVirtual_Script.
-
Click
to open the Script editor and copy the function name. Paste the name into the Function field.
-
Click Save & close.
-
Navigate to
→ . -
Click + Create API token at the upper right.
-
Enter a name (ex. Run virtual export).
-
In the Token permissions section, select Custom scopes.
-
From the JavaScript APIs list, select the JavaScript API you created in step B, (ex. Demo – exports.runVirtual(options)).
-
Click Save & close.