Skip to main content
Our Script Paging is a powerful scripting language that allows you to specify how data should be fetched and processed in a Custom Connector, giving you control over pagination, rate limiting, and API request and response management.
We assume you will have some familiarity with scripting languages in order to use this feature. If you prefer to work without code, Custom Connector also offers a range of standard paging options that will suit most needs. Read Pagination for a description of the available options.

Using Script Paging

When creating a custom connector:
  1. Follow the process given in Custom connector setup.
  2. When selecting a Pagination option, select Script.
  3. This gives you a multi-line text input field where you can write your Script Paging script, following the syntax rules given below.
You must configure any required header parameters, query parameters, and URI parameters in the custom connector setup. Script Paging operations can then use these parameters to control the paging of the response. For example, the following endpoint URI specifies values for the limit and offset parameters:
Your script can then read these parameter values, use them in expressions, and write new values to them, as required. For example:
All of these script operations are described in the following sections. It’s important to understand how and when the Script Paging script is executed.
  1. The request for data is sent to the endpoint URI, passing all header, query and URI paramters. The script isn’t executed at this point.
  2. The first page of results is retrieved, based on the passed parameters.
  3. The script is now executed, sequentially one line at a time. The script can now modify any parameters, including those in the response header and response body which was retrieved in step 2.
  4. The next page of data is retrieved, based on the parameters which may have been modified by the script.
  5. The script is executed again.
  6. Steps 4 and 5 continue until a script operation determines that data retrieval should stop, or the end of the retrievable data is reached.

Basic syntax

Script Paging scripts are composed of statements. Each statement typically performs a single action, such as setting a variable, modifying a request, or processing a response. A script typically consists of multiple statements. Each statement must be placed on a separate line and end with a semicolon ;. You can include comments in your scripts to explain the logic or to temporarily disable code.
  • Single-line comments start with //.
  • Multi-line comments are enclosed between /* and */.
An example of the script syntax is as follows:
Specific elements of this script are described in the following sections.
Script Paging is not intended to be a fully-featured programming language, but rather a simple scripting language with a limited set of operations that allow you to control pagination and API request and response management. For this reason, Script Paging doesn’t support programming constructs such as loops or conditionals. Only the functions specifically listed in this article are available for use in Script Paging.

Variables

Variables are defined by the keyword var followed by the variable name and the value assigned to it. Variables can have one of four data types:
  • String: A string of characters, enclosed in quotes " ".
  • Boolean: true or false.
  • Number: An integer with or without a decimal point.
  • Array: An array of strings, booleans, or numbers.
You don’t have to explicitly assign a data type; Script Paging will automatically determine the appropriate type based on the data assigned to it. Some examples of variable definitions:
You can declare a variable with the value of another variable already declared:

Debug logging

To help debug your pagination scripts, you can add debug logging by using @log(...). The parameters can include strings and any supported types of variable references. After you add logging, the messages appear in the Logs tab in Custom Connector or the Logging tab in at the debug level. The following examples show how to use @log(...): Example 1
Example 2
Using debug logging in your pagination script helps you identify issues, such as values not being extracted correctly from the response or your @pager.stop rule not returning true when expected.

Arithmetic operations

Arithmetic operations can be performed directly within variable assignments. For example:

String operations

To concatenate strings or string variables, use the + operator. You can concatenate any number of strings or string variables in a single statement. Some examples:
If a string contains quotes, ", they must be escaped with the \ character when assigned to a string variable:

Comparison expressions

Script Paging supports the following comparison operators:
  • Equal to ==
  • Not equal to !=
  • Less than <
  • Less than or equal to <=
  • Greater than >
  • Greater than or equal to >=
The form of the comparison statement is "value" operator "value". For example:
Comparison expressions are used in the pager.stop operation, which will exaluate an expression to determine if there are more pages to retrieve. For example:

Script Paging operations

The full power of Script Paging is from the operations that allow you to define how responses from endpoints are processed. These operations are listed in this section. Operation statements begin with the @ symbol. The operation may return data which you can assign to a variable, or it may perform some operation based on the result of an evaluated expression.

