- Maia sends your request to the endpoint URI, passing all header, query, and URI parameters.
- The first page of results is retrieved, based on the parameters set in the connector configuration.
- The pagination script is then executed sequentially, one line at a time. Depending on the statements in the script, this may modify parameters in the request sent, and in the response header and response body that was retrieved.
- The next page of results is retrieved, based on the parameters set in the connector configuration, which may have been modified by the pagination script.
- The pagination script is executed again.
- The previous two steps repeat until the pagination script determines that the connector should stop retrieving data or that there is no more data to retrieve.
Script paging is not a full programming language. It’s a simple scripting language with a limited set of operations that you can use to control pagination, rate limiting, and API request and response management. As a result, Script paging doesn’t support programming constructs such as loops or conditionals. You can only use the syntax explained in this guide in Script pagination scripts.
Using Script paging
To set a custom connector or Flex connector to use Script paging:- If you’re creating a new connector, follow the steps in Creating custom connectors until you reach the Pagination step. If you’re editing an existing connector, click the Pagination tab in the custom connector editor.
- Select the Script pagination method.
- Write your pagination script in the text field, following the syntax rules in this guide.
Parameters in pagination scripts
Script paging operations can use a connector’s query parameters, URI parameters, and header parameters to control how the response is paginated. For example, the following endpoint URI specifies values for thelimit and offset query parameters:
Script paging syntax
Script pagination scripts are composed of multiple statements. Each statement typically performs a single action, such as setting a variable, modifying a request, or processing a response. Each statement must be on a new line and end with a semicolon;.
Some statements are operations—a specific type of statement that begins with the @ symbol and calls a predefined action, such as @log(...) or @pager.stop(...). For more information about operations, read Script paging operations and JSON manipulation operations.
Comments
You can include comments in your scripts to explain the logic or to temporarily disable code.- To add a single-line comment, write
//at the start of the line, followed by your comment. - To add a multi-line comment, enclose the comment in
/*and*/.
Variables
Variables are defined by the keywordvar followed by the variable name and the value assigned to it. Variables can have one of four data types:
- String: A string of characters. String values must be enclosed in quotes
" ". - Boolean: Boolean values must only be
trueorfalse. - Number: An integer with or without a decimal point.
- Array: An array of strings, booleans, or numbers. An array can only contain values of the same data type.
copyOfContentType variable will always be the same as the value of the contentType variable:
Arithmetic statements
Script pagination scripts can perform arithmetic directly within variable assignments, for example:String statements
To concatenate strings or string variables, use the+ operator. You can concatenate any number of strings or string variables in a single statement. If a string contains quote marks ", they must be escaped with the \ character when assigned to a string variable:
For example:
Comparison statements
Comparison statements must be written in the format<value> <operator> <value>. Comparison statements are used in the pager.stop operation, which will evaluate an expression to determine if there are more pages to retrieve, as shown in the example below.
Script pagination scripts support the following comparison operators, which you can use to compare literal values and variables:
- Equal to
== - Not equal to
!= - Less than
< - Less than or equal to
<= - Greater than
> - Greater than or equal to
>=
Increment and decrement statements
Increment statements increase a number. The incremented value should be assigned to a new variable.Debug logging
You can add debug logging using the@log(...) operation to help debug your pagination scripts. The arguments passed in the @log(...) operation can include strings and variables.
The messages returned by debug logging are shown in different places according to where you’re using the connector:
- If you’re configuring or editing a custom connector or Flex connector, the messages are shown in the Logs tab at the bottom of the custom connector editor when you send a test request.
- If you run the custom connector or Flex connector in a pipeline in Designer, the messages are shown in the Logging tab when you view details about a task.
@pager.stop rule not returning true when expected, as shown in the following examples.
Script paging operations
Script pagination supports operations that allow you to define how responses from endpoints are processed. This is what makes the Script pagination method so flexible. Operations begin with the@ symbol. An operation may return data that you can assign to a variable, or perform an action based on the result of an evaluated expression.
The following sub-sections of this guide contain an explanation of what each operation does, a table listing any parameters to configure, and one or more examples showing how to configure the operation.
pager.pageCount()
Returns the number of pages fetched. You can assign the returned number to a variable for use later in the script.pager.stop(expression)
Tells the connector not to attempt to fetch the next page if theexpression evaluates to true.
response.header.get(key)
Returns the value of the specified key in a key-value pair in the response header.response.header.getNextLink()
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.response.header.getLastLink()
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.response.body.get(key)
Returns the value of the specified key in a key-value pair in the response body.response.status.get()
Return the value of the response status code as a number.request.header.put(key, value)
Adds the specified key and value to the request header parameters.request.header.remove(key)
Removes the value of the specified key from request header parameters.request.header.clear()
Removes all existing values from the request header parameters.request.header.get(key)
Returns the value of the specified key in a key-value pair in the request header.request.query.put(key, value)
Adds the specified key and value to the request query parameters.request.query.remove(key)
Removes the value of the specified key from request query parameters.request.query.clear()
Removes all existing values from the request query parameters.request.query.get(key)
Returns the value of the specified key in a key-value pair in the request query parameters.request.body.set(jsonString)
Sets the value of a key-value pair in the request body. This is used with JSON body format.request.body.put(key, value)
Adds a new key-value pair to the request body. This is used with JSON body format.request.body.remove(key)
Removes the value of the specified key from the request body. This is used with JSON body format.request.body.clear()
Clears all existing values from the request body. This is used with JSON body format.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.request.uri.set(URI)
Sets the value of the URI.request.uri.append(path)
Appends a string to the request URI. Typically used to append a relative path to a base URI.request.uri.replace(parameter, value)
Replaces parameterized values in the URI path. The parameterized values are set as URI parameters.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.JSON manipulation operations
json.put(key, value, jsonObject)
Adds a key-value pair to a JSON object. The updated JSON object value should be assigned to a new variable.json.remove(key, jsonObject)
Removes a key from a JSON object. The updated JSON object value should be assigned to a new variable.json.get(path, object)
Extracts a value from a JSON object. You need to know the path to the object in the JSON structure.
Assuming the following JSON structure:
Examples
The following examples show some common uses for Script pagination. 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: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:Example 3: page based
An endpoint which uses page-based pagination requires 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: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: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:Example 6: link header
Link header pagination uses a field in the response header to point to the next page, as in the following example: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:
