createOnEvent().
Use them to validate credentials, register provider webhooks, seed connection-specific metadata, or clean up external resources before a connection is deleted.
Choose the lifecycle event
Supported events:For agents
For agents
Before generating an event function, identify the lifecycle event, provider endpoint, required metadata, and desired failure behavior.For
validate-connection, throwing rejects the auth attempt. On first connect, the connection is deleted. On reconnect, the connection is marked as an auth error (refresh_exhausted) and the user must reconnect explicitly. For cleanup functions, make the provider call idempotent because the external resource may already be gone.For same-account enforcement on reconnect, store the provider account identifier in connection metadata during post-connection-creation, then compare it to a live identity API call in validate-connection. Do not compare connection_config fields: reconnect upserts new credentials before validation runs, so config values already reflect the newly authenticated account.Create a setup function
Add a file under the integration’son-events/ folder:
salesforce/on-events/post-connection-setup.ts
index.ts:
index.ts
Create a connection validation function
Add a file under the integration’son-events/ folder:
salesforce/on-events/validate-connection.ts
index.ts:
index.ts
organization_id after auth succeeds. On reconnect, validation compares the new credentials against the saved org ID and rejects the attempt if they differ.
Compare the locked value in metadata to a live value from the provider API. Do not compare
connection_config fields: on reconnect, Nango upserts the new credentials and overwrites connection_config before validate-connection runs, so config already reflects the account the user just authenticated with.Create a cleanup function
If the provider webhook is registered per connection, clean it up before the Nango connection is deleted:salesforce/on-events/pre-connection-cleanup.ts
index.ts:
index.ts