pager.pageCount()

Returns the number of pages fetched. You can assign the returned number to a variable for use later in the script. Example:

pager.stop(expression)

Tells the connector not to attempt to fetch the next page if the expression evaluates to true. Parameters:
  • expression: string. A valid Script Paging comparison expression that will be evaluated to produce a true or false result.
Examples:

response.header.get(key)

Returns the value of the specified key in a key-value pair in the response header. Parameters:
  • key: string. The key that you want the value of.
Example:

Gets the next page link from the response header of an API that uses link header paging. This works for any API that uses a standard link header paging model, for example the GitHub API illustrated in Example 6. Example:

Gets the last page link from the response header of an API that uses link header paging. This works for any API that uses a standard link header paging model, for example the GitHub API illustrated in Example 6. Example:

response.body.get(key)

Returns the value of the specified key in a key-value pair in the response body. Parameters:
  • key: string. The key that you want the value of. You must specify the key location using JSON Pointer notation, as defined in RFC 6901.
Example:

response.status.get()

Return the value of the response status code as a number. Example:

request.header.put(key, value)

Adds the specified key value to the request headers parameters. Parameters:
  • key: string. The name of the key that the value will be assigned to.
  • value: string. The value that will be assigned to the key.
Example:

request.header.remove(key)

Removes the value of the specified key from request header parameters. Parameters:
  • key: string. The name of the key that will be removed.
Example:

request.header.clear()

Clears all existing values from the request header parameters. Example:

request.header.get(key)

Returns the value of the specified key in a key-value pair in the request header. Parameters:
  • key: string. The key that you want the value of.
Example:

request.query.put(key, value)

Adds the specified key value to the request query parameters. Parameters:
  • key: string. The name of the key that the value will be assigned to.
  • value: string. The value that will be assigned to the key.
Example:

request.query.remove(key)

Removes the value of the specified key from request query parameters. Parameters:
  • key: string. The name of the key that will be removed.
Example:

request.query.clear()

Clear all existing values from the request query parameters. Example:

request.query.get(key)

Returns the value of the specified key in a key-value pair in the request query parameters. Parameters:
  • key: string. The key that you want the value of.
Example:

request.body.set(jsonString)

Sets the value of a key-value pair in the request body. This is used with JSON body format. Parameters:
  • jsonString: string. A valid JSON string that contains a key-value pair. You must specify the key location using JSON Pointer notation, as defined in RFC 6901. Quotes in the string must be escaped as \".
Example:

request.body.put(key, value)

Adds a new key-value pair to the request body. This is used with JSON body format. Parameters:
  • key: string. The name of the key that you want to add.
  • value: string. The value assigned to the key.
Example:

request.body.remove(key)

Removes the value of the specified key from the request body. This is used with JSON body format. Parameters:
  • key: string. The name of the key that you want to remove.
Example:

request.body.clear()

Clears all existing values from the request body. This is used with JSON body format. Example:

request.body.get(key)

Returns the value of the specified key in a key-value pair in the request body. This is used with JSON body format. Parameters:
  • key: string. The key that you want the value of.
Example:

request.uri.set(URI)

Sets the value of the URI. Parameter:
  • URI: string. The URI to set.
Example:

request.uri.append(path)

Appends a string to the request URI. Typically used to append a relative path to a base URI. Parameter:
  • path: string. The path to append to the request URI.
Example:

request.uri.replace(parameter, value)

Replaces parameterized values in the URI path. The parameterized values are set as URI parameters. Parameters:
  • parameter: string. The name of the parameter to be replaced.
  • value: string or number. The value to replace the parameter with.
Example:

request.ratelimit.set(allowlist, header, wait, retry)

Configures the rate limit for the endpoint. Suggested values for rate limits can usually be found in the API’s documentation. Parameters:
  • allowlist: array of numbers, for example [429, 403]. These are the status codes for surpressing the rate limit error.
  • header: string. The response header key whose presence indicates throttled response.
  • wait: number. Sets the wait in milliseconds, which should be extracted from the response.
  • retry: number. Sets number of retries.
Example:

JSON manipulation operations

Add a key-value pair to a JSON object.

The updated JSON object value should be assigned to a new variable. Parameters:
  • key: string. The key to add or update in the form of a JSON pointer.
  • value: string. The value to assign.
  • jsonObject: string. The JSON object to which the key-value pair will be added.
Example:

Remove a key from a JSON object

The updated JSON object value should be assigned to a new variable. Parameters:
  • key: string. The key to remove in the form of a JSON pointer.
  • jsonObject: string. The JSON object from which the key will be removed.
Example:

json.get(path, object)

Extracts a value from a JSON object. This requires knowing the path to the object in the JSON structure. Parameters:
  • path: string. The JSON path to the object. Specify the path using / to separate the names of nested elements, with / by itself indicating a root-level element.
  • object: string. The JSON object to extract the value from.
Example: Assuming the following JSON structure:

Increment and decrement operations

Increment a value

Increment a number. The incremented value should be assigned to a new variable.

Decrement a value

Decrement a number. The decremented value should be assigned to a new variable.

Examples

The following examples show some common uses for Script Paging. In each case, we give a real-world API call and the response returned by that call. We then show a suggested script to paginate that response.

Example 1: relative path

In this example, each page of data retrieved from the API endpoint provides us with a relative path which points to the next page we need to retrieve. We can use a script to read that path from each page and use it to retrieve the next page. The following URI retrieves filtered data in JSON format:
This retrieves a page of data with the following structure:
We can use the following script to query that data structure and extract from it the path to the next page of data, appending that path to the URI used in the next call to the endpoint. As this script is executed after each page is retrieved, we continue to fetch each subsequent page until we reach the last page.

Example 2: full path

This is similar to the last example, in that each page of data contains a pointer to the next page of data. However, the pointer is a full URI, not a relative path. This makes our script simpler, as we don’t have to concatenate the different parts of a URI. Use the following URI to retrieve the first page of data:
This returns data with the following structure:
A simple script extracts the next page URI and uses it to fetch the next page of data:

Example 3: page based

An endpoint which uses page-based pagination requires an incrementing a page parameter to retrieve each subsequent page. Each page of data contains its page number in the response structure, meaning that we need to read that number, increment it, and query again with the incremented page number. In the following URI, we are telling the endpoint to send us data starting at page 1:
The first page of data is returned with the following structure:
Our script needs to extract the value of the current page from the response body, add 1 to it, and construct a new query which includes the new page number as a parameter. We will use the script to stop data retrieval after 50 pages:

Example 4: cursor based

Cursor pagination uses a cursor parameter to navigate between pages. We need a script that reads the value of the next page cursor and puts that into the next query. Use the following URI to retrieve a page of data:
The response has the following structure:
The cursor that points to the next page is starting_after. The following script will extract this and put it into the next page query:

Example 5: offset

Offset pagination involves paging by incrementing a query parameter. We need to set the parameter in our initial query, and then use a script to increment the parameter for each subsequent page. Use the following URI to retrieve the first page of data:
This data has the following structure:
As there is no paging data in the response, the following script takes the offset value from the original query paramter and increments it by a fixed amount to retrieve the next page of data. Sensible offset numbers can usually be found in the API’s documentation. Note that because nothing in the response tells us when we’ve reached the last page, we’re checking for when an empty response body is returned, as this will tell us we’ve reached the end of the data.

Link header pagination uses a field in the response header to point to the next page, as in the following example:
We can use the following script to retrieve the link to the next page. If we also retrieve the link to the last page, we can then compare the two and stop paging when we reach the last page:

GraphQL paging example

API endpoint: https://organizationapi.com/graphql The API integrates pagination directly within the query structure. This requires us to parameterize the page value in the GraphQL query and to pass the page value in the variables. Example query to fetch customers:
Example response (truncated):

Solution 1: POST request

A POST request using this JSON POST body containing the query and variables:
Implement the paging script:

Solution 2: GET request

A GET request using the query and variables as query parameters:
Implement the paging script: