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
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
TbD
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
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?