Multi-language SDK for integrating M-Pesa STK Push — Python, Node.js, React & more.
Plug & play SDK for fast payments in your app, backend, or frontend.
Built-in token handling, encrypted credentials, and callback verification.
Supports Python, Flask, Django, Node.js, Express, React & Next.js.
pip install astrapay
npm install astrapay
composer require astrasoftwares/astrapay
# pip install astrapay
from astrapay import AstraMpesa
client = AstraMpesa(
consumer_key="YOUR_KEY",
consumer_secret="YOUR_SECRET",
shortcode="174379",
passkey="YOUR_PASSKEY",
callback_url="https://yourdomain.com/callback"
)
res = client.pay(phone="254712345678", amount=10)
print(res)
// npm install astrapay
from flask import Flask, request, jsonify
from astrapay import AstraMpesa
app = Flask(__name__)
client = AstraMpesa(
consumer_key="YOUR_KEY",
consumer_secret="YOUR_SECRET",
shortcode="174379",
passkey="YOUR_PASSKEY",
callback_url="https://yourdomain.com/callback"
)
@app.route("/pay", methods=["POST"])
def pay():
data = request.json
res = client.pay(phone=data["phone"], amount=data["amount"])
return jsonify(res)
# pip install astrapay
from django.http import JsonResponse
from astrapay import AstraMpesa
client = AstraMpesa(
consumer_key="YOUR_KEY",
consumer_secret="YOUR_SECRET",
shortcode="174379",
passkey="YOUR_PASSKEY",
callback_url="https://yourdomain.com/callback"
)
def pay_view(request):
phone = request.POST.get("phone")
amount = request.POST.get("amount")
res = client.pay(phone, amount)
return JsonResponse(res)
// npm install astrapay
const AstraPay = require("astrapay");
const client = new AstraPay({
consumerKey: "YOUR_KEY",
consumerSecret: "YOUR_SECRET",
shortcode: "174379",
passkey: "YOUR_PASSKEY",
callbackUrl: "https://yourdomain.com/callback"
});
client.pay({ phone: "254712345678", amount: 10 })
.then(console.log)
.catch(console.error);
// npm install astrapay
const express = require("express");
const AstraPay = require("astrapay");
const app = express();
app.use(express.json());
const client = new AstraPay({
consumerKey: "YOUR_KEY",
consumerSecret: "YOUR_SECRET",
shortcode: "174379",
passkey: "YOUR_PASSKEY",
callbackUrl: "https://yourdomain.com/callback"
});
app.post("/pay", async (req, res) => {
const { phone, amount } = req.body;
const result = await client.pay({ phone, amount });
res.json(result);
});
app.listen(3000);
// React frontend using Fetch API
// npm install astrapay
fetch("/pay", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ phone: "254712345678", amount: 10 })
})
.then(res => res.json())
.then(data => console.log(data));
// Vue.js setup
// npm install astrapay
export default {
methods: {
pay() {
fetch("/pay", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ phone: "254712345678", amount: 10 })
})
.then(res => res.json())
.then(data => console.log(data));
}
}
}
// pages/api/pay.js
// npm install astrapay
import AstraPay from "astrapay";
const client = new AstraPay({
consumerKey: process.env.KEY,
consumerSecret: process.env.SECRET,
shortcode: "174379",
passkey: process.env.PASSKEY,
callbackUrl: "https://yourdomain.com/callback"
});
export default async function handler(req, res) {
if (req.method === "POST") {
const { phone, amount } = req.body;
const result = await client.pay({ phone, amount });
res.status(200).json(result);
} else {
res.status(405).end(); // Method Not Allowed
}
}
// Install via Composer
// composer require astrasoftwares/astrapay
require 'vendor/autoload.php';
use Astrapay\AstraMpesa;
$client = new AstraMpesa([
'consumerKey' => 'YOUR_KEY',
'consumerSecret' => 'YOUR_SECRET',
'shortcode' => '174379',
'passkey' => 'YOUR_PASSKEY',
'callbackUrl' => 'https://yourdomain.com/callback'
]);
$response = $client->pay('254712345678', 10);
print_r($response);
// Install via Composer
// composer require astrasoftwares/astrapay
require 'vendor/autoload.php';
use Astrapay\AstraMpesa;
public function stkPush(Request $request)
{
$client = new AstraMpesa([
'consumerKey' => env('MPESA_CONSUMER_KEY'),
'consumerSecret' => env('MPESA_CONSUMER_SECRET'),
'shortcode' => env('MPESA_SHORTCODE'),
'passkey' => env('MPESA_PASSKEY'),
'callbackUrl' => env('MPESA_CALLBACK_URL')
]);
$response = $client->pay($request->phone, $request->amount);
return response()->json($response);
}
Collaborate with devs building payments for Africa. Connect with us on:
AstraPay SDK is a product by Astra Softwares, empowering developers with seamless mobile money integration across multiple languages and platforms.