mpesajs
Version:
A Node.js SDK for seamless integration with M-Pesa payment gateway, providing easy-to-use methods for handling transactions, payments, and API interactions
71 lines (70 loc) • 2.75 kB
TypeScript
/**
* Auth class handles authentication with the M-Pesa API
* by generating access tokens using consumer credentials.
* This class provides functionality to generate OAuth tokens
* required for authenticating API requests to the M-Pesa system.
*/
export declare class Auth {
/** Consumer key provided by M-Pesa developer portal */
private consumerKey;
/** Consumer secret provided by M-Pesa developer portal */
private consumerSecret;
/**
* Base URL for the token generation endpoint.
* Different URLs are used for sandbox and production environments.
*/
private baseUrl;
private readonly rateLimiter;
/**
* Creates an instance of Auth class
* @param consumerKey - The consumer key obtained from M-Pesa developer portal
* @param consumerSecret - The consumer secret obtained from M-Pesa developer portal
* @param sandbox - Boolean flag to determine environment:
* true for sandbox/testing environment
* false for production environment
*/
constructor(consumerKey?: string, consumerSecret?: string, sandbox?: boolean);
/**
* Generates an access token for M-Pesa API authentication.
* The method:
* 1. Combines consumer key and secret
* 2. Base64 encodes the credentials
* 3. Makes HTTP GET request to token endpoint
* 4. Processes the response to extract token
*
* @returns Promise containing an object with:
* - token: The access token string for API authentication
* - expiresIn: Token validity period in seconds
*
* @throws MpesaError with specific error codes and messages based on the API response
* @throws Error for network issues or invalid responses
*
* @example
* ```typescript
* const auth = new Auth('your-key', 'your-secret');
* const {token, expiresIn} = await auth.generateToken();
* ```
*/
/**
* Generates an authentication token for M-Pesa API access.
*
* This method:
* 1. Combines the consumer key and secret
* 2. Base64 encodes the credentials
* 3. Makes an HTTP GET request to the token endpoint
* 4. Returns the access token and expiry time
*
* @returns Promise containing an object with:
* - token: The access token string for API authentication
* - expiresIn: Token validity period in seconds
*
* @throws {Error} If network connection fails or no response received
* @throws {Error} If request setup fails
* @throws {Error} If API returns error response
*/
generateToken(): Promise<{
token: string;
tokenType: string;
expiresIn: number;
}>;
}