Get Sandbox Credentials
Use the official portal
Section titled “Use the official portal”Safaricom’s primary entry points are the API catalog and the documentation portal. The portal is where you create apps, request product access, and gather the values this SDK expects.
Typical sandbox checklist
Section titled “Typical sandbox checklist”- Create or open your Daraja app in the Safaricom developer portal.
- Copy the sandbox
consumerKeyandconsumerSecret. - Add the sandbox shortcode and passkey for STK or C2B testing.
- Create an initiator user and encrypted
securityCredentialif you plan to test B2C, balance, status, or reversal. - Put those values in a local
.envfile that is excluded from git.
What Daraja means by shortcode
Section titled “What Daraja means by shortcode”- A shortcode is the business number used to receive or send funds through the enabled product on your profile.
- A paybill is typically used to collect money from customers repeatedly.
- Buy goods is commonly used for retail purchases.
- A till number is the buy-goods store identifier attached to that retail collection flow.
For this SDK, what matters is not only the number itself, but also which Daraja products Safaricom has enabled on that shortcode.
Before testing B2C-style APIs
Section titled “Before testing B2C-style APIs”- Make sure you have an initiator user in the portal.
- Generate
securityCredentialwith the sandbox public key certificate, not the production one. - Keep the unencrypted initiator password out of source control and shared logs.
Map values to the SDK
Section titled “Map values to the SDK”consumerKey->MPESA_CONSUMER_KEYconsumerSecret->MPESA_CONSUMER_SECRET- sandbox or production selector ->
MPESA_ENVIRONMENT - shortcode ->
MPESA_SHORT_CODE - STK passkey ->
MPESA_PASS_KEY - initiator username ->
MPESA_INITIATOR_NAME - encrypted credential ->
MPESA_SECURITY_CREDENTIAL
Practical tip
Section titled “Practical tip”Keep one .env file per account or environment. Accidentally mixing sandbox and production values is one of the fastest ways to burn time on avoidable auth failures.