Skip to content
newtonmunene99Public

Repository files navigation

Mpesa-Api

A typed client for Safaricom's M-Pesa Daraja 3.0 API. It has no runtime dependencies and runs on Node.js, Bun, Deno and edge runtimes such as Cloudflare Workers.

Badge
CI CI
Latest Latest
Install size Install size
Node Node

Documentation: newtonmunene99.github.io/mpesa-api

Supported APIs:

Installation

npm i mpesa-api
# or
pnpm add mpesa-api
# or
yarn add mpesa-api

Versions

  • 4.x targets Daraja 3.0. It is ESM only and needs Node.js 22.12+ (or Bun, Deno or an edge runtime). Upgrading? See Migrating from 3.x.
  • 3.x supports CommonJS (require) and older Node versions. Install it with npm i mpesa-api@3. Bug and security fixes for 3.x are maintained on the v3.x branch.

Quick start

import { createMpesa } from 'mpesa-api';

const mpesa = createMpesa({
  environment: 'sandbox',
  consumerKey: process.env.MPESA_CONSUMER_KEY!,
  consumerSecret: process.env.MPESA_CONSUMER_SECRET!,
  passkey: process.env.MPESA_PASSKEY!,
});

const { checkoutRequestId } = await mpesa.stkPush.send({
  shortCode: 174379,
  type: 'paybill',
  amount: 1,
  phoneNumber: '0708374149',
  callbackUrl: 'https://example.com/payments/stk',
  accountReference: 'INV-001',
});
console.log(checkoutRequestId);

createMpesa makes no network calls. The access token is fetched on the first API call, cached, and refreshed before it expires.

Next, get your Daraja credentials and read the configuration and certificates guides. Callbacks are posted to your server, so it must be reachable over HTTPS; see IP whitelisting.

Contributing

Contributions are welcome. See CONTRIBUTING.md for setup, the checks to run, and how releases work. Please follow the Code of Conduct, and report security issues privately as described in SECURITY.md.

Credits

Name Role
Newton Munene Contributor
Nelson Bwogora Contributor

License

MIT License

Copyright (c) 2018 Newton Munene

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Releases

Sponsor this project

Packages

Used by

Contributors

Languages