Developer guide

Sending OTP and verification codes by SMS in Kenya

One-time passwords protect sign-ins, payments and password resets. They must arrive fast, arrive once and reach people even if they opted out of your promotions. Here is how to send them with JMSURF SMS.

How the flow works

  1. Your server generates a random code (6 digits is common) and stores a hash of it with an expiry, for example 5 minutes.
  2. Your server calls POST /api/v1/sms/send with type transactional and a new reference.
  3. The user types the code; your server checks it against the hash, the expiry and an attempt limit.
  4. On success, delete the stored code so it cannot be used again.

The request

curl
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].

Make every message count.

Start with a free workspace. Add your audience, top up with M-Pesa and keep your customers in the conversation.

Create your workspace