import type { GithubIssueDemo, NangoSync } from './models';export default async function fetchData(nango: NangoSync) { // Fetch issues from GitHub. const res = await nango.get({ endpoint: '/repos/NangoHQ/interactive-demo/issues?labels=demo&sort=created&direction=asc' }); // Map issues to your preferred schema. const issues: GithubIssueDemo[] = res.data.map(({ id, title, html_url }: any) => { return { id, title, url: html_url }; }); // Persist issues to the Nango cache. await nango.batchSave(issues, 'GithubIssueDemo');}
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.
Makes an HTTP request inside an integration script:
Copy
Ask AI
const config = { endpoint: '/some-endpoint' };await nango.get(config); // GET requestawait nango.post(config); // POST requestawait nango.put(config); // PUT requestawait nango.patch(config); // PATCH requestawait nango.delete(config); // DELETE request
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.
{ data: {}, // the response provided by the server status: 200, // the HTTP status code headers: {}, // the HTTP headers config: {}, // the config provided for the request request: {} // the request that generated this response }
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:
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:
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.
Defaults to false. If false, the token will only be refreshed if it expires within 15 minutes. If true, a token refresh attempt will happen on each request. This is only useful for testing and should not be done at high traffic.
Defaults to false. If false, the refresh token is not included in the response, otherwise it is. In production, it is not advised to return the refresh token, for security reasons, since only the access token is needed to sign requests.
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.
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.
Copy
Ask AI
const githubIssuesToDelete: { id: string }[] = ...; // Fetch issues to delete from GitHub API. await nango.batchDelete(githubIssuesToDelete, 'GitHubIssue');