Examples
Configuration
createSync
The description of the sync.
The endpoints of the sync.
You can call this endpoint to fetch records synced by the sync.
You need one endpoint per model.Which you can then call like this:
The function that will be called when the sync is triggered.
The frequency of the sync.
The models that will be synced by this function.
You need one endpoint per model.
The type of the sync.
If
true
, automatically runs the sync when a new connection is created.
Otherwise, it needs to be triggered via the API or Nango UI.The connection’s metadata of the action.
The function that will be called when a webhook is received.
The integration’s scopes required by the action.
This field is for documentation purposes only and currently not enforced by Nango.
[DEPRECATED] Instead use
await nango.deleteRecordsFromPreviousExecutions('modelName')
inside the sync exec
function.When trackDeletes
is set to true
, Nango automatically detects deleted records during full syncs only and marks them as deleted in each record’s metadata (soft delete). These records remain stored in the cache.When set to false
, Nango does not mark missing records as deleted, even if they weren’t returned in the latest full sync—they simply remain in the cache unchanged.Defaults to false
.The version of the sync.
Use it to track changes to the sync inside Nango’s UI.
The webhook subscriptions of the sync.
Specify the types of webhooks the method
onWebhook
will handle.
If a webhook type is not on the list, it will not be handled.createAction
The description of the sync.
The endpoints of the sync.
You can call this endpoint to fetch records synced by the sync.
You need one endpoint per model.
The function that will be called when the action is triggered.
The input required by the action when triggering it.
The output of the action.
The version of the sync.
Use it to track changes to the sync inside Nango’s UI.
The connection’s metadata of the action.
The integration’s scopes required by the action.
This field is for documentation purposes only and currently not enforced by Nango.
createOnEvent
The description of the sync.
The event that will trigger this function.
The function that will be called when the action is triggered.
The version of the onEvent function.
Use it to track changes to the onEvent function inside Nango’s UI.
The connection’s metadata of the function.
HTTP requests
Makes an HTTP request inside an integration function:Note that all HTTP requests benefit from automatic credential injection. Because functions are executed in the context of a specific integration & connection, Nango can automatically retrieve & refresh the relevant API credentials.
HTTP request retries
To configure retries when HTTP requests fail, use theretries
and retryOn
parameters in your HTTP requests.
The following will apply:
- By default, retries are performed when:
- HTTP Status:
>= 500
,429
- Network error:
ECONNRESET
,ETIMEDOUT
, andECONNABORTED
. - Pre-configured headers for providers in (providers.yaml).
- HTTP Status:
- 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
3000ms
, the delay between attempts is multiplied by2
each time (exponential backoff) and is capped at10 minutes
.
Logging
You can collect logs in integration functions. This is particularly useful when:- developing, to debug your integration functions
- in production, to collect information about integration function executions & understand issues
Environment variables
Integration functions 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 functions with:Trigger action
Integration functions currently do not support importing files, which limits the ability to share code between integration functions. As a temporary workaround, you can call action functions from other integration functions with:Paginate through API responses
Nango provides a helper to automatically paginate endpoint responses. Similar to HTTP requests, thenango.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:
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 functions.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.
cursor
, link
or offset
with the following settings:
Get Integration
Returns the current integration informationGET /integrations/{uniqueKey}
query parameters: documentation
Response
See GET /integrations/{uniqueKey}
response: documentation
Manage connection metadata
Get connection metadata
Returns the connection’s metadata.Set connection metadata
Set custom metadata for the connection (overrides existing metadata).Edit connection metadata
Edit custom metadata for the connection. Only overrides & adds specified properties, not the entire metadata.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.
Sync-specific helper methods
Sync functions 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 uniqueid
field used to dedupe records.
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). To implement deletion detection in your syncs, follow this guide. The only field that needs to be present in each record when callingbatchDelete
is the unique id
; the other fields are ignored.
Detect deletions automatically
Automatically detects and marks records as deleted by comparing the current sync execution with the previous one.batchSave
. Nango will compare the records saved in the current execution with those from the previous execution and mark any missing records as deleted.
Parameters
Important considerations:
- Only use within syncs that fetch the complete dataset
- Call this function only after successfully saving all records
- If your sync fails or doesn’t fetch the complete dataset, avoid calling this function as it may cause false deletions
- Records are soft deleted (marked with
_metadata.deleted = true
) and remain in the cache
Update records
Updates records in the Nango cache by merging the given data into the existing record. Theid
field is required in each record and used to determine what existing record to merge into.
batchUpdate
is primarily useful in webhook sync functions, 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.
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.Variant
If you are using sync variants, you can access the current variant name via thenango.variant
property.
Action-specific helper methods
ActionError
You can use ActionError
in an action function to return a descriptive error to your app when needed:
Relative imports in functions
You can import relative files into your functions to allow for code abstraction and to maintain DRY (Don’t Repeat Yourself) principles. This means you can reuse code across different functions by importing it. The imported file must live in thenango-integrations
directory and can be imported in the following way:
Pre-included Dependencies
Some libraries are pre-included for usage in functions: Please reach out in the community if you would like to request additional ones.Questions, problems, feedback? Please reach out in the Slack community.