Skip to main content

mpesa@1.0.2

buyGoods(data, options)
checkTransactionStatus(data, options)
registerUrl(data, options)
remitTax(data, options)
request(method, path, body, options)
stkPush(data, options)

Functions

buyGoods

buyGoods(data, options) ⇒ Operation

Pay for goods and services directly from your business account to a till number, merchant store number or Merchant HO

ParamTypeDescription
dataBuyGoodsObjectThe object that will be attached to the POST request body
optionsRequestOptionsOptional request object. Includes headers, error mappings and query params

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the DARAJA API server (excluding the body)
referencesan array of all previous data objects used in the Job

Example: Pay for goods

buyGoods({
"Initiator": "testapi",
"SecurityCredential": "sbMXpuhMX5LzieNiDrx9TgscfaBxxvie0WlBDdGli4MWu4s5gbhYVlBy+T89xHQdoYwcG202KNp403ln2dLFnytPqw==",
"Amount": 1,
"PartyA": 600999,
"PartyB": 600000,
"AccountReference": 353353,
"Requester": 254708374149,
"Remarks": "ok",
"QueueTimeOutURL": "https://mydomain.com/b2b/queue/",
"ResultURL": "https://mydomain.com/b2b/result/"
});

checkTransactionStatus

checkTransactionStatus(data, options) ⇒ Operation

Check the status of the transaction

ParamTypeDescription
dataTransactionStatusObjectThe object that will be attached to the POST request body
optionsRequestOptionsOptional request object. Includes headers, error mappings and query params

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the DARAJA API server (excluding the body)
referencesan array of all previous data objects used in the Job

Example: Check status of a transaction

checkTransactionStatus({
"Initiator": "testapi",
"SecurityCredential": "ctHDk+dN14A22B5GyQQvISSTY3K1tVnCTuQGoG7PsTCadzTkl5wz44Rhpkb0BZDvfRA==",
"TransactionID": "OEI2AK4Q16",
"PartyA": 600998,
"IdentifierType": 4,
"ResultURL": "https://mydomain.com/TransactionStatus/result/",
"QueueTimeOutURL": "https://mydomain.com/TransactionStatus/queue/",
"Remarks": "fdfdfdfdf",
"Occassion": "null",
});

registerUrl

registerUrl(data, options) ⇒ Operation

Register a URL that allows receiving payment notifications to your paybill.

ParamTypeDescription
dataRegisterUrlObjectThe object that will be attached to the POST request body
optionsRequestOptionsOptional request object. Includes headers, error mappings and query params

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the DARAJA API server (excluding the body)
referencesan array of all previous data objects used in the Job

Example: Register a URL to receive payment notifications

registerUrl({
"ShortCode": 600426,
"ResponseType": "Completed",
"ConfirmationURL": "https://mydomain.com/confirmation",
"ValidationURL": "https://mydomain.com/validation"
});

remitTax

remitTax(data, options) ⇒ Operation

Remit tax to the Kenya Revenue Authority (KRA).

ParamTypeDescription
dataRemitTaxObjectThe object that will be attached to the POST request body
optionsRequestOptionsOptional request object. Includes headers, error mappings and query params

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the DARAJA API server (excluding the body)
referencesan array of all previous data objects used in the Job

Example: Pay tax to KRA

remitTax({
"Initiator": "testapi",
"SecurityCredential": "Uq1qluCjSYfMF3XEvlpfuatnWwWerwq42fB+mMd8nsKdAVO04DGHhG/s3xO3g7POki9B8i7cSoEkBux4bQrlDLDWJhaTrt1TdbE+ZQ==",
"Amount": 1,
"PartyA": 600995,
"AccountReference": 353353,
"Remarks": "ok",
"QueueTimeOutURL": "https://mydomain.com/b2b/queue/",
"ResultURL": "https://mydomain.com/b2b/result/"
})

request

request(method, path, body, options) ⇒ Operation

Make a general HTTP request

ParamTypeDescription
methodstringHTTP method to use
pathstringPath to resource.
bodyobjectObject which will be attached to the POST body
optionsRequestOptionsOptional request options

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the DARAJA API server (excluding the body)
referencesan array of all previous data objects used in the Job

Example: Register a URL to receive payment notifications

request("POST", "/mpesa/c2b/v1/registerurl", 
{
"ShortCode": 600426,
"ResponseType": "Completed",
"ConfirmationURL": "https://mydomain.com/confirmation",
"ValidationURL": "https://mydomain.com/validation"
});

