
Topic Summary
This topic focuses on the key Mpesa API endpoints that are used for processing payments and transactions: STK Push, C2B, B2C, and Balance Inquiry. These endpoints are critical for integrating Mpesa payment solutions into your applications, allowing you to manage both incoming and outgoing transactions as well as query account balances.
Lesson: Key Mpesa API Endpoints
- STK Push API (Sim Tool Kit Push)
-
Purpose: The STK Push API is used to initiate payment requests directly to a customer’s phone. It is primarily used for Customer-to-Business (C2B) transactions, allowing customers to pay businesses via Mpesa.
-
Endpoint:
POST https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest
-
Required Parameters:
- Shortcode: The business shortcode assigned by Safaricom.
- LipaNaMpesaOnlineShortcode: The shortcode of the business account for payments.
- PhoneNumber: The phone number of the customer making the payment.
- Amount: The amount being paid.
- AccountReference: A reference for the transaction (e.g., an order number).
- TransactionDesc: Description of the transaction (e.g., payment for goods/services).
- Callback URL: A URL where Safaricom sends the response after processing the request.
-
Request Example:
{ "Shortcode": "174379", "LipaNaMpesaOnlineShortcode": "174379", "PhoneNumber": "254700000000", "Amount": 100, "AccountReference": "ORD12345", "TransactionDesc": "Payment for goods", "CallbackURL": "https://yourcallbackurl.com" }
-
Response:
The API will send a response indicating whether the payment request was successful or failed. For successful requests, the response will include the CheckoutRequestID and ResponseCode.
-
- C2B (Customer-to-Business) API
-
Purpose: The C2B API allows customers to send payments to a business. This is useful for services like payments for goods, subscriptions, and utility bill payments.
-
Endpoint:
POST https://api.safaricom.co.ke/mpesa/c2b/v1/paymentrequest
-
Required Parameters:
- Shortcode: The business shortcode.
- PhoneNumber: The customer’s phone number.
- Amount: The amount being paid.
- CommandID: The command for the payment (e.g.,
CustomerBuyGoodsOnline
). - BillRefNumber: A reference number (e.g., invoice number).
- Shortcode: The business shortcode.
-
Request Example:
{ "Shortcode": "174379", "PhoneNumber": "254700000000", "Amount": 100, "CommandID": "CustomerBuyGoodsOnline", "BillRefNumber": "ORD12345" }
-
Response:
A response will be sent with a ResultCode and ResultDesc. If the request is successful, the ResultCode will be0
.
-
- B2C (Business-to-Customer) API
-
Purpose: The B2C API allows businesses to send money to customers. This is often used for payroll disbursements, refunds, or other business-to-customer transactions.
-
Endpoint:
POST https://api.safaricom.co.ke/mpesa/b2c/v1/paymentrequest
-
Required Parameters:
- Amount: The amount to be sent to the customer.
- PhoneNumber: The customer’s phone number.
- Shortcode: The business shortcode.
- CommandID: The type of transaction (e.g.,
BusinessPayment
). - Initiator: The username of the payment initiator.
- SecurityCredential: A secure credential for authentication.
-
Request Example:
{ "Amount": 1000, "PhoneNumber": "254700000000", "Shortcode": "174379", "CommandID": "BusinessPayment", "Initiator": "businessUser", "SecurityCredential": "yourSecureCredential" }
-
Response:
A successful response will return details of the transaction, including ResultCode and ResultDesc. If the transaction is successful, ResultCode will be0
.
-
- Balance Inquiry API
-
Purpose: The Balance Inquiry API allows businesses to check the account balance of a specific shortcode.
-
Endpoint:
POST https://api.safaricom.co.ke/mpesa/accountbalance/v1/query
-
Required Parameters:
- Shortcode: The business shortcode.
- Initiator: The username of the person querying the balance.
- SecurityCredential: A secure credential for authentication.
-
Request Example:
{ "Shortcode": "174379", "Initiator": "businessUser", "SecurityCredential": "yourSecureCredential" }
-
Response:
A successful response will return the Balance along with the ResultCode and ResultDesc.
-
Summary
In this lesson, you explored the key Mpesa API endpoints: STK Push, C2B, B2C, and Balance Inquiry. These endpoints enable you to perform various payment transactions such as receiving payments from customers, sending money to clients, and checking your account balance. Understanding these endpoints and their parameters is essential for successfully integrating Mpesa services into your application.