Secure integration guide

The whole path from sign-up to live payments, in order, with what to do and what never to do at each step.

This page is the path every Swichi Payments integration should follow, from the first key to live money. Each step says what to do and what never to do. The other pages go deeper; this one keeps the order and the safety rules in one place.

The flow at a glance

  1. Sign up and read the docs.
  2. Get a sandbox key, scoped to what you need.
  3. Build: collect money with an idempotency key, the amount decided by your server.
  4. Add a webhook endpoint and keep its signing secret safe.
  5. Verify every webhook before acting on it.
  6. Test every outcome in the sandbox.
  7. Go live: finish verification, then swap in live credentials.
  8. Run it: watch, rotate and revoke.

1. Sign up and read the docs

Make a Swichi account. Your sandbox is there from the start, with test money. Read, in this order: Sandbox, Collections, Webhooks, Go live.

The API is plain HTTPS and JSON at https://gateway.swichi.cc/v1. There is nothing to install.

2. Get a sandbox key

On Swichi Gateway → Developers → API keys, with Test mode on, press Get my sandbox key.

Do

  • Give the key only the scopes it needs (collections, payouts, transfers, read). A shop that only takes payments needs collections and read, not payouts.
  • Add an IP allow list with your servers' addresses.
  • Keep the key in an environment variable or your secret manager, and send it as Authorization: Bearer <key>.

Never

  • Put a key in a mobile app, a browser, JavaScript sent to customers, or a public repository. Calls to Swichi come from your server only.
  • Share one key between unrelated systems. Make one per system, so you can revoke one without stopping the others.

See API keys.

3. Collect money safely

curl https://gateway.swichi.cc/v1/collections \
  -H "Authorization: Bearer $SWICHI_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-1042" \
  -d '{"amount": 25000, "currency": "TZS", "phone": "0754 000 001", "reference": "order-1042"}'

Do

  • Send an Idempotency-Key on every POST, made from your own record (order-1042). If the network fails, retry with the same key: Swichi returns the first result instead of charging twice. The same key with a different body is refused. See Idempotency.
  • Take the amount from your own database (the order's total), on your server.
  • Put your order id in reference, so every event leads back to your order.
  • Treat a new collection as waiting. It is paid only when Swichi says so (step 5), never when your request returns.

Never

  • Take the amount, currency or account from the browser or the customer's request. Anyone can change those.
  • Mark an order paid because the customer came back to your "thank you" page.
  • Retry a POST with a new key after a timeout. Read the object instead, or retry with the same key.

4. Add a webhook endpoint

On Swichi Gateway → Developers → Webhooks, add your HTTPS URL, for example https://shop.example/swichi/webhook. Sandbox and live have separate endpoints.

Swichi shows the endpoint's signing secret once, starting with whsec_.

Do

  • Store the secret like a password: an environment variable such as SWICHI_WEBHOOK_SECRET, or your secret manager.
  • Use HTTPS only.
  • Press Send test event and check that a signed webhook.test arrives and passes your check.

Never

  • Commit the secret, log it, or send it to the browser.
  • Reuse the sandbox secret in live: live has its own endpoint and its own secret.

5. Verify every webhook before acting on it

Events follow the Standard Webhooks specification. Each request carries webhook-id, webhook-timestamp and webhook-signature headers. Your handler must, in this order:

  1. Read the raw body as bytes or a string, before any JSON parsing. A parsed and re-encoded body does not match the signature.
  2. Check the timestamp: refuse it if it is more than 5 minutes from your clock.
  3. Check the signature: v1, + base64(HMAC-SHA256(key, id.timestamp.body)), where the key is the part of your secret after whsec_, base64-decoded. Compare with a constant-time function (crypto.timingSafeEqual, hash_equals). If it doesn't match, answer 401 and do nothing.
  4. Skip duplicates: keep the webhook-ids you have handled. The same event can arrive more than once; handle it once.
  5. Match it to your order by data.reference, and check that data.amount and data.currency are what the order expects.
  6. Act, for example mark the order paid on collection.succeeded, then answer 2xx within 10 seconds. Do slow work (emails, stock) after answering, from a queue.

Code for Node.js, PHP, Laravel and Next.js is on Webhooks.

Do

  • Treat a webhook as a reminder and the API as the truth: when in doubt, read GET /v1/collections/{id} with your key.
  • Run a small job that reads any order still waiting after a few minutes, in case a webhook was missed.

Never

  • Act on a webhook whose signature you did not check, or one that is too old.
  • Trust an event because it came from a known IP address or has the right shape.
  • Let one failing order stop your endpoint from answering the others.

If your endpoint is down, Swichi retries for 3 days (after 5 s, 5 min, 30 min, 2 h, 5 h, 10 h, then every 12 h), then turns the endpoint off and tells you. You can resend any event from the endpoint's deliveries.

6. Test every outcome in the sandbox

Nothing real moves in the sandbox. Use the magic numbers or force an outcome:

  • A phone ending in 001 pays at once; one ending in 002 declines.
  • POST /v1/sandbox/collections/{id}/simulate with {"outcome": "succeeded"} (or failed, expired) settles a collection on demand.

Before going live, check that your integration:

  • marks an order paid only after a verified collection.succeeded;
  • leaves it unpaid on collection.failed and collection.expired, and lets the customer try again;
  • charges once when you send the same request twice (same Idempotency-Key);
  • refuses a webhook with a wrong signature, and one older than 5 minutes;
  • handles the same webhook twice without doing the work twice;
  • finds a paid order even when its webhook never arrives (reading the API);
  • keeps working when Swichi answers slowly or with an error.

See Sandbox.

7. Go live

  1. Finish every line of Developers → Verification: your identity, your business, Swichi's checks, a settlement account and the terms. See Go live.
  2. Turn Test mode off. Make a live key with the same narrow scopes and IP allow list.
  3. Add a live webhook endpoint. It gets its own whsec_ secret.
  4. Put the live key and the live secret in your production environment. The code does not change.
  5. Make one small real payment end to end, and check the order, the webhook and your records agree.

Never use a live key in development or tests, or a sandbox key in production.

8. Run it safely

  • Watch the endpoint's deliveries for failures, and alert on orders stuck waiting.
  • Rotate keys and the webhook secret on a schedule, and at once when someone with access leaves. Make the new key first, deploy it, then revoke the old one: nothing stops.
  • If a key leaks, revoke it on API keys (it stops working at once), check its last use, and make a new one.
  • If the webhook secret leaks, rotate it on the endpoint and deploy the new one.
  • Read errors by code: every error has a code, a doc_url and a request_id. Send the request_id when you ask Swichi for help. See Errors.
  • Limits grow with trust. A new business starts low; over a limit you get limit_exceeded.

Before you launch

  • Keys and secrets live only on your servers, in environment variables or a secret manager.
  • Each key has only the scopes it needs and an IP allow list.
  • Every POST sends an Idempotency-Key from your own record.
  • Amounts come from your database, never from the browser.
  • Webhooks are verified on the raw body, with a 5-minute window and a constant-time compare.
  • Duplicate webhooks are skipped by webhook-id.
  • An order is paid only after a verified collection.succeeded (or reading the API).
  • A job catches orders whose webhook never came.
  • Sandbox and live have separate keys, endpoints and secrets.
  • You know how to revoke a key and rotate the secret today.

On this page