onfido

package module
v0.0.0-...-ff1d90d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 14 Imported by: 0

README

Onfido Go SDK

CodeQL Go Report Card Go Reference

An unofficial Go SDK for interacting with the Onfido API. This SDK was created because Onfido does not provide an official Go SDK for developers.

Installation

go get -u github.com/besafe-labs/onfido-go-sdk

Usage


package main

import (
	"context"
	"log"

	"github.com/besafe-labs/onfido-go-sdk"
)

func main() {
	client, err := onfido.NewClient("your-api-token")
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// Create an applicant
	applicant, err := client.CreateApplicant(context.Background(), onfido.CreateApplicantPayload{
		FirstName: "John",
		LastName:  "Doe",
	})
	if err != nil {
		log.Fatal(err)
	}

	// Create a workflow run
	workflowRun, err := client.CreateWorkflowRun(context.Background(), onfido.CreateWorkflowRunPayload{
		ApplicantID: applicant.ID,
		WorkflowID:  "your-workflow-id",
	})
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Workflow run created: %s", workflowRun.ID)
}

Available Resources

Applicants
  • All endpoints related to applicants
Workflow Runs
  • All endpoints related to workflow runs
Documents
  • All endpoints related to documents

Features

  • Automatic retries with configurable retry count and wait time
  • Region-specific endpoints (EU, US, CA)
  • Pagination support
  • Comprehensive error handling
  • Context support for cancellation and timeouts

Configuration Options

// Configure region
client, err := onfido.NewClient(token, onfido.WithRegion(onfido.API_REGION_US))

// Configure retries
client, err := onfido.NewClient(token, onfido.WithRetries(3, 5*time.Second))

Error Handling

The SDK provides detailed error information through the OnfidoError struct:

