0.1.2 • Published 4 months ago

leja-recharge-node-sdk v0.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

Leja Recharge Node.js SDK

A Node.js SDK for interacting with the Leja Recharge API.

npm version License: MIT

Documentation

Take a look at the API docs here

Installation

npm install leja-recharge-node-sdk
# or
yarn add leja-recharge-node-sdk

Authentication

The SDK uses Basic Authentication with your Client ID and API key (Both found on your dashboard). You'll need both credentials to initialize the client:

import { LejaRechargeClient } from 'leja-recharge-node-sdk';

// Initialize the client with your credentials
const client = new LejaRechargeClient({
  clientId: 'your_client_id',
  apiKey: 'your_api_key'
});

Quick Start

import { LejaRechargeClient, AirtimeRecipient } from 'leja-recharge-node-sdk';

// Initialize the client with your credentials
const client = new LejaRechargeClient({
  clientId: 'your_client_id',
  apiKey: 'your_api_key'
});

// Purchase airtime
const recipients: AirtimeRecipient[] = [
  {
    phoneNumber: '+254712345678',
    amount: 10.0,
    country: 'KE'
  }
];

const transaction = await client.purchaseAirtime(
  '+254712345678',
  recipients,
  true,
  'https://your-callback-url.com/webhook'
);

// Get transaction details
const transactionDetails = await client.getTransaction(transaction.trackingId);

// Get account balance
const balance = await client.getBalance();

// Get supported countries
const countries = await client.getCountries(1, 20);

Features

  • Purchase airtime for multiple recipients
  • Get transaction details
  • Check account balance
  • List supported countries
  • Async support for airtime purchases
  • Webhook callbacks for async transactions
  • Comprehensive error handling
  • TypeScript support with type definitions
  • Request and response validation using Zod

Error Handling

The SDK provides custom exceptions for different error scenarios:

import {
  LejaRechargeError,
  AuthenticationError,
  ValidationError,
  APIError,
  NetworkError
} from 'leja-recharge-node-sdk';

try {
  const client = new LejaRechargeClient({
    clientId: 'invalid_client_id',
    apiKey: 'invalid_api_key'
  });
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.error(`Authentication failed: ${error.message}`);
  }
}

Exception Types

  • LejaRechargeError: Base exception class for all SDK errors
  • AuthenticationError: Raised when authentication fails
  • ValidationError: Raised when input validation fails
  • APIError: Raised when the API returns an error response
  • NetworkError: Raised when network communication fails

TypeScript Support

The SDK is written in TypeScript and provides type definitions for all models:

import {
  AirtimeRecipient,
  TransactionResponse,
  BalanceResponse,
  CountryResponse,
  PaginatedResponse
} from 'leja-recharge-node-sdk';

Development

  1. Clone the repository

    git clone https://github.com/Asilimia/leja-recharge-node-sdk.git
    cd leja-recharge-node-sdk
  2. Install dependencies:

    npm install
    # or
    yarn install
  3. Build the project:

    npm run build
    # or
    yarn build
  4. Run tests:

    npm test
    # or
    yarn test

Testing

The test suite includes:

  • Unit tests for all SDK functionality
  • Mock tests for API interactions
  • Model validation tests
  • Error handling tests
  • Integration test examples

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT License

0.1.2

4 months ago

0.1.1

4 months ago

0.1.0

4 months ago