stkPush

stkPush(data, options) ⇒ Operation

Initiate a STK pin prompt to a Safaricom mobile number.

ParamTypeDescription
dataSTKPushObjectThe object that will be attached to the POST request body
optionsRequestOptionsOptional request object. Includes headers, error mappings and query params

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the DARAJA API server (excluding the body)
referencesan array of all previous data objects used in the Job

Example: Initiate STK Push

stkPush({
"Amount": 1,
"PartyA": 254708374149,
"PartyB": 174379,
"PhoneNumber": 254708374149,
"CallBackURL": "https://mydomain.com/path",
"AccountReference": "CompanyXLTD",
"TransactionDesc": "Payment of X"
});

Interfaces

BuyGoodsObject

Buy goods parameter definition

Properties

NameTypeDescription
InitiatorstringThe M-Pesa API operator username. This user needs Org Business Pay Bill API initiator role on M-Pesa
SecurityCredentialstringThe encrypted password of the M-Pesa API operator.
AmountnumberThe transaction amount.
PartyAnumberYour shortcode. The shortcode from which money will be deducted.
AccountReferencestringThe account number to be associated with the payment. Up to 13 characters
RemarksstringAny additional information to be associated with the transaction.
QueueTimeOutURLURLA URL that will be used to notify your system in case the request times out before processing.
ResultURLURLA URL that will be used to send transaction results after processing.

HttpState

State object

Properties

NameDescription
datathe parsed response body
responsethe response from the DARAJA API server (excluding the body)
referencesan array of all previous data objects used in the Job

RegisterUrlObject

Register URL parameter definition

Properties

NameTypeDescription
ShortCodenumericUsually, a unique number is tagged to an M-PESA pay bill/till number of the organization.
ResponseTypestringThis parameter specifies what is to happen if for any reason the validation URL is not reachable. Sample values: 'Canceled', 'Completed'
ConfirmationURLURLThis is the URL that receives the confirmation request from API upon payment completion.
ValidationURLURLThis is the URL that receives the validation request from the API upon payment submission.

RemitTaxObject

Remit tax parameter definition

Properties

NameTypeDescription
InitiatorstringThe M-Pesa API operator username.
SecurityCredentialstringThe encrypted password of the M-Pesa API operator.
AmountnumberThe transaction amount.
PartyAnumberThis is your own shortcode from which the money will be deducted.
AccountReferencestringThe payment registration number (PRN) issued by KRA.
RemarksstringAny additional information to be associated with the transaction.
QueueTimeOutURLURLA URL that will be used to notify your system in case the request times out before processing.
ResultURLURLA URL that will be used to send transaction results after processing.

RequestOptions

Options provided to the HTTP request

Properties

NameTypeDescription
errorsobjectMap of errorCodes -> error messages, ie, { 404: 'Resource not found;' }. Pass false to suppress errors for this code.
queryobjectAn object of query parameters to be encoded into the URL.
headersobjectAn object of headers to append to the request.

STKPushObject

STKPush request data object

Properties

NameTypeDescription
AmountnumberAmount charged.
PartyAnumberThe phone number that receives the STK push prompt. Expected to be a valid Safaricom Number that is M-pesa registered in the format 2547XXXXXXXX
PartyBnumberThe organization that receives the funds
PhoneNumbernumberThe mobile number to receive the STK pin prompt
CallBackURLURLA valid secure URL that is used to receive notifications from M-Pesa API. It is the endpoint to which the results will be sent by M-Pesa API
AccountReferencestringAlong with the business name, this value is also displayed to the customer in the STK pin prompt message. Maximum of 12 characters
TransactionDescstringAny additional information/comment that can be sent along with the request from you system.

TransactionStatusObject

Check transaction status data object.

Properties

NameTypeDescription
InitiatorstringThe name of the initiator initiating the request.
SecurityCredentialstringEncrypted credential of the user getting transaction status.
TransactionIDstringUnique identifier to identify a transaction on Mpesa.
PartyAnumberOrganization/MSISDN receiving the transaction.
IdentifierTypenumberType of organization receiving the transaction. Example: "4" Orgnaization shortcode.
ResultURLURLThe path that stores information of a transaction.
QueueTimeOutURLURLThe path that stores information of timeout transaction.
RemarksstringComments that are sent along with the transaction.
OccassionstringOptional parameter.