Initialize the SDK with your API key and make your first request:
import FingoPay from '@fingoafrica/fingo-pay';const client = new FingoPay({ apiKey: process.env['FINGO_PAY_API_KEY'], // This is the default and can be omitted});const acceptedResponse = await client.mpesa.createCharge({ amount: 150000, merchantTransactionId: 'invoice_48291', phoneNumber: '+254712345678',});console.log(acceptedResponse.data);
Never expose your secret API key in client-side code or version control. Use environment variables.
When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of APIError will be thrown:
The “raw” Response returned by fetch() can be accessed through the .asResponse() method on the APIPromise type. This method returns as soon as the headers for a successful response are received and does not consume the response body, allowing custom parsing or streaming logic.Use .withResponse() to get the raw Response along with the parsed data:
const client = new FingoPay();// Access raw response without consuming bodyconst response = await client.mpesa .createCharge({ amount: 150000, merchantTransactionId: 'invoice_48291', phoneNumber: '+254712345678', }) .asResponse();console.log(response.headers.get('X-My-Header'));console.log(response.statusText);// Get both parsed data and raw responseconst { data: acceptedResponse, response: raw } = await client.mpesa .createCharge({ amount: 150000, merchantTransactionId: 'invoice_48291', phoneNumber: '+254712345678', }) .withResponse();console.log(raw.headers.get('X-My-Header'));console.log(acceptedResponse.data);
Using the logLevel client option (overrides the environment variable)
import FingoPay from '@fingoafrica/fingo-pay';const client = new FingoPay({ logLevel: 'debug', // Show all log messages});
Available log levels, from most to least verbose:
Level
Description
debug
Show debug messages, info, warnings, and errors
info
Show info messages, warnings, and errors
warn
Show warnings and errors (default)
error
Show only errors
off
Disable all logging
At the debug level, all HTTP requests and responses are logged, including headers and bodies. Some authentication-related headers are redacted, but sensitive data in request and response bodies may still be visible.
Provide a custom logger instead of the default globalThis.console. Most logging libraries are supported, including pino, winston, bunyan, consola, signale, and @std/log.
import FingoPay from '@fingoafrica/fingo-pay';import pino from 'pino';const logger = pino();const client = new FingoPay({ logger: logger.child({ name: 'FingoPay' }), logLevel: 'debug',});
Provide custom fetchOptions to add runtime-specific proxy options:
Node.js
Bun
Deno
import FingoPay from '@fingoafrica/fingo-pay';import * as undici from 'undici';const proxyAgent = new undici.ProxyAgent('http://localhost:8888');const client = new FingoPay({ fetchOptions: { dispatcher: proxyAgent, },});
import FingoPay from '@fingoafrica/fingo-pay';const client = new FingoPay({ fetchOptions: { proxy: 'http://localhost:8888', },});