SDK Documentation

Complete guide to using the Fayda Authentication JavaScript/TypeScript SDK.

Installation
Install the Fayda Authentication SDK using your favorite package manager.
bash
# Using npm
npm i fayda-auth

# Using yarn
yarn add fayda-auth

# Using pnpm
pnpm add fayda-auth
Quick Start
Get started with the SDK in just a few lines of code.
javascript
import { FaydaAuth } from 'fayda-auth';

// Initialize the SDK with your API key
const fayda = new FaydaAuth({
  apiKey: 'your-api-key-here'
});

// Complete authentication flow
async function authenticateUser(fcn) {
  try {
    // Step 1: Initiate OTP
    const otpResult = await fayda.initiateOTP(fcn);
    console.log('OTP sent to:', otpResult.maskedMobile);
    
    // Step 2: Get OTP from user (implementation depends on your UI)
    const otp = await getUserOTPInput();
    
    // Step 3: Verify OTP
    const verifyResult = await fayda.verifyOTP(otpResult.transactionId, otp, fcn);
    console.log('User authenticated:', verifyResult.user.fullName[0].value);
    
    return verifyResult.user;
  } catch (error) {
    console.error('Authentication failed:', error.message);
    throw error;
  }
}
Configuration
The FaydaAuth constructor accepts a configuration object with the following options.
OptionTypeRequiredDefaultDescription
apiKeyStringYes-Your API key from the developer dashboard.
baseURLStringNohttps://fayda-auth.vercel.appBase URL for the our API.
timeoutNumberNo30000Request timeout in milliseconds.
retriesNumberNo3Number of retry attempts for failed requests.
debugBooleanNofalseEnable debug logging.
API Methods
Core methods available in the SDK for authentication.

initiateOTP(fcn)

Initiates OTP verification with a Fayda Card Number.

javascript
const result = await fayda.initiateOTP('1234567890123456');
console.log('Transaction ID:', result.transactionId);
console.log('OTP sent to:', result.maskedMobile);

verifyOTP(transactionId, otp, fcn)

Verifies an OTP code and returns user data if successful.

javascript
const result = await fayda.verifyOTP(transactionId, '123456', '1234567890123456');
console.log('User:', result.user.fullName[0].value);
console.log('UIN:', result.user.uin);

// Access photo and QR code
const photoBase64 = result.photo.data;
const qrCodeBase64 = result.qrCode.data;
Error Handling
The SDK throws standardized error objects for different failure scenarios.
javascript
try {
  const result = await fayda.initiateOTP('invalid-fcn');
} catch (error) {
  console.log('Error type:', error.error);
  console.log('Message:', error.message);
  console.log('Status code:', error.statusCode);
  
  // Handle specific error types
  switch (error.error) {
    case 'INVALID_FCN':
      // Handle invalid FCN format
      break;
    case 'FCN_NOT_FOUND':
      // Handle FCN not found
      break;
    case 'RATE_LIMITED':
      // Handle rate limiting
      break;
    case 'NETWORK_ERROR':
      // Handle network issues
      break;
    default:
      // Handle other errors
      break;
  }
}
View complete error reference →
TypeScript Support
The SDK includes full TypeScript definitions. Here are the main types.
typescript
import { 
  FaydaAuth, 
  FaydaConfig, 
  OTPInitiateResponse, 
  OTPVerifyResponse,
  FaydaUser,
  FaydaError 
} from '@fayda/auth';

// Configuration type
interface FaydaConfig {
  apiKey: string;
  timeout?: number;
  retries?: number;
  debug?: boolean;
}

// User data type
interface FaydaUser {
  uin: string;
  fullName: Array<{ language: string; value: string }> ;
  dateOfBirth: string;
  gender: Array<{ language: string; value: string }> ;
  phone: string;
  region: Array<{ language: string; value: string }> ;
  zone: Array<{ language: string; value: string }> ;
  woreda: Array<{ language: string; value: string }> ;
}

// Error type
interface FaydaError extends Error {
  error: string;
  statusCode: number;
  details?: any;
  timestamp: string;
}
Best Practices
Recommendations for security, performance, and user experience.
More Examples
For more comprehensive examples and use cases, visit our examples section.