Articles in this section

Invoke a My API virtual export with paging

This example creates a virtual export with paging – a resource that exists only at runtime, not added to your integrator.io account – in My APIs and runs it to demonstrate the record returned from the source application.

A. Write the script

  1. From the Resources menu, select Scripts.

  2. At the right, click + Create script.

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

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

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

    • It must handle any errors.

    • In this case, it calls exports.runVirtualWithPaging( options ).

    • Update pageSize values based on the business need, such that the response size limit is well below the 5 MB max limit per page. Note that Celigo currently supports a maximum of 1000 pages.

    • Define the export with the options object definition, as shown below in the NSObject JSON structure:

    Note

    The export object definition in the below example is for NetSuite. For other applications, you will need to use the export API endpoint (https://api.integrator.io/v1/exports/<exportId>) endpoint to get the object definition.

  5. Save the script.

import { exports } from 'integrator-api';

function handleRequest(options) {
  
  let success = true;
  let invokeExportResponse;
  let nsObject;
  let response = {};
  
  nsObject = return_NetSuite_Export_Obj(options);

  try {
    const pagingObj = {
      export:nsObject
    }
    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;

  function return_NetSuite_Export_Obj(options) {
  
    let netSuiteObject = {
      "_connectionId": '6363916275366d48c0f1521d',
      "asynchronous": true,
      "pageSize": 5,
      "netsuite": {
          "type": "restlet",
          "skipGrouping": true,
          "statsOnly": false,
          "restlet": {
              "recordType": "item",
              "searchId": "69",
              "restletVersion": "suiteapp2.0",
              "markExportedBatchSize": 100
          },
          "distributed": {}
      },
      "adaptorType": "NetSuiteExport"
    };
    return netSuiteObject;
  }

  return {
    statusCode: response.statusCode,
    headers: {},
    body: response.body,
  };
}

B. 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 – exports.runVirtualPaging(options)).

  4. Select the script saved in step A, exportVirtualPaging_Script.

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

  6. Click Save & close.

Create_myAPI.png

C. 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 export paging).

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

  5. From the My APIs list, select the My API you created in step B, (ex. Demo – exports.runVirtualPaging(options)).

  6. Click Save & close.

D. Test the virtual export

Open Postman and connect to the My API created above at the provided invoke URL.

Invoke_URL_highlight.png
  1. To fetch the first page of data, the request body should be empty.

  2. Copy the pagedExportState from the response, and use it as the request body to fetch the 2nd page of data.

  3. Similarly, fetch the remaining pages of data, by calling the API multiple times with updated pagedExportState as the request body.

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

{
 "records":[
   {"amount":1000}
 ] 
}

Sample response

{
  "data": [
    {
      "id": "313097",
      "recordType": "invoice",
      "Order Type": "",
      "*": " ",
      "Date": "8/14/2019",
      "As-Of Date": "8/14/2019",
      "Period": "Aug 2019",
      "Tax Period": "",
      "Type": "Invoice",
      "Document Number": "INV73910366",
      "Name": "Ariba Customer",
      "Account": "4000 Sales",
      "Memo": "",
      "Billing Account": "",
      "Amount": "2000.00",
      "CeligoAT_BigCommerceOrderID": "552",
      "Item": "Demo core product 2"
    }
  ],
  "dataURIs": [
    "https://tstdrv1934805.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=313097&compid=TSTDRV1934805"
  ]
}

E. Expose via 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.