AI tools
Composer
Pick a task and a language and get working integration code, step by step.
Tell the Composer what you're building and which language you use. Copy each step into your project.
I want to
In
- 1
Start a payment
Create a payment and get a checkout URL to send your customer to. They can pay by card or bank transfer.
Node.jsconst res = await fetch("https://sandbox.cswitchcba.com/v1/payments", { method: "POST", headers: { Authorization: `Bearer ${process.env.CSWITCH_SECRET_KEY}`, "Content-Type": "application/json", "Idempotency-Key": crypto.randomUUID(), }, body: JSON.stringify({ "amount": 4200000, "email": "ngozi@example.com", "reference": "order_2231", "callback_url": "https://yourshop.ng/thanks" }), }); const { data } = await res.json(); - 2
Verify a payment
Confirm a payment's status and amount on your server before you release goods or services.
Node.jsconst res = await fetch("https://sandbox.cswitchcba.com/v1/payments/order_2231", { method: "GET", headers: { Authorization: `Bearer ${process.env.CSWITCH_SECRET_KEY}`, }, }); const { data } = await res.json(); - 3
Confirm it with a webhook
Trust the event only after checking its signature, then verify on your server.
Node.jsimport crypto from "node:crypto"; // In your webhook route: check the signature on the raw body first export function isFromCSwitch(rawBody, signature) { const expected = crypto.createHmac("sha512", process.env.CSWITCH_SECRET_KEY).update(rawBody).digest("hex"); return crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(signature ?? "")); }