curl --request POST \
--url https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa \
--header 'Content-Type: application/json' \
--data '
{
"amount": 25000,
"msisdn": 712345678,
"idempotency_key": "3f1c-qr-mpesa-0001"
}
'import requests
url = "https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa"
payload = {
"amount": 25000,
"msisdn": 712345678,
"idempotency_key": "3f1c-qr-mpesa-0001"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({amount: 25000, msisdn: 712345678, idempotency_key: '3f1c-qr-mpesa-0001'})
};
fetch('https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'amount' => 25000,
'msisdn' => 712345678,
'idempotency_key' => '3f1c-qr-mpesa-0001'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa"
payload := strings.NewReader("{\n \"amount\": 25000,\n \"msisdn\": 712345678,\n \"idempotency_key\": \"3f1c-qr-mpesa-0001\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa")
.header("Content-Type", "application/json")
.body("{\n \"amount\": 25000,\n \"msisdn\": 712345678,\n \"idempotency_key\": \"3f1c-qr-mpesa-0001\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"amount\": 25000,\n \"msisdn\": 712345678,\n \"idempotency_key\": \"3f1c-qr-mpesa-0001\"\n}"
response = http.request(request)
puts response.read_body{
"status": {
"ok": true,
"code": 123,
"desc": "<string>"
},
"data": {
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"method": "mpesa",
"status": "initiated",
"amount": 100000,
"currency": "KES",
"merchant": "<string>",
"fee": 123,
"receipt": "<string>",
"failure_reason": "<string>",
"created_at": "2023-11-07T05:31:56Z"
}
}{
"status": {
"ok": false,
"code": 422,
"desc": "Unprocessable Entity"
},
"errors": {
"code": "insufficient_funds",
"message": "insufficient funds"
}
}{
"status": {
"ok": false,
"code": 422,
"desc": "Unprocessable Entity"
},
"errors": {
"code": "insufficient_funds",
"message": "insufficient funds"
}
}{
"status": {
"ok": false,
"code": 422,
"desc": "Unprocessable Entity"
},
"errors": {
"code": "insufficient_funds",
"message": "insufficient funds"
}
}{
"status": {
"ok": false,
"code": 422,
"desc": "Unprocessable Entity"
},
"errors": {
"code": "insufficient_funds",
"message": "insufficient funds"
}
}{
"status": {
"ok": false,
"code": 422,
"desc": "Unprocessable Entity"
},
"errors": {
"code": "insufficient_funds",
"message": "insufficient funds"
}
}Pay a merchant by M-Pesa (STK push)
Sends an STK prompt to the payer’s phone. The payer needs no CloudPay account. Same lifecycle
as top-ups (initiated -> pending -> completed | failed | review); the merchant till
is credited only after rails confirms. Poll GET /v1/pay/{code}/payments/{id}.
Anti-spam: a phone may have at most 3 unanswered prompts in 10 minutes (429 too_many_prompts).
Amount bounds are M-Pesa’s: KES 1 to 250,000.
Test environment: each prompt is capped at KES 100 (400 test_amount_limit); the prompt is
refused before anything is recorded. A live merchant not yet approved for production is
refused with 403 production_not_approved.
Development also has a daily ceiling per tenant (default KES 1,000; staff set test_daily_limit, 0 disables
Development collections): today’s non-failed collections (Africa/Nairobi day) plus this one over it is
refused with 400 test_daily_limit before anything is recorded.
curl --request POST \
--url https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa \
--header 'Content-Type: application/json' \
--data '
{
"amount": 25000,
"msisdn": 712345678,
"idempotency_key": "3f1c-qr-mpesa-0001"
}
'import requests
url = "https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa"
payload = {
"amount": 25000,
"msisdn": 712345678,
"idempotency_key": "3f1c-qr-mpesa-0001"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({amount: 25000, msisdn: 712345678, idempotency_key: '3f1c-qr-mpesa-0001'})
};
fetch('https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'amount' => 25000,
'msisdn' => 712345678,
'idempotency_key' => '3f1c-qr-mpesa-0001'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa"
payload := strings.NewReader("{\n \"amount\": 25000,\n \"msisdn\": 712345678,\n \"idempotency_key\": \"3f1c-qr-mpesa-0001\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa")
.header("Content-Type", "application/json")
.body("{\n \"amount\": 25000,\n \"msisdn\": 712345678,\n \"idempotency_key\": \"3f1c-qr-mpesa-0001\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.cloudpay.africa/cloudpay/v1/pay/{code}/mpesa")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"amount\": 25000,\n \"msisdn\": 712345678,\n \"idempotency_key\": \"3f1c-qr-mpesa-0001\"\n}"
response = http.request(request)
puts response.read_body{
"status": {
"ok": true,
"code": 123,
"desc": "<string>"
},
"data": {
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"method": "mpesa",
"status": "initiated",
"amount": 100000,
"currency": "KES",
"merchant": "<string>",
"fee": 123,
"receipt": "<string>",
"failure_reason": "<string>",
"created_at": "2023-11-07T05:31:56Z"
}
}{
"status": {
"ok": false,
"code": 422,
"desc": "Unprocessable Entity"
},
"errors": {
"code": "insufficient_funds",
"message": "insufficient funds"
}
}{
"status": {
"ok": false,
"code": 422,
"desc": "Unprocessable Entity"
},
"errors": {
"code": "insufficient_funds",
"message": "insufficient funds"
}
}{
"status": {
"ok": false,
"code": 422,
"desc": "Unprocessable Entity"
},
"errors": {
"code": "insufficient_funds",
"message": "insufficient funds"
}
}{
"status": {
"ok": false,
"code": 422,
"desc": "Unprocessable Entity"
},
"errors": {
"code": "insufficient_funds",
"message": "insufficient funds"
}
}{
"status": {
"ok": false,
"code": 422,
"desc": "Unprocessable Entity"
},
"errors": {
"code": "insufficient_funds",
"message": "insufficient funds"
}
}