Guide · Developers
SMS API security checklist for developers
A practical checklist for keeping SMS API keys and webhooks safe: least-privilege scopes, IP allowlists, secret storage, rate limits, idempotency and key rotation.
SMSGELY Team6 min read
An SMS API key is a key to your wallet and to your customers’ phones. If it leaks, someone else can send messages under your sender ID, at your cost, to anyone. The good news is that most leaks are prevented by a handful of habits.
This checklist covers how to create, store, use and rotate SMSGELY API keys safely, and how to verify webhooks. It is written for developers, but business owners can use it to review an integration built for them.
How SMSGELY API keys work
Keys look like smsgely_abcd1234ef_YOUR_SECRET: a fixed prefix, a short public identifier and a secret. A few properties matter for security:
- The full key is shown only once, when you create it in Dashboard → Developer → API keys. SMSGELY stores only a hash, so it cannot be shown again, even by our staff.
- Each key has scopes that limit what it can do:
sms:send,sms:read,contacts:read,contacts:writeandreports:read. - A key can have an IP allowlist of addresses or IPv4 CIDR ranges. Requests from anywhere else are refused with
403 FORBIDDEN. - Keys can be revoked at any time.
Requests send the key in a header, never in the URL:
curl https://<api-url-from-your-dashboard>/balance \
-H "Authorization: Bearer $SMSGELY_API_KEY"
1. Create keys with the least privilege
- One key per application or environment. Your website, your mobile backend and your staging server should each have their own key. If one leaks, you revoke only that one.
- Only the scopes it needs. A WooCommerce store or contact form needs only
sms:send. A reporting dashboard needs onlysms:readorreports:read. Avoid givingcontacts:writeunless the application really manages contacts. - Descriptive names. Name keys after where they are used, such as “Shop website – production”, so you know what breaks when you revoke one.
If a key without the right scope is used, the API returns 403 INSUFFICIENT_SCOPE. That is the system working as intended.
2. Restrict keys by IP address
- Add an IP allowlist when your server has a fixed outgoing IP address, which most VPS and dedicated servers do.
- Use CIDR ranges for a small group of servers, for example
203.0.113.0/28. - Skip the allowlist only when you must, such as on shared hosting with changing IPs, and compensate with stricter scopes and monitoring.
With an allowlist, a leaked key is useless outside your servers.
3. Keep keys out of code and out of the browser
This is where most leaks happen.
- Never call the SMS API from browser JavaScript or a mobile app. Anything shipped to a device can be extracted. Have the app call your own backend, and let the backend call SMSGELY.
- Store keys in environment variables or a secrets manager, not in source files.
- Add
.envto.gitignorebefore the first commit. Keys pushed to a public repository are found quickly by automated scanners. - WordPress: define the key in
wp-config.php, never in theme files, page builders or widgets. See the WordPress guide. - Do not log full keys. If you log requests, mask the
Authorizationheader. - Do not paste keys into tickets, chat or screenshots. Our support team never needs your full key.
For example, in a .env file that is never committed:
SMSGELY_API_KEY=smsgely_abcd1234ef_YOUR_SECRET
4. Protect the endpoints that trigger SMS
Your API key might be safe while your own application is abused. A public form that sends an OTP can be used by bots to send thousands of messages at your cost.
- Rate limit OTP and form endpoints per phone number and per IP address in your application.
- Use a CAPTCHA or similar check on public forms.
- Validate phone numbers before calling the API. Reject numbers that are not Bangladeshi mobile numbers if you only serve Bangladesh.
- Limit OTP resends, for example one per minute and a few per hour.
- Keep a sensible wallet balance. A prepaid wallet naturally caps how much an abused integration can spend.
SMSGELY also applies a per-key rate limit (by default 120 requests per minute). Exceeding it returns 429 RATE_LIMITED with a Retry-After header; respect it rather than retrying immediately.
5. Make retries safe with idempotency
Security includes not doing things twice by accident. Timeouts and retries can send duplicate messages if you are not careful.
- Send an
Idempotency-Keyon every POST, derived from your own data, such asotp-<user-id>-<attempt>orwc-1042-completed. - Retry with the same key and body. Within 24 hours SMSGELY replays the original result with
Idempotent-Replayed: trueand sends nothing new. - Handle
409 IDEMPOTENCY_CONFLICT. It means the same key was reused with a different body, usually a bug.
Details are in the API reference.
6. Verify every webhook
Webhooks tell your server about status changes. Because your webhook URL is public, you must check that each request really comes from SMSGELY.
- Use HTTPS only. SMSGELY only delivers webhooks to HTTPS URLs.
- Verify the
SMSGELY-Signatureheader. It has the formt=<unix>,v1=<hex>, wherev1is an HMAC-SHA256 of<t>.<raw body>using your webhook secret. - Compare in constant time to avoid timing attacks.
- Reject old timestamps, more than five minutes from your server time, to block replays.
- Use the raw body. Parsing and re-serialising JSON changes the bytes and breaks the signature.
- Deduplicate on the event
id. Delivery is at-least-once, so the same event can arrive more than once. - Respond 2xx within 8 seconds and do heavy work afterwards. Failed deliveries are retried up to 8 times with backoff.
import crypto from "node:crypto";
export function verifySmsgelySignature(rawBody, header, secret) {
const fields = Object.fromEntries(header.split(",").map((p) => p.split("=")));
const t = Number(fields.t);
if (!t || Math.abs(Date.now() / 1000 - t) > 300) return false;
const expected = crypto
.createHmac("sha256", secret)
.update(fields.t + "." + rawBody)
.digest();
const received = Buffer.from(fields.v1 || "", "hex");
return received.length === expected.length && crypto.timingSafeEqual(received, expected);
}
7. Rotate keys, and know what to do after a leak
- Rotate keys periodically and whenever someone with access leaves your team or agency.
- Rotate without downtime: create a new key, deploy it, confirm traffic uses it, then revoke the old one.
- If a key leaks: revoke it immediately in Dashboard → Developer → API keys, create a replacement, check delivery reports for messages you did not send, and contact support if you see misuse.
8. Secure the account itself
An API key is only as safe as the dashboard where it is created.
- Use a strong, unique password for your SMSGELY account. Passwords are stored with argon2id, but reused passwords leak elsewhere.
- Review active sessions and sign out of devices you no longer use.
- Keep the account email verified and monitored, so important account emails reach someone who acts on them.
9. Handle errors without leaking information
- Show users a generic message such as “We could not send the code, please try again”, and log the detailed error code privately.
- Treat
401 INVALID_API_KEYand403 FORBIDDENin production as alerts: they can mean a revoked key or a request from an unexpected IP. - See the full list in the error reference.
The short version
- One key per app, minimum scopes.
- IP allowlist wherever possible.
- Keys in environment variables, never in browsers, apps or repositories.
- Rate limit anything that triggers SMS.
- Idempotency keys on every POST.
- Verify webhook signatures and timestamps.
- Rotate regularly; revoke fast after a leak.
For an overview of authentication options, read authentication in the developer docs, or start with the plain-language SMS API guide.
- SMS API
- API security
- webhooks
- API keys
- developers
Questions about this article? Contact our team or browse the FAQ.