Collections API
Accept incoming payments in TZS. This API supports collections only — there is no payout endpoint. All requests use HTTPS and JSON.
Authentication
Generate keys on the API Keys page. Each mode (test/live) has a public key (pk_…) and a secret key (sk_…). The secret is shown once; we only store a hash. Keep it on your server — never in a browser or mobile app.
Every request sends four headers. The signature is a hex HMAC-SHA256 using your secret key over timestamp.METHOD.path.body (body is empty for GET).
Authorization: Bearer sk_test_xxx X-CP-Timestamp: <unix seconds> X-CP-Signature: hex(HMAC_SHA256(secret, ts + "." + METHOD + "." + path + "." + rawBody)) Idempotency-Key: <unique per new collection, 8-100 chars> (POST only)
// Node.js
import crypto from "node:crypto";
const ts = Math.floor(Date.now() / 1000).toString();
const path = "/api/public/v1/collections";
const body = JSON.stringify(payload);
const sig = crypto.createHmac("sha256", process.env.CP_SECRET)
.update(`${ts}.POST.${path}.${body}`).digest("hex");Endpoints
| POST | /api/public/v1/collections | Request a payment from a customer |
| GET | /api/public/v1/collections/{id} | Verify a collection's status |
Base URL: your CollectPay TZ site address, e.g. https://your-domain/api/public/v1.
Create a collection
| Field | Type | Rules |
|---|---|---|
| amount | integer | Required. Whole TZS, 500 – 50,000,000 |
| currency | string | Optional. Only "TZS" |
| reference | string | Required. Your unique order ID, 3-64 chars |
| description | string | Optional. Up to 255 chars |
| customer.name | string | Required. 2-100 chars |
| customer.phone | string | Required. 2556XXXXXXXX or 2557XXXXXXXX |
| customer.email | string | Optional |
curl -X POST https://your-domain/api/public/v1/collections \
-H "Authorization: Bearer $CP_SECRET" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-1042-attempt-1" \
-H "X-CP-Timestamp: $TS" -H "X-CP-Signature: $SIG" \
-d '{"amount":25000,"reference":"ORDER-1042","customer":{"name":"Asha Mussa","phone":"255712345678"}}'201 Created
{ "data": { "id": "cp_t_3f9a…", "reference": "ORDER-1042", "mode": "test",
"amount": 25000, "currency": "TZS", "status": "pending",
"customer": { "name": "Asha Mussa", "phone": "255712345678", "email": null },
"created_at": "2026-12-03T17:20:00Z", "completed_at": null } }Retrying with the same Idempotency-Key returns the original result (header Idempotent-Replayed: true) and never creates a second charge.
Verify transaction status
GET /api/public/v1/collections/cp_t_3f9a…
→ 200 { "data": { "id": "cp_t_3f9a…", "status": "successful", … } }Statuses: pending, successful, failed. Always verify status from your server before delivering goods.
Webhook notifications
Set an HTTPS webhook URL per mode on the API Keys page. When a collection settles we POST:
POST <your webhook URL>
X-CollectPay-Signature: t=1764781200,v1=5d1e…
{ "event": "collection.successful", "data": { …same shape as GET… } }Verify: compute HMAC-SHA256 with your webhook signing secret (whsec_…) over t + "." + rawBody, compare to v1 in constant time, and reject timestamps older than 5 minutes. Respond with 2xx quickly.
const [t, v1] = header.split(",").map(p => p.split("=")[1]);
const expected = crypto.createHmac("sha256", WHSEC).update(`${t}.${rawBody}`).digest("hex");
const ok = crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(v1))
&& Math.abs(Date.now()/1000 - Number(t)) < 300;Error codes
{ "error": { "code": "validation_error", "message": "customer.phone: …" } }| HTTP | Code | Meaning |
|---|---|---|
| 400 | idempotency_key_required | Missing or invalid Idempotency-Key header |
| 400 | invalid_json | Body is not valid JSON |
| 400 | https_required | Request was not sent over HTTPS |
| 401 | invalid_api_key | Secret key missing, malformed or revoked |
| 401 | signature_missing | X-CP-Timestamp or X-CP-Signature header missing |
| 401 | timestamp_expired | Timestamp more than 5 minutes from server time |
| 401 | signature_invalid | Signature does not match the request |
| 403 | account_suspended | Merchant account is suspended |
| 404 | not_found | Collection does not exist for this key's mode |
| 409 | duplicate_reference | Reference already used in this mode |
| 409 | idempotency_conflict | Idempotency-Key reused with different parameters |
| 422 | validation_error | One or more fields are invalid (message lists them) |
| 429 | rate_limited | More than 60 requests per minute for this key |
| 503 | live_provider_unavailable | Live collections are not enabled yet |
Test mode integration
Use your sk_test_ key. No real money moves and test payments never change your balance. The customer phone decides the outcome:
255700000001(or any other valid number) → successful255700000002→ failed255700000003→ stays pending
Live mode returns live_provider_unavailable until a payment provider is connected to the platform.
Limits & safety
- HTTPS only; plain HTTP requests are rejected.
- 60 requests per minute per key (429 when exceeded).
- Signed requests with a 5-minute timestamp window stop replays.
- Idempotency keys and unique references stop duplicate collections.
- Rotate a key any time — the old secret stops working immediately.