npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

daraja.js

v1.0.5

Published

A wrapper for seamless integration with Mpesa's Daraja API

Downloads

590

Readme

Daraja.js

A node.js wrapper for seamless integration with Mpesa's Daraja API. This library provides a builder-style interface to enhance the ease of use. Visit https://daraja-sdks.github.io to find implementations in other languages and more documentation.

Integrated APIs:

  • ✅ Mpesa Customer to Business (C2B)
  • ✅ Mpesa Express/Lipa na Mpesa Online/STK Push
  • ✅ Lipa na Mpesa Query
  • ✅ Mpesa Business to Customer (B2C)
  • ✅ Mpesa Transaction Status query
  • ✅ Mpesa Account Balance query
  • ✅ Mpesa Reversals

Documentation

More In-depth documentation on library usage can be found here. Also, full-blown examples on library usage can be found at the daraja-sdks github repos.

Creating an app instance

To get started, create an instance of the Mpesa class:

// with es-modules syntax
import { Mpesa } from "daraja.js"

// with common-modules syntax
const Mpesa = require("daraja.js").Mpesa

// create an app instance
const app = new Mpesa({
    consumerKey: process.env.APP_KEY, // required
    consumerSecret: process.env.APP_SECRET, // required
    initiatorPassword: "Safaricom999!*!", // required only in production
    organizationShortCode: 174379, // optional
    certificatePath: "some/path", // optional
    securityCredential: "someSecureCredential" // optional
})

STK Push / Lipa na Mpesa Online / Mpesa Express API:

// ...

const res = await app
  .stkPush()
  .amount(1)
  .callbackURL("https://example.com/callback")
  .phoneNumber(254708374149)
  .lipaNaMpesaPassKey(process.env.LNM_PASSKEY)
  .send();

console.log(res.isOkay());

STK Query API:

// ...

const res = await app
  .stkPush()
  .shortCode("174379")
  .checkoutRequestID("ws_CO_DMZ_123212312_2342347678234")
  .lipaNaMpesaPassKey(process.env.LNM_PASSKEY)
  .queryStatus(); // sends the query request

Customer to business(C2B) simulate API:

// ...

const res = await app
  .c2b()
  .shortCode("600998")
  .accountNumber("Bill payment")
  .amount(1)
  .phoneNumber(254708374149)
  .simulate();

Customer to business(C2B) Register URLs API:

// ...

const res = await app
  .c2b()
  .shortCode("600998")
  .confirmationURL("https://example.com/callback")
  .validationURL("https://example.com/callback")
  .callbackURL("https://example.com/callback")
  .registerURLS();

Business to customer(B2C) API:

// ...

const res = await app
  .b2c()
  .amount(1)
  .phoneNumber(254708374149)
  .shortCode("600982")
  .resultURL("https://example.com/callback")
  .timeoutURL("https://example.com/callback")
  .send();

Transaction Status API:

// ...

const res = await app
  .transactionStatus()
  .shortCode("600998")
  .transactionID("OEI2AK4Q16")
  .timeoutURL("https://example.com/callback")
  .resultURL("https://example.com/callback")
  .queryStatus();

Account Balance API:

// ...

const res = await app
  .accountBalance()
  .shortCode("600998")
  .timeoutURL("https://example.com/callback")
  .resultURL("https://example.com/callback")
  .query();

Reversals API:

// ...

const res = await app
  .reversal()
  .amount(1)
  .shortCode("600998")
  .initiator("testapi")
  .transactionID("OEI2AK4Q16")
  .resultURL("https://example.com/callback")
  .timeoutURL("https://example.com/callback")
  .send();