Mpesa SDK
Installation Guide
Before you begin, ensure you have the following:
- Node.js installed on your machine.
- An active M-Pesa account with the necessary credentials.
- A basic understanding of TypeScript (optional but recommended).
npm
yarn
pnpm
Setup
Note
To use the mpesa sdk, you need to initialize it with your credentials.
Consider storing all these credentials in a .env
file and using dotenv
to load them into your application.
Basic Setup
Using Environment Variables
A more secure approach is to use environment variables. Here's an example using a Config object that loads values from environment variables:
Certificate Structure
When using the certFolderPath
option, you need to organize your certificates
in a specific folder structure. All certificates can be obtained from the
M-Pesa Daraja Portal.
Important
- The SDK will automatically look for certificates in the appropriate
environment subfolder (live or sandbox) - Make sure to download the correct
certificates from the Daraja portal for each environment - The certificate
files must be named
cert.cer
- You only need to provide the path to the parentcerts
folder in thecertFolderPath
configuration
Example folder structure in your project
API Reference
Parameter | Type | Required | Description | Example |
---|---|---|---|---|
env | string | Yes | Specifies the M-Pesa environment to use. Can be either "sandbox" for testing or "live" . | "sandbox" or "live" |
type | number | Yes | Determines the type of transaction. Use 2 for C2B (Customer-to-Business) or 4 for B2C (Business-to-Customer). | 2 , 4 |
shortcode | number | Yes | The M-Pesa shortcode associated with your business. | 4657849 |
store | number | Yes | Alias for shortcode , used to identify the business account receiving payments. | 4657849 |
key | string | Yes | Consumer Key from your M-Pesa API credentials. | "MPESA_B2C_CONSUMER_KEY" |
secret | string | Yes | Consumer Secret from your M-Pesa API credentials. | "MPESA_B2C_CONSUMER_SECRET" |
username | string | No | Username required for certain B2B or B2C operations, The user is created in mpesa org portal. | "USER_NAME" |
password | string | No | Password required for specific B2B or B2C operations, The pass for the user created in mpesa org portal. | "PASSWORD" |
certFolderPath | string | No | Absolute path to the folder containing SSL certificates for secure requests (if required). | "/path/to/certs" |
passkey | string | Yes | The M-Pesa passkey for generating secure payment requests, typically used in C2B transactions. | "MPESA_PASSKEY" |
Note
- All the credentials above can be obtained from the M-Pesa daraja portal. -
- This includes the certificates, which are required for secure communication with the M-Pesa API. - The shortcode and store are typically the same for most businesses. (Buy Goods or Paybill)
IMPORTANT:
Note
- Ensure your consumer key, consumer secret, and passkey match
the environment you are using (
sandbox
orlive
). -certFolderPath
is required for environments that mandate SSL/TLS certificate-based authentication. -type
determines the context of the operations. Using the correct type is critical for accurate integration.