How the flow works
- Your server generates a random code (6 digits is common) and stores a hash of it with an expiry, for example 5 minutes.
- Your server calls POST /api/v1/sms/send with type transactional and a new reference.
- The user types the code; your server checks it against the hash, the expiry and an attempt limit.
- On success, delete the stored code so it cannot be used again.
The request
curl -X POST https://jmsurfsms.co.ke/api/v1/sms/send \
-H "Authorization: Bearer $JMSURF_API_KEY" \
-H "Content-Type: application/json" \
-d '{"to":"07XXXXXXXX","message":"Your verification code is 482915. It expires in 5 minutes. Do not share it.","reference":"3c1b0e4a-7f9d-4a8e-9b21-6d5f0c2e8a17","type":"transactional"}'type transactional means the code still reaches a number that replied STOP to your marketing. Use it only for messages the person asked for.
Make it reliable
- Generate the reference once per code and reuse it on retries, so a timeout never sends two different messages.
- Keep the message under 160 plain characters (no emoji) so it is a single SMS costing KES 0.40.
- Use your approved sender ID so users recognise and trust the message.
- Listen for sms.failed webhooks and offer the user another way, such as email, when a code cannot be delivered.
- Do not resend automatically on sms.unknown; the outcome is being reconciled and the user may already have the code.
Protect against abuse
- Limit requests per phone number and per IP on your side, for example 3 codes per 10 minutes, so attackers cannot run up your SMS bill.
- Each API key has its own per-minute rate limit (600 per minute by default) and can be locked to your server's IP addresses.
- Give the OTP key only the sms:send scope.
- Never put the API key in a mobile app or browser code; send codes from your server.
Testing
Use a jms_test_ key while building. It returns the same responses as a live key, records the message with the status sandbox and never charges or delivers, so automated tests cost nothing.
Start sending today
Create a free workspace, top up with M-Pesa and send your first message at KES 0.40 per SMS. Questions first? Call or WhatsApp +254 712 504 593 or email [email protected].
Related pages
- SMS API in Node.jsSend SMS from Node.js with fetch and the JMSURF SMS API: single and bulk sends, idempotent retries and an Express webhook with HMAC signature check.
- SMS opt-outsHow to handle SMS opt-outs in Kenya: consent, STOP replies, opt-out lines on promotions, transactional messages and the Data Protection Act, 2019.
- SACCOsSMS for SACCOs and microfinance in Kenya: loan repayment reminders, contribution receipts, AGM notices and OTPs through the API. Flat KES 0.40 per SMS.
- API referenceEvery endpoint, request and response of the REST API.