Articles in this section

Pass data through query parameters using JavaScript APIs

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!

A. Write the script

  1. From the Resources menu, select Scripts.

  2. At the right, click + Create script.

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

  4. 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 })
    }
  }
}

B. Create the JavaScript API

  1. Navigate toResourcesAPIsJavaScript.

  2. Click + Create API at the upper right.

  3. Enter a name (ex. Demo – exports.runVirtual(options)).

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

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

  6. Click Save & close.

C. Get an API token

  1. Navigate to ResourcesAPI token.

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

  3. Enter a name (ex. Run virtual export).

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

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

  6. Click Save & close.

D. Test the virtual export

Open Postman and connect to the JavaScript API created above at the provided URL, including the URL parameters: GET https://api.integrator.io/v1/apis/<apiid>/request?ticketId=1