type OnfidoError struct {
    Type    string
    Message string
    Fields  map[string]any
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Tests

This SDK directly tests with the onfido API. To run the tests, you need to provide your API token as an environment variable:

# Set environment variables
export ONFIDO_API_TOKEN=your-api-token
export ONFIDO_WORKFLOW_ID=your-workflow-id

# Run tests
go test -v
  • go-onfido - Another version of go SDK for Onfido API. This sdk was found after bootstrapping this SDK project.

License

MIT License

Documentation

Index

Constants

View Source
const (
	// CURRENT_CLIENT_VERSION is the current version of the Go-Onfido client
	CURRENT_CLIENT_VERSION = "1.0.0"
	// LATEST_API_VERSION is the latest version of the Onfido API
	LATEST_API_VERSION = "v3.6"
	// DEFAULT_API_REGION is the default region for the Onfido API
	DEFAULT_API_REGION = API_REGION_EU
)
View Source
const (
	// API_REGION_EU is the EU region for the Onfido API
	API_REGION_EU apiRegion = "eu"
	// API_REGION_US is the US region for the Onfido API
	API_REGION_US apiRegion = "us"
	// API_REGION_CA is the CA region for the Onfido API
	API_REGION_CA apiRegion = "ca"
)
View Source
const (
	// SortAsc is the ascending sort direction
	SortAsc sortDirection = "asc"
	// SortDesc is the descending sort direction
	SortDesc sortDirection = "desc"
)

Variables

View Source
var ErrInvalidId = &OnfidoError{Type: "validation_error", Message: "id is required"}

Functions

This section is empty.

Types

type Address

type Address struct {
	Country        string `json:"country,omitempty"`
	Postcode       string `json:"postcode,omitempty"`
	FlatNumber     string `json:"flat_number,omitempty"`
	BuildingNumber string `json:"building_number,omitempty"`
	BuildingName   string `json:"building_name,omitempty"`
	Street         string `json:"street,omitempty"`
	SubStreet      string `json:"sub_street,omitempty"`
	Town           string `json:"town,omitempty"`
	State          string `json:"state,omitempty"`
	Line1          string `json:"line1,omitempty"`
	Line2          string `json:"line2,omitempty"`
	Line3          string `json:"line3,omitempty"`
}

type Applicant

type Applicant struct {
	ID          string     `json:"id,omitempty"`
	Email       string     `json:"email,omitempty"`
	Dob         string     `json:"dob,omitempty"`
	IdNumbers   []IdNumber `json:"id_numbers,omitempty"`
	PhoneNumber string     `json:"phone_number,omitempty"`
	FirstName   string     `json:"first_name,omitempty"`
	LastName    string     `json:"last_name,omitempty"`
	CreatedAt   *time.Time `json:"created_at,omitempty"`
	UpdatedAt   *time.Time `json:"updated_at,omitempty"`
	Href        string     `json:"href,omitempty"`
	Sandbox     bool       `json:"sandbox,omitempty"`
	Address     *Address   `json:"address,omitempty"`
	Location    *Location  `json:"location,omitempty"`
}

Applicant represents an applicant in the Onfido API

type Client

type Client struct {
	Endpoint  string
	Retries   int
	RetryWait time.Duration
	// contains filtered or unexported fields
}

Client is a client for the Onfido API

func NewClient

func NewClient(apiToken string, opts ...ClientOption) (*Client, error)

NewClient creates a new Client

func (*Client) Close

func (c *Client) Close()

Close closes the idle connections of the underlying HTTP client.

The client can be reused after closing as per the http.Client documentation.

func (*Client) CreateApplicant

func (c *Client) CreateApplicant(ctx context.Context, payload CreateApplicantPayload) (*Applicant, error)

CreateApplicant creates a new applicant in the Onfido API

func (*Client) CreateWorkflowRun

func (c *Client) CreateWorkflowRun(ctx context.Context, payload CreateWorkflowRunPayload) (*WorkflowRun, error)

CreateWorkflowRun creates a new workflow run in the Onfido API

func (*Client) DeleteApplicant

func (c *Client) DeleteApplicant(ctx context.Context, applicantId string) error

DeleteApplicant deletes an applicant from the Onfido API

func (*Client) DownloadDocument

func (c *Client) DownloadDocument(ctx context.Context, documentId string) ([]byte, error)

func (*Client) DownloadDocumentNFCFace

func (c *Client) DownloadDocumentNFCFace(ctx context.Context, documentId string) ([]byte, error)

func (*Client) DownloadDocumentVideo

func (c *Client) DownloadDocumentVideo(ctx context.Context, documentId string) ([]byte, error)

func (*Client) ListApplicants

func (c *Client) ListApplicants(ctx context.Context, opts ...IsListApplicantOption) ([]Applicant, *PageDetails, error)

ListApplicants retrieves all applicants from the Onfido API

func (*Client) ListDocuments

func (c *Client) ListDocuments(ctx context.Context, applicantId string) ([]Document, *PageDetails, error)

ListDocuments retrieves a list of documents from the Onfido API

func (*Client) ListWorkflowRuns

func (c *Client) ListWorkflowRuns(ctx context.Context, opts ...IsListWorkflowRunOption) ([]WorkflowRun, *PageDetails, error)

ListWorkflowRuns retrieves a list of workflow runs from the Onfido API

func (*Client) RestoreApplicant

func (c *Client) RestoreApplicant(ctx context.Context, applicantId string) error

RestoreApplicant restores a deleted applicant in the Onfido API

func (*Client) RetrieveApplicant

func (c *Client) RetrieveApplicant(ctx context.Context, applicantId string) (*Applicant, error)

RetrieveApplicant retrieves an applicant from the Onfido API

func (*Client) RetrieveDocument

func (c *Client) RetrieveDocument(ctx context.Context, documentId string) (*Document, error)

RetrieveDocument retrieves a document from the Onfido API

func (*Client) RetrieveWorkflowRun

func (c *Client) RetrieveWorkflowRun(ctx context.Context, workflowRunID string) (*WorkflowRun, error)

RetrieveWorkflowRun retrieves a workflow run from the Onfido API

func (*Client) RetrieveWorkflowRunEvidenceSummaryFile

func (c *Client) RetrieveWorkflowRunEvidenceSummaryFile(ctx context.Context, workflowRunID string) (*WorkflowRunEvidenceSummary, error)

RetrieveWorkflowRunEvidenceSummaryFile retrieves the signed evidence file for a workflow run

func (*Client) UpdateApplicant

func (c *Client) UpdateApplicant(ctx context.Context, applicantId string, payload CreateApplicantPayload) (*Applicant, error)

UpdateApplicant updates an existing applicant in the Onfido API

func (*Client) UploadDocument

func (c *Client) UploadDocument(ctx context.Context, payload UploadDocumentPayload) (*Document, error)

UploadDocument uploads a document to the Onfido API

type ClientOption

type ClientOption func(*clientOptions)

func WithRegion

func WithRegion(region apiRegion) ClientOption

func WithRetries

func WithRetries(retries int, wait time.Duration) ClientOption
type Consent struct {
	Granted bool   `json:"granted,omitempty"`
	Name    string `json:"name,omitempty"`
}

type CreateApplicantPayload

type CreateApplicantPayload struct {
	// FirstName is the first name of the applicant, required
	FirstName string `json:"first_name,omitempty"`
	// LastName is the last name of the applicant, required
	LastName    string     `json:"last_name,omitempty"`
	Email       string     `json:"email,omitempty"`
	Dob         time.Time  `json:"dob,omitempty"`
	IdNumbers   []IdNumber `json:"id_numbers,omitempty"`
	PhoneNumber string     `json:"phone_number,omitempty"`
	Consents    []Consent  `json:"consents,omitempty"`
	Address     *Address   `json:"address,omitempty"`
	Location    *Location  `json:"location,omitempty"`
}
type CreateWorkflowRunLink struct {
	CompletedRedirectURL string     `json:"completed_redirect_url,omitempty"`
	ExpiredRedirectURL   string     `json:"expired_redirect_url,omitempty"`
	ExpiresAt            *time.Time `json:"expires_at,omitempty"`
	Language             string     `json:"language,omitempty"`
}

type CreateWorkflowRunPayload

type CreateWorkflowRunPayload struct {
	ApplicantID    string                 `json:"applicant_id,omitempty"`
	WorkflowID     string                 `json:"workflow_id,omitempty"`
	Tags           []string               `json:"tags,omitempty"`
	CustomerUserID string                 `json:"customer_user_id,omitempty"`
	Link           *CreateWorkflowRunLink `json:"link,omitempty"`
	CustomData     map[string]any         `json:"custom_data,omitempty"`
}

type Document

type Document struct {
	ID             string       `json:"id,omitempty"`
	FileType       string       `json:"file_type,omitempty"`
	Type           DocumentType `json:"type,omitempty"`
	Side           string       `json:"side,omitempty"`
	IssuingCountry string       `json:"issuing_country,omitempty"`
	ApplicantID    string       `json:"applicant_id,omitempty"`
	CreatedAt      time.Time    `json:"created_at,omitempty"`
	Href           string       `json:"href,omitempty"`
	DownloadHref   string       `json:"download_href,omitempty"`
	FileName       string       `json:"file_name,omitempty"`
	FileSize       int          `json:"file_size,omitempty"`
}

Document represents a document in the Onfido API

type DocumentSide

type DocumentSide string
const (
	DocumentSideFront DocumentSide = "front"
	DocumentSideBack  DocumentSide = "back"
)

type DocumentType

type DocumentType string

DocumentType represents the type of document

  • The document types declared here are not exhaustive, the API may support more types
const (
	DocumentTypeUnknown              DocumentType = "unknown"
	DocumentTypePassport             DocumentType = "passport"
	DocumentTypeDrivingLicence       DocumentType = "driving_licence"
	DocumentTypeNationalIdentityCard DocumentType = "national_identity_card"
	DocumentTypeResidencePermit      DocumentType = "residence_permit"
	DocumentTypeWorkPermit           DocumentType = "work_permit"
	DocumentTypeVoterID              DocumentType = "voter_id"
	DocumentTypeTaxID                DocumentType = "tax_id"
)

type IdNumber

type IdNumber struct {
	Type      string `json:"type,omitempty"`
	Value     string `json:"value,omitempty"`
	StateCode string `json:"state_code,omitempty"`
}

type IsListApplicantOption

type IsListApplicantOption interface {
	// contains filtered or unexported methods
}

type IsListWorkflowRunOption

type IsListWorkflowRunOption interface {
	// contains filtered or unexported methods
}

type LimitPaginationOption

type LimitPaginationOption func(*limitPaginationOption)

func WithPageLimit

func WithPageLimit(limit int) LimitPaginationOption

type ListApplicantsOption

type ListApplicantsOption func(*listApplicantsOptions)

func WithIncludeDeletedApplicants

func WithIncludeDeletedApplicants() ListApplicantsOption

type ListWorkflowRunOption

type ListWorkflowRunOption func(*listWorkflowRunOptions)

func WithWorkflowRunCreatedAfter

func WithWorkflowRunCreatedAfter(date time.Time) ListWorkflowRunOption

WithWorkflowRunCreatedAfter filters the list of workflow runs to those created after the specified date.

The hour, minute, second and timezone of the date are ignored, onfido throws an error for any other format.

func WithWorkflowRunCreatedBefore

func WithWorkflowRunCreatedBefore(date time.Time) ListWorkflowRunOption

WithWorkflowRunCreatedBefore filters the list of workflow runs to those created before the specified date.

The hour, minute, second and timezone of the date are ignored, onfido throws an error for any other format.

func WithWorkflowRunSort

func WithWorkflowRunSort(sort sortDirection) ListWorkflowRunOption

func WithWorkflowRunStatus

func WithWorkflowRunStatus(status WorkflowRunStatus) ListWorkflowRunOption

func WithWorkflowRunTags

func WithWorkflowRunTags(tags ...string) ListWorkflowRunOption

type Location

type Location struct {
	IpAddress          string `json:"ip_address,omitempty"`
	CountryOfResidence string `json:"country_of_residence,omitempty"`
}

type OnfidoError

type OnfidoError struct {
	Type    string         `json:"type,omitempty"`
	Message string         `json:"message,omitempty"`
	Fields  map[string]any `json:"fields,omitempty"`
}

func (OnfidoError) Error

func (e OnfidoError) Error() string

type PageDetails

type PageDetails struct {
	Total     *int
	Limit     *int
	FirstPage *int
	LastPage  *int
	NextPage  *int
	PrevPage  *int
}

type PaginationOption

type PaginationOption func(*paginationOption)

func WithPage

func WithPage(page int) PaginationOption

type UploadDocumentPayload

type UploadDocumentPayload struct {
	ApplicantID          string       `json:"applicant_id,omitempty"`
	File                 *os.File     `json:"file,omitempty"`
	FileType             string       `json:"file_type,omitempty"`
	Type                 DocumentType `json:"type,omitempty"`
	Side                 DocumentSide `json:"side,omitempty"`
	IssuingCountry       string       `json:"issuing_country,omitempty"`
	Location             *Location    `json:"location,omitempty"`
	ValidateImageQuality bool         `json:"validate_image_quality,omitempty"`
}

type WorkflowRun

type WorkflowRun struct {
	ID                string            `json:"id,omitempty"`
	ApplicantID       string            `json:"applicant_id,omitempty"`
	WorkflowID        string            `json:"workflow_id,omitempty"`
	WorkflowVersionID uint              `json:"workflow_version_id"`
	DashboardURL      string            `json:"dashboard_url,omitempty"`
	Status            WorkflowRunStatus `json:"status,omitempty"`
	Tags              []string          `json:"tags,omitempty"`
	CustomerUserID    string            `json:"customer_user_id,omitempty"`
	Output            map[string]any    `json:"output,omitempty"`
	Reasons           []string          `json:"reasons,omitempty"`
	Error             *OnfidoError      `json:"error,omitempty"`
	SDKToken          string            `json:"sdk_token,omitempty"`
	Link              *WorkflowRunLink  `json:"link,omitempty"`
	CreatedAt         *time.Time        `json:"created_at,omitempty"`
	UpdatedAt         *time.Time        `json:"updated_at,omitempty"`
}

WorkflowRun represents a workflow run in the Onfido API

type WorkflowRunEvidenceSummary

type WorkflowRunEvidenceSummary struct {
	URL string `json:"url,omitempty"`
}

WorkflowRunEvidenceSummary represents the evidence summary file response

type WorkflowRunLink struct {
	URL                   string `json:"url,omitempty"`
	CreateWorkflowRunLink `json:",inline"`
}

type WorkflowRunStatus

type WorkflowRunStatus string

WorkflowRunStatus represents the status of a workflow run

const (
	WorkflowRunStatusProcessing    WorkflowRunStatus = "processing"
	WorkflowRunStatusAwaitingInput WorkflowRunStatus = "awaiting_input"
	WorkflowRunStatusApproved      WorkflowRunStatus = "approved"
	WorkflowRunStatusDeclined      WorkflowRunStatus = "declined"
	WorkflowRunStatusReview        WorkflowRunStatus = "review"
	WorkflowRunStatusAbandoned     WorkflowRunStatus = "abandoned"
	WorkflowRunStatusError         WorkflowRunStatus = "error"
)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL