Skip to main content

JavaScript / Node.js SDK

The official Cashless SDK for JavaScript and Node.js. Published on npm as @cashless/sdk.

Installation

npm install @cashless/sdk

Quick start

import { cashless } from '@cashless/sdk';

// Initialize (use .sandbox() for testing)
const api = cashless.api.live('YOUR_KEY', 'YOUR_SECRET');

// Create a bill
const bill = await api.issue(
new cashless.BillParams('0712345678', 5000, 'ORDER-001')
);
console.log(bill.webPayUrl);

// Check payment status
const updated = await api.get(bill.id);
console.log(updated.status.name); // "FULLY_PAID", "PARTIALLY_PAID", or "NOT_PAID"

API

cashless.api.live(key, secret)

Creates a billing API client for the production environment (api.cashless.co.tz).

cashless.api.sandbox(key, secret)

Creates a billing API client for the sandbox environment (api-sandbox.cashless.co.tz).

api.issue(params): Promise<BillDto>

Creates a new bill.

Parameters:

  • params -- A cashless.BillParams instance

Returns: Promise<BillDto>

api.get(id): Promise<BillDto>

Retrieves a bill by its UUID.

Parameters:

  • id -- Bill UUID string

Returns: Promise<BillDto>

new cashless.BillParams(mobile, amount, reference)

Creates bill parameters.

ParameterTypeDescription
mobilestring | nullCustomer phone number (pass null to skip USSD push)
amountnumberAmount in TZS
referencestringYour unique transaction reference

cashless.BillDto

PropertyTypeDescription
.idstringBill UUID
.mobilestringCustomer phone
.amountnumberBill amount
.statusStatusPayment status enum
.referencestringYour reference
.webPayUrlstringWeb payment page URL
.datenumberCreation timestamp

cashless.BillDto.Status

ValueMeaning
FULLY_PAIDFull payment received
PARTIALLY_PAIDPartial payment received
NOT_PAIDNo payment yet

TypeScript

The SDK includes TypeScript definitions. Types are available under the cashless namespace:

import { cashless } from '@cashless/sdk';

const api: cashless.BillingAPI = cashless.api.live(key, secret);
const bill: cashless.BillDto = await api.issue(params);

Example: Express.js integration

import express from 'express';
import { cashless } from '@cashless/sdk';

const app = express();
const api = cashless.api.live(process.env.CASHLESS_KEY, process.env.CASHLESS_SECRET);

app.post('/checkout', async (req, res) => {
const { mobile, amount, orderId } = req.body;

const bill = await api.issue(
new cashless.BillParams(mobile || null, amount, orderId)
);

// Redirect to payment page, or return bill info
res.json({
paymentUrl: bill.webPayUrl,
billId: bill.id,
});
});

app.get('/payment-status/:billId', async (req, res) => {
const bill = await api.get(req.params.billId);
res.json({ status: bill.status.name });
});