Cash flow forecasting from Paystack: 1) Export monthly revenue totals from GET /transaction (group by month, sum amounts). 2) Get MRR from active subscriptions: GET /subscription?status=active, sum amounts. 3) Get settlement schedule: GET /settlement for historical payout timing (T+1 or T+2). 4) Feed aggregated data to LLM — not raw transactions. 5) Prompt for projection with stated assumptions. Key: the LLM should return a range (low/mid/high), not a single number, and must state what assumptions drive the projection. Never use an AI cash flow projection for financial commitments without human review.
// Prepare aggregated data for LLM — never raw transactions
async function buildForecastInput() {
var headers = { Authorization: 'Bearer ' + process.env.PAYSTACK_SECRET_KEY };
// 1. Monthly revenue for past 6 months
var monthlyRevenue = [];
for (var i = 5; i >= 0; i--) {
var d = new Date();
d.setMonth(d.getMonth() - i);
var from = new Date(d.getFullYear(), d.getMonth(), 1).toISOString().slice(0, 10);
var to = new Date(d.getFullYear(), d.getMonth() + 1, 0).toISOString().slice(0, 10);
var res = await fetch('https://api.paystack.co/transaction?status=success&from=' + from + '&to=' + to + '&perPage=100', { headers });
var txns = (await res.json()).data || [];
monthlyRevenue.push({
month: from.slice(0, 7),
total_ngn: txns.reduce((s, t) => s + t.amount, 0) / 100,
count: txns.length,
});
}
// 2. Current MRR from active subscriptions
var subsRes = await fetch('https://api.paystack.co/subscription?status=active&perPage=100', { headers });
var subs = (await subsRes.json()).data || [];
var mrr = subs.reduce((s, sub) => s + sub.amount, 0) / 100;
// 3. Settlement lag (from recent settlements)
var settleRes = await fetch('https://api.paystack.co/settlement?perPage=10', { headers });
var settlements = (await settleRes.json()).data || [];
var avgLagDays = settlements.length
? settlements.reduce((s, st) => {
var txDate = new Date(st.effective_date);
var settleDate = new Date(st.settlement_date);
return s + (settleDate - txDate) / 86400000;
}, 0) / settlements.length
: 1; // default T+1
return { monthlyRevenue, mrr_ngn: mrr, active_subscriptions: subs.length, avg_settlement_lag_days: avgLagDays };
}
// Feed to LLM for forecast
async function generateCashFlowForecast() {
var data = await buildForecastInput();
var prompt = `Here is our Paystack revenue data for the past 6 months:
Monthly revenue: ${JSON.stringify(data.monthlyRevenue)}
Current MRR from active subscriptions: NGN ${data.mrr_ngn}
Active subscriptions: ${data.active_subscriptions}
Average settlement lag: ${data.avg_settlement_lag_days.toFixed(1)} days
Based on this data, forecast next month's revenue (low/mid/high range).
State the assumptions driving each scenario.
Highlight any trends you see in the monthly data.
Note: Paystack settles net of fees (~1.5% + NGN 100 per transaction).`;
var response = await anthropic.messages.create({
model: 'claude-opus-4-6',
max_tokens: 1024,
messages: [{ role: 'user', content: prompt }],
});
return response.content[0].text;
}
See building a natural language revenue dashboard for the interactive query pattern over the same data.
KES 2,999
The definitive guide to building payment systems with Paystack — from your first transaction to production-grade payment infrastructure across Africa. 9 modules, 47 lessons. Free preview available.
Start Paystack Course