Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@onfido/api

onfido76.2kMIT4.5.0TypeScript support: included

Node.js library for the Onfido API

axios, typescript, openapi-client, openapi-generator, @onfido/api, onfido, identity, verification, api

readme

Onfido Node.js Library

The official Node.js library for integrating with the Onfido API.

Documentation can be found at https://documentation.onfido.com.

This library is only for use on the backend, as it uses Onfido API tokens which must be kept secret. If you do need to collect applicant data in the frontend of your application, we recommend that you use the Onfido SDKs: iOS, Android, Web, and React Native.

This version uses Onfido API v3.6. Refer to our API versioning guide for details of which client library versions use which versions of the API.

npm version Build Status

Installation & Usage

Installation

Npm

npm install @onfido/api

Yarn

yarn add @onfido/api

Getting Started

Require the package:

const {
  DefaultApi,
  Configuration,
  WebhookEventVerifier
} = require("@onfido/api");
const { isAxiosError } = require("axios");

For TypeScript users, types are available as well:

import {
  DefaultApi,
  Configuration,
  Region,
  WebhookEventVerifier
} from "@onfido/api";
import { isAxiosError } from "axios";

Configure with your API token and region:

const onfido = new DefaultApi(
  new Configuration({
    apiToken: process.env.ONFIDO_API_TOKEN,
    region: Region.EU, // Supports Region.EU, Region.US and Region.CA
    baseOptions: { timeout: 60_000 } // Additional Axios options (timeout, etc.)
  })
);

NB: by default, timeout is set to 30 seconds.

Making a call to the API

Using async/await (in an async function):

(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);
    }
  }
})();

Please find more information regarding Axios errors in library documentation.

Using promises:

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.
  });

File download

File downloads, for example onfido.downloadDocument(documentId), will return an instance of a FileTransfer object.

This object will have a content type, e.g. image/png.

download.headers["content-type"];

Call slice() to get a Buffer of the download:

const blob = download.data.slice();

File upload

File upload should make use of the provided FileTransfer class, e.g.:

onfido.uploadDocument(
  "passport",
  "<APPLICANT_ID>",
  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").

Webhook event verification

Webhook events payload needs to be verified before it can be accessed. Library allows to easily decode the payload and verify its signature before returning it as an object for user convenience:

(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
    }
  }
})();

Recommendations

Do not use square bracket syntax

Except for accessing Task object's outputs, retain from using the square bracket syntax (i.e. []) to access not defined properties to avoid breaking changes when these fields will appear.

Contributing

This library is automatically generated using OpenAPI Generator (version: 7.9.0); therefore all the contributions, except tests files, should target Onfido OpenAPI specification repository instead of this repository.

For contributions to the tests instead, please follow the steps below:

  1. Fork repository
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Make your changes
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request

Versioning policy

Semantic Versioning policy is used for library versioning, following guidelines and limitations below:

  • MAJOR versions (x.0.0) might:
    • target a new API version
    • include non-backward compatible change
  • MINOR versions (0.x.0) might:
    • add a new functionality, non-mandatory parameter or property
    • deprecate an old functionality
    • include non-backward compatible change to a functionality which is:
      • labelled as alpha or beta
      • completely broken and not usable
  • PATCH version (0.0.x) might:
    • fix a bug
    • include backward compatible changes only

More documentation

More documentation and code examples can be found at https://documentation.onfido.com.

Support

Should you encounter any technical issues during integration, please contact Onfido's Customer Support team via the Customer Experience Portal which also includes support documentation.

changelog

Changelog

v4.4.0 20th December 2024

  • Release based on Onfido OpenAPI spec version v4.4.0:
    • [CAT-1593] Fix missing webhook type and evidence folder webhook
    • [CAT-1590] Allow any type for the workflow task output

v4.3.0 27th November 2024

  • Release based on Onfido OpenAPI spec version v4.3.0:
    • [CAT-1581] Revert "[CAT-1528] Fix barcode field in document properties object"

v4.2.0 19th November 2024

  • Release based on Onfido OpenAPI spec version v4.2.0:
    • feat: add new fields to facial similarity report objects
    • [CAT-1552] Add missing document types

v4.1.0 8th November 2024

  • Release based on Onfido OpenAPI spec version v4.1.0:
    • [CAT-1528] Fix barcode field in document properties object

v4.0.0 23rd October 2024

  • Release based on Onfido OpenAPI spec version v4.0.0:
    • [CAT-1379] Add a few missing properties
    • [CAT-1447] Fix applicant consents
    • [CAT-1379] Fix check creation, remove some deprecated properties and deprecate others
    • Use document-type enum for document upload
    • [CAT-1306] Add webhooks event resource
    • Upgrade OpenAPI generator to v7.9.0 (was v7.6.0)
    • Migration Guide

v3.6.0 20th September 2024

  • Release based on Onfido OpenAPI spec version v3.5.0:
    • [CAT-1376] Add record item object definition for watchlist enhanced properties field

v3.5.0 6th September 2024

  • Release based on Onfido OpenAPI spec version v3.4.0:
    • Fix error raised when uploading media with location in typescript
    • Fix examples in templated README in typescript
    • SDK Token: Remove UUID format from application_id
  • test: fix workflow-runs test
  • docs: update README with the new onfido version

v3.4.0 24th July 2024

  • Release based on Onfido OpenAPI spec version v3.3.0:
    • Expose customer_user_id in workflow_runs
    • adding sdk_token to workflow run schema

v3.3.0 17th July 2024

  • Release based on Onfido OpenAPI spec version v3.2.0:
    • [CAT-1289] Document report's properties: Add middle name
    • chore(qes): add documents endpoint
    • [CAT-1297] Webhook Event: remove uuid format from object.id
    • fix(qes): fix download document http method
    • Add started_at_iso8601 field in webhook event
    • add jpeg file type for documents

v3.2.0 2nd July 2024

  • Release based on Onfido OpenAPI spec version v3.1.0:
    • Add missing fields in document report's properties

v3.1.0 24th June 2024

  • Library has been updated and automatically generated from Onfido OpenAPI Spec (release v3.0.0)
  • Integration tests have been refreshed and API coverage increased

v3.0.0 6th May 2024 (pre-release)

v2.9.0 24 November 2023

  • Add resource to WebhookEvent

v2.8.0 24 November 2023

  • Added signed_evidence_file method for WorkflowRuns

v2.7.2, 14 June 2023

  • Added sandbox field to Check.

v2.7.1, 30 May 2023

  • Fix bug when uploading documents with validateImageQuality.

v2.7.0, 24 January 2023

  • Updated to use API v3.6. For more details please see our release notes

v2.6.0, 7 December 2022

  • Added WorkflowRuns support

v2.5.0, 22 November 2022

  • Added Motion capture support

v2.4.0, 06 October 2022

  • Updated to use API v3.5. For more details please see our release notes

v2.3.1, 24 May 2022

  • Reinstated privacyNoticesReadConsentGiven parameter

v2.3.0, 10 May 2022

  • Updated to use API v3.4. For more details please see our release notes
  • Added location to applicant request, applicant response and documents request. location is a mandatory parameter for all applicants in order to create a check using API v3.4
  • Added consents to applicant request. consents is a mandatory parameter for any applicant located in the United States in order to create a check using API v3.4
  • Added phoneNumber to applicant request and response

v2.2.0, 02 March 2022

  • Updated to use API v3.3

v2.1.2, 17 Feb 2022

  • Fixed advancedValidation flag when uploading live photo

v2.1.1, 6 Jan 2022

  • Added validate_image_quality to document upload request

v2.1.0, 24 June 2021

  • Updated to use API v3.2

v2.0.2, 25 May 2021

  • Added environments to Webhook object

v2.0.1, 17 May 2021

  • Added sub_result to trigger sandbox pre-determined responses for Document report sub-results
  • Added consider array functionality for sandbox pre-determined responses

v2.0.0, 9 April 2021

  • Updated to use API v3.1
  • Region is now a required argument when constructing an Onfido instance. The region previously defaulted to Region.EU
  • Add support for downloading a check

v1.6.0, 8 March 2021

  • Add privacy_notices_read_consent_given to the type for creating a chec

v1.5.4, 12 January 2021

  • Updated dependencies.

v1.5.3, 3 December 2020

  • Library should work now with esModuleInterop: false

v1.5.2, 3 September 2020

  • Fix type declaration location for TypeScript users

v1.5.1, 20 August 2020

  • Ensure consistent version in User-Agent

v1.5.0, 14 August 2020

  • Add User Agent header

v1.4.0, 15 July 2020

v1.3.0, 30 June 2020

  • Add support for cross_device_url when generating an SDK token

v1.2.0, 23 June 2020

  • Allow specifying the filepath and content type explicitly for file upload
  • Add support for the Canada region (api.ca.onfido.com)
  • Add missing document_ids field to CheckRequest type

v1.1.0, 2 April 2020

  • Improved typing of check and report objects.

v1.0.0, 23 January 2020

  • The initial release of this library, which uses v3 of the Onfido API