Integration scripts
Examples
Read more about integration scripts to understand what role they play in Nango.
Integration scripts expose a helper object (NangoSync
for sync scripts, NangoAction
for action scripts), which allows to interact with external APIs & Nango more easily.
HTTP requests
Makes an HTTP request inside an integration script:
Note that all HTTP requests benefit from automatic credential injection. Because scripts are executed in the context of a specific integration & connection, Nango can automatically retrieve & refresh the relevant API credentials.
Parameters
Response
HTTP request retries
To configure retries when HTTP requests fail, use the retries
and retryOn
parameters in your HTTP requests.
The following will apply:
- By default, retries are performed for
5xx
&429
statuses, as well asECONNRESET
,ETIMEDOUT
, andECONNABORTED
. - Use the
retryOn
parameter to specify an array of additional status codes to retry on. - By default, no retries are performed:
retries
default to0
- The retry starting delay is
100ms
, the delay between attempts is multiplied by2
each time (exponential backoff) and there isno maximum delay
between retries.
Logging
You can collect logs in integration scripts. This is particularly useful when:
- developing, to debug your integration scripts
- in production, to collect information about integration script executions & understand issues
Collect logs in integration scripts as follows:
Logs can be viewed & searched in the Nango UI. We plan to make them exportable in the future as well.
Environment variables
Integration scripts sometimes need to access sensitive variables that should not be revealed directly in the code.
For this, you can define environment variables in the Nango UI, in the Environment Settings tab. Then you can retrieve these environment variables from integration scripts with:
Parameters
No parameters.
Response
Trigger action
Integration scripts currently do not support importing files, which limits the ability to share code between integration scripts.
As a temporary workaround, you can call action scripts from other integration scripts with:
Parameters
Response
Paginate through API responses
Nango provides a helper to automatically paginate endpoint responses.
Similar to HTTP requests, the nango.paginate()
method takes in a ProxyConfiguration
parameter.
Use the paginate
field to of the ProxyConfiguration
to specify how the endpoint’s pagination work. Here’s an example for a Jira endpoint:
As shown in the example above, use a for
loop to iterate through the paginated results.
Nango has pre-configured the pagination settings for some popular APIs, so you don’t have to specify them in scripts.
You can view the pre-configured pagination settings for all APIs in the providers.yaml file.
Please note that some APIs have diverging pagination strategies per endpoint, so you might still need to override pre-configured pagination settings at times.
The pagination helper supports 3 types of pagination: cursor
, link
or offset
with the following settings:
Get Integration
Returns the current integration information
With credentials
Parameters
See GET /integrations/{uniqueKey}
query parameters: documentation
Response
See GET /integrations/{uniqueKey}
response: documentation
Manage connection metadata
Get connection metadata
Returns the connection’s metadata.
Better, you can specify the type of the metadata;
Parameters
No parameters.
Example Response
Set connection metadata
Set custom metadata for the connection (overrides existing metadata).
Parameters
Response
Empty response.
Edit connection metadata
Edit custom metadata for the connection. Only overrides & adds specified properties, not the entire metadata.
Parameters
Response
Empty response.
Get the connection credentials
Returns a specific connection with credentials.
The response content depends on the API authentication type (OAuth 2, OAuth 1, API key, Basic auth, etc.).
When you fetch the connection with this API endpoint, Nango will check if the access token has expired. If it has, it will refresh it.
We recommend not caching tokens for longer than 5 minutes to ensure they are fresh.
Parameters
Example Response
Sync-specific helper methods
Sync scripts persist data updates to the Nango cache, which your app later fetches (cf. step-by-step guide).
Save records
Upserts records to the Nango cache (i.e. create new records, update existing ones). Each record needs to contain a unique id
field used to dedupe records.
Parameters
Delete records
Marks records as deleted in the Nango cache. Deleted records are still returned when you fetch them, but they are marked as deleted in the record’s metadata (i.e. soft delete).
The only field that needs to be present in each record when calling batchDelete
is the unique id
; the other fields are ignored.
Parameters
Update records
Updates records in the Nango cache by merging the given data into the existing record. The id
field is required in each record and used to determine what existing record to merge into.
batchUpdate
is primarily useful in webhook sync scripts, where you receive partial updates from a webhook and want to merge them into the existing records.
The merge algorithm used is a deep merge. Nested objects are merged recursively, while arrays always use the new value for the array. Any fields not present in the update record are left unchanged.
Take special care when using batchUpdate
with records containing arrays. The merge algorithm does not attempt to merge arrays, but rather always uses the value of the new array.
Parameters
Get records
Fetches records from the Nango cache by ID. Returns a Map where the keys are the requested IDs, and the values are the corresponding records. Any records that are not found will simply be absent from the map.
Example usage:
Fetching records by ID is useful when you need to update specific records with a more granular approach than nango.batchUpdate()
, which performs a deep merge. Note that nango.batchUpdate()
is more performant than using nango.getRecordsById()
, followed by nango.batchSave()
.
A common use case is when handling external webhooks, where only a partial update of a record is received from an API.
Action-specific helper methods
ActionError
You can use ActionError
in an action script to return a descriptive error to your app when needed:
In this case, the response to the trigger action call will be:
Relative imports in scripts
You can import relative files into your scripts to allow for code abstraction and to
maintain DRY (Don’t Repeat Yourself) principles. This means you can reuse code across
different scripts by importing it. The imported file must live in the nango-integrations
directory and can be imported in the following way:
Note that you cannot import third-party modules at this time. Additionally, if there is a compilation error in an imported file, the entry point file will also fail to compile.
Pre-included Dependencies
Some libraries are pre-included for usage in scripts:
Please reach out in the community if you would like to request additional ones.
Questions, problems, feedback? Please reach out in the Slack community.
Was this page helpful?