Errors
Two different kinds of failure
Request errors — your API call itself was wrong (bad phone number, expired key, rate limit) — throw a typed error synchronously:
TypeScript
import { ValidationError, RateLimitError, VectaPayError } from "@vectapay/sdk";
try {
await vectapay.payments.collect({ /* ... */ });
} catch (error) {
if (error instanceof ValidationError) {
console.log(error.details); // field-level messages, when the backend returns them
} else if (error instanceof RateLimitError) {
console.log(`retry after ${error.retryAfterSeconds}s`);
} else if (error instanceof VectaPayError) {
console.log(error.statusCode, error.message);
}
}| Class | Status | Meaning |
|---|---|---|
ValidationError | 400, 422 | The request itself is malformed. |
AuthenticationError | 401 | Missing, invalid, or wrong-environment API key. |
PermissionError | 403 | Valid key, but missing role/plan permission. |
NotFoundError | 404 | No such payment/payout/resource. |
ConflictError | 409 | An idempotencyKey was reused for a request with different parameters. |
RateLimitError | 429 | Retried automatically; check the retry-after property if you’ve disabled retries. Also returned when a test-mode prompt budget is used up. |
ServerError | 5xx | Retried automatically. |
NetworkError / TimeoutError | — | No response was received at all. |
Every error class in every SDK catches most-specific-first — order your catch/except blocks
that way, ending with the base VectaPayError/VectaPayError class as a catch-all.
Python only: PermissionError and TimeoutError are also names of built-in Python
exceptions (OSError subclasses). from vectapay import PermissionError, TimeoutError shadows
them in that scope — fine inside a small handler, but if a file already relies on the built-in
meaning (catching an OS-level permission or socket timeout error), import these two under an
alias instead: from vectapay import PermissionError as VectaPayPermissionError.
Environment and test-mode errors
These are request errors specific to which key and which mode you are using. None of them come from the customer — they mean the call itself was aimed at the wrong place.
| Status | Message | Cause |
|---|---|---|
| 401 | API key environment mismatch. Required: LIVE, Got: TEST. | A pk_test_ key was sent to a live-only route — payouts, refunds, payment retries, or batch operations. Create a pk_live_ key, or take the call out of your test suite. |
| 400 | Verify the phone you want to test with first: open Developer → Testing and send the KES 10 verification prompt. | A test-mode collect() before any phone is verified. |
| 400 | Test payments can only be sent to your verified test phone (the number ending in 678). … | A test-mode collect() to a number that is not the verified one. The message shows the last three digits of the verified number. |
| 400 | Test payments always run on our test line. Remove providerAccountId from the request. | A test-mode collect() with a providerAccountId that isn’t the test line. Remove it — test mode always picks the line for you. |
| 429 | Too many test prompts have been sent. Try again in about N minutes. | A test-mode prompt budget is used up. Read retryAfterSeconds from the error body and wait — see Testing for the budgets. |
Payment outcome failures — the request succeeded, but the payment didn’t (the customer
cancelled, insufficient funds) — arrive later, on the payment record or a payment.failed
webhook, as a FailureCode. This is the taxonomy to branch your business logic on — see
Events for a worked example per language.
FailureCode reference
| Code | What happened | What to tell the customer |
|---|---|---|
CUSTOMER_CANCELLED | They dismissed the prompt on their phone. | Ask them to try again. |
CUSTOMER_TIMEOUT | They didn’t enter their PIN in time. | Send the request again. |
INSUFFICIENT_FUNDS | Not enough money in their account. | Ask them to top up and retry. |
INVALID_ACCOUNT | The phone number or account isn’t valid. | Confirm the details and try again. |
INVALID_AMOUNT | This amount couldn’t be processed. | Check the amount and try again. |
LIMIT_EXCEEDED | Above the limit for a single payment. | Split it into smaller payments. |
DUPLICATE_PAYMENT | A matching payment already exists. | Check your payments list first. |
SUBSCRIBER_BUSY | The customer’s phone has another M-Pesa transaction in progress. | Ask them to finish or cancel it, then retry in a minute. |
PROVIDER_UNAVAILABLE | The provider isn’t responding right now. | Nothing to do — it’ll be retried. |
PROVIDER_REJECTED | The provider declined this payment. | Try a different payment method. |
AUTHENTICATION_FAILED | VectaPay couldn’t connect to your payment account. | Check your payment method setup. |
NETWORK_TIMEOUT | The connection to the provider timed out. | Nothing to do — it’ll be retried. |
UNKNOWN | Unclassified failure. | Contact support if this keeps happening. |
This is the same taxonomy shown to merchants in plain language in the dashboard — see Why did a payment fail? if you’re building a merchant-facing surface of your own and want copy to match.