Developers
Code snippets
Copy-ready examples that match the API documentation. The full reference for every endpoint is there.
Start a payment
Ask the customer's phone to confirm an M-Pesa payment. Swap in your key, your email and the id of one of your connected accounts.
curl -X POST https://pay.mutexai.net/api/v1/initiatestkpush \
-H "Content-Type: application/json" \
-d '{
"api_key": "mp_live_YOUR_KEY",
"email": "[email protected]",
"amount": 1500,
"msisdn": "0712345678",
"reference": "ORDER-12345",
"account_id": "GP12345678"
}'
<?php
$payload = [
'api_key' => getenv('MUTEXPAY_KEY'),
'email' => '[email protected]',
'amount' => 1500,
'msisdn' => '0712345678',
'reference' => 'ORDER-12345',
'account_id' => 'GP12345678',
];
$ch = curl_init('BASE_URL/api/v1/initiatestkpush');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Accept: application/json'],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_TIMEOUT => 30,
]);
$raw = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
$answer = json_decode($raw, true);
if ($code !== 200 || ($answer['success'] ?? '0') !== '200') {
throw new RuntimeException($answer['message'] ?? 'MutexPay did not accept the request');
}
// Only says the phone was asked to confirm. Wait for the webhook, or poll the status.
$requestId = $answer['transaction_request_id'];
const response = await fetch('https://pay.mutexai.net/api/v1/initiatestkpush', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify({
api_key: process.env.MUTEXPAY_KEY,
email: '[email protected]',
amount: 1500,
msisdn: '0712345678',
reference: 'ORDER-12345',
account_id: 'GP12345678',
}),
signal: AbortSignal.timeout(30_000),
});
const answer = await response.json();
if (!response.ok || answer.success !== '200') {
throw new Error(answer.message ?? 'MutexPay did not accept the request');
}
// Only says the phone was asked to confirm. Wait for the webhook, or poll the status.
const requestId = answer.transaction_request_id;
Read the status of a payment
Ask what M-Pesa has reported so far. Treat only TransactionStatus "Completed" as money received.
curl -X POST https://pay.mutexai.net/api/v1/transactionstatus \
-H "Content-Type: application/json" \
-d '{
"api_key": "mp_live_YOUR_KEY",
"email": "[email protected]",
"transaction_request_id": "GPPID3011202609451234567"
}'
<?php
$payload = [
'api_key' => getenv('MUTEXPAY_KEY'),
'email' => '[email protected]',
'transaction_request_id' => 'GPPID3011202609451234567',
];
$ch = curl_init('BASE_URL/api/v1/transactionstatus');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Accept: application/json'],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_TIMEOUT => 30,
]);
$answer = json_decode(curl_exec($ch), true);
curl_close($ch);
$paid = ($answer['TransactionStatus'] ?? '') === 'Completed';
const response = await fetch('https://pay.mutexai.net/api/v1/transactionstatus', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify({
api_key: process.env.MUTEXPAY_KEY,
email: '[email protected]',
transaction_request_id: 'GPPID3011202609451234567',
}),
signal: AbortSignal.timeout(30_000),
});
const answer = await response.json();
const paid = answer.TransactionStatus === 'Completed';
Check a webhook signature
A webhook that says "paid" is only worth acting on once you have checked who sent it. Use the raw request body exactly as it arrived.
<?php
// Verify a MutexPay webhook before trusting it.
$raw = file_get_contents('php://input');
$secret = 'YOUR_SIGNING_SECRET'; // Webhook Settings in your MutexPay dashboard
$header = $_SERVER['HTTP_X_MUTEXPAY_SIGNATURE'] ?? '';
if (!preg_match('/t=(\d+),v1=([0-9a-f]{64})/', $header, $m)) {
http_response_code(401); exit;
}
[$_, $timestamp, $signature] = $m;
if (abs(time() - (int) $timestamp) > 300) {
http_response_code(401); exit; // too old: possible replay
}
$expected = hash_hmac('sha256', $timestamp . '.' . $raw, $secret);
if (!hash_equals($expected, $signature)) {
http_response_code(401); exit;
}
$event = json_decode($raw, true);
// Only "payment.completed" with ResponseCode 0 means the customer paid.
if (($event['Event'] ?? '') === 'payment.completed' && (int) ($event['ResponseCode'] ?? 1) === 0) {
// fulfil the order
}
http_response_code(200);
import crypto from 'node:crypto';
// Returns the parsed event when the signature checks out, otherwise null.
export function verifyWebhook(rawBody, signatureHeader, secret) {
const match = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(signatureHeader ?? '');
if (!match) return null;
const [, timestamp, signature] = match;
if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return null; // too old: possible replay
const expected = crypto.createHmac('sha256', secret).update(`${timestamp}.${rawBody}`).digest('hex');
const given = Buffer.from(signature, 'hex');
const wanted = Buffer.from(expected, 'hex');
if (given.length !== wanted.length || !crypto.timingSafeEqual(given, wanted)) return null;
return JSON.parse(rawBody);
}
// In your route handler, read the raw body before any parser touches it:
// const raw = await request.text();
// const event = verifyWebhook(raw, request.headers['x-mutexpay-signature'], process.env.MUTEXPAY_WEBHOOK_SECRET);
// if (!event) return new Response(null, { status: 401 });
// if (event.Event === 'payment.completed' && event.ResponseCode === 0) {
// // fulfil the order once, keyed by event.TransactionID
// }
// return new Response(null, { status: 200 });