
Lesson Summary
In this lesson, we will walk through the official Mpesa API documentation. This will help you understand how to read and navigate the documentation, identify the available endpoints, understand request and response formats, and know where to find additional resources for troubleshooting and support. By the end of this lesson, you will be comfortable using the Mpesa API documentation for all your integration needs.
Lesson Details:
-
Introduction to Mpesa API Documentation
- The Mpesa API documentation is hosted on the Safaricom Developer Portal and provides detailed information on how to interact with the Mpesa Daraja API.
- The documentation includes all available API endpoints, request methods (POST, GET), and response formats, as well as guidelines on usage limits, error codes, and best practices.
- You can access the official documentation here for the latest updates and features.
-
Navigating the API Documentation
The Mpesa API documentation is divided into key sections that describe the available services and how to use them effectively:- Introduction Section: Gives an overview of the API, authentication methods, and general setup instructions.
- API Endpoints: Each API service (like STK Push, C2B, B2C, etc.) is listed here with detailed information on how to send requests and interpret responses.
- Authentication and Authorization: Explains how to authenticate API requests using OAuth 2.0 tokens (the process of obtaining and using access tokens).
- Request Parameters: Describes the required parameters that must be included in your API calls, such as the amount, phone number, and short code for various transactions.
- Response Codes: Lists the possible response codes and their meanings, helping you understand what each response indicates (success, failure, errors).
-
Key Mpesa APIs Covered in the Documentation
-
STK Push API:
Used to initiate a payment request to a customer’s phone via Mpesa’s STK (Sim Tool Kit) Push. This API is primarily used for Customer-to-Business (C2B) transactions.- Endpoint:
https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest
- Required Parameters:
PhoneNumber
,Amount
,Shortcode
,LipaNaMpesaOnline
- Endpoint:
-
C2B (Customer to Business) API:
Allows businesses to receive payments directly from customers through Mpesa.- Endpoint:
https://api.safaricom.co.ke/mpesa/c2b/v1/paymentrequest
- Required Parameters:
PhoneNumber
,Amount
,Shortcode
,CommandID
- Endpoint:
-
B2C (Business to Customer) API:
Used by businesses to send money to customers, such as disbursements and refunds.- Endpoint:
https://api.safaricom.co.ke/mpesa/b2c/v1/paymentrequest
- Required Parameters:
Amount
,PhoneNumber
,Shortcode
,CommandID
- Endpoint:
-
Account Balance API:
Allows businesses to query the account balance of a specific shortcode.- Endpoint:
https://api.safaricom.co.ke/mpesa/accountbalance/v1/query
- Required Parameters:
Shortcode
,Initiator
,SecurityCredential
- Endpoint:
-
-
Understanding Response Formats and Error Codes
-
Successful Responses:
When an API call is successful, Mpesa returns a 200 OK status along with a body containing transaction details (e.g.,ResultCode: 0
,ResultDesc: "Success"
). -
Error Responses:
When there is an error in the request, Mpesa returns a corresponding error code (e.g.,ResultCode: 1
,ResultDesc: "Insufficient funds"
). The documentation provides details on possible error codes and troubleshooting steps.
-
-
Best Practices for Using Mpesa APIs
- Rate Limiting: Mpesa imposes rate limits to prevent abuse. Ensure your app handles rate limits by checking the documentation for acceptable limits on API calls.
- Error Handling: Always account for potential errors in transactions by implementing robust error handling to address issues like invalid input, network failures, or insufficient funds.
- Security: Use secure communication channels (HTTPS) and follow best practices for storing and using OAuth tokens and sensitive credentials.
-
Getting Help and Additional Resources
- FAQs and Support: If you encounter issues, check the developer FAQs and the support section on the Safaricom Developer Portal.
- Developer Community: Join the Safaricom Developer Forum to ask questions, share experiences, and connect with other developers.
Summary
In this lesson, you learned how to navigate the Mpesa API documentation, including how to understand the different API endpoints, required parameters, and response formats. You explored key Mpesa APIs such as STK Push, C2B, B2C, and Account Balance, and gained insights on best practices for handling requests, responses, and error codes. This knowledge will be vital as you integrate Mpesa payments into your applications.