### Install with Npm Source: https://github.com/onfido/onfido-node/blob/master/README.md Install the Onfido Node.js library using npm. ```sh npm install @onfido/api ``` -------------------------------- ### Install with Yarn Source: https://github.com/onfido/onfido-node/blob/master/README.md Install the Onfido Node.js library using Yarn. ```sh yarn add @onfido/api ``` -------------------------------- ### Create Applicant and Check API Calls (Promises) Source: https://github.com/onfido/onfido-node/blob/master/README.md Example of chaining API calls to create an applicant and then a check using promises. Includes basic error handling. ```js onfido .createApplicant({ first_name: "Jane", last_name: "Doe", location: { ip_address: "127.0.0.1", country_of_residence: "GBR" } }) .then(applicant => onfido.createCheck({ applicant_id: applicant.data.id, report_names: ["identity_enhanced"] }) ) .then(check => // Handle successfully created check. ) .catch(error => { // Handle error. }); ``` -------------------------------- ### Create Applicant API Call (Async/Await) Source: https://github.com/onfido/onfido-node/blob/master/README.md Example of creating an applicant using the Onfido API with async/await. Includes basic error handling for Axios errors. ```js (async () => { try { const applicant = await onfido.createApplicant({ first_name: "Jane", last_name: "Doe", location: { ip_address: "127.0.0.1", country_of_residence: "GBR", }, }); // ... } catch (error) { if (isAxiosError(error)) { console.log(`status code: ${error.response?.status}`); const error_details = error.response?.data.error; // An error response was received from the Onfido API, extra info is available. if (error_details) { console.log(error_details.message); console.log(error_details.type); } else { // No response was received for some reason e.g. a network error. console.log(error.message); } } else { console.log(error.message); } } })(); ``` -------------------------------- ### Get Document Download Data as Buffer Source: https://github.com/onfido/onfido-node/blob/master/README.md Extract the downloaded document data as a Buffer using the slice() method on the FileTransfer object. ```js const blob = download.data.slice(); ``` -------------------------------- ### Configure with API Token Source: https://github.com/onfido/onfido-node/blob/master/README.md Configure the Onfido API client using an API token and specifying the region. Supports EU, US, and CA regions. Default timeout is 30 seconds, can be overridden. ```js const onfido = new DefaultApi( new Configuration({ apiToken: process.env.ONFIDO_API_TOKEN, region: Region.EU, // Supports Region.EU (Europe), Region.US (United States), and Region.CA (Canada) baseOptions: { timeout: 60_000 }, // Additional Axios options (timeout, etc.) }), ); ``` -------------------------------- ### Configure with OAuth2 Client Credentials Source: https://github.com/onfido/onfido-node/blob/master/README.md Configure the Onfido API client using OAuth2 client credentials. The SDK handles token exchange and refresh automatically. Supports EU, US, and CA regions. ```js const onfido = new DefaultApi( new Configuration({ oauthClientId: process.env.ONFIDO_OAUTH_CLIENT_ID, oauthClientSecret: process.env.ONFIDO_OAUTH_CLIENT_SECRET, region: Region.EU, // Supports Region.EU (Europe), Region.US (United States), and Region.CA (Canada) baseOptions: { timeout: 60_000 }, // Additional Axios options (timeout, etc.) }), ); ``` -------------------------------- ### Upload Document with FileTransfer Source: https://github.com/onfido/onfido-node/blob/master/README.md Upload a document using the FileTransfer class, providing the document buffer, filename, applicant ID, and document type. The FileTransfer can be initialized with a buffer or a file path. ```js onfido.uploadDocument( "passport", "", new FileTransfer(Buffer.from(document.buffer), document.filename), ); // FileTransfer object can also be created from an existing file, e.g. new FileTransfer("path/to/passport.png") ``` -------------------------------- ### Import Package (TypeScript) Source: https://github.com/onfido/onfido-node/blob/master/README.md Import the necessary components from the Onfido API package for TypeScript usage. ```ts import { DefaultApi, Configuration, Region, WebhookEventVerifier, } from "@onfido/api"; import { isAxiosError } from "axios"; ``` -------------------------------- ### Require Package (JavaScript) Source: https://github.com/onfido/onfido-node/blob/master/README.md Require the necessary components from the Onfido API package for JavaScript usage. ```js const { DefaultApi, Configuration, WebhookEventVerifier, } = require("@onfido/api"); const { isAxiosError } = require("axios"); ``` -------------------------------- ### Access Document Download Content Type Source: https://github.com/onfido/onfido-node/blob/master/README.md Retrieve the content type of a downloaded document. The download object is an instance of a FileTransfer. ```js download.headers["content-type"]; ``` -------------------------------- ### Verify Webhook Event Signature Source: https://github.com/onfido/onfido-node/blob/master/README.md Verifies the signature of an incoming webhook event payload to ensure its authenticity and integrity. It requires the webhook secret token and the event signature. Handles invalid signatures by catching OnfidoInvalidSignatureError. ```javascript (async () => { try { const token = process.env.ONFIDO_WEBHOOK_SECRET_TOKEN; const verifier = new WebhookEventVerifier(token); const signature = "a0...760e"; const event = verifier.readPayload(`{"payload":{"r...3"}}`, signature); } catch (e) { if (e instanceof OnfidoInvalidSignatureError) { // Invalid webhook signature } } })(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.