Who is this for?
This guide is for developers who want to connect their Node.js applications to
Monieswitch, automate payments, manage wallets, or build custom integrations.
Overview
This tutorial covers:
- Setting up your Node.js environment
- Authenticating with the Monieswitch API
- Making your first API request
- Handling errors and responses
- Best practices for security
Prerequisites
Set Up Node.js
Node.js installed (v14 or higher recommended). Download from
nodejs.org. Create Monieswitch Account
Get API Key
API Key from your Monieswitch dashboard.
1. Install Dependencies
We recommend using axios for HTTP requests and dotenv for environment variables.
Create a .env file in your project root:
MONIESWITCH_API_KEY=your_api_key_here
Never commit your .env file or API keys to source control.
3. Making Your First API Request
Here’s a sample script to fetch your merchant details from Monieswitch:
require("dotenv").config();
const axios = require("axios");
const API_BASE = "https://nini.monieswitch.com";
const API_KEY = process.env.MONIESWITCH_API_KEY;
async function getMerchantDetails() {
try {
const response = await axios.get(`${API_BASE}/merchant/details`, {
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
});
console.log("Merchant Details:", response.data);
} catch (error) {
if (error.response) {
console.error("API Error:", error.response.status, error.response.data);
} else {
console.error("Request Error:", error.message);
}
}
}
getMerchantDetails();
4. Creating a Payment Link
Here’s how to create a payment link using the Monieswitch API:
require("dotenv").config();
const axios = require("axios");
const API_BASE = "https://nini.monieswitch.com";
const API_KEY = process.env.MONIESWITCH_API_KEY;
async function createPaymentLink() {
try {
const payload = {
amount: 200,
channel: "API",
name: "Product Purchase",
description: "One-time setup fee",
supportCard: true,
requiresName: true,
requiresPhone: true,
supportBankTransfer: true,
webhookURL: "https://yoursite.com/webhook/payment",
callbackURL: "https://yoursite.com/thank-you",
email: "[email protected]",
fields: [
{ type: "text", name: "customer_name", label: "Full Name" },
{ type: "tel", name: "phone", label: "Phone Number" },
],
chargeBearer: "merchant",
reusable: true,
customLink: "your-custom-link",
};
const response = await axios.post(`${API_BASE}/payment-links`, payload, {
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
});
console.log("Payment Link Created:", response.data);
} catch (error) {
if (error.response) {
console.error("API Error:", error.response.status, error.response.data);
} else {
console.error("Request Error:", error.message);
}
}
}
createPaymentLink();
5. Handling Errors and Responses
Always check for HTTP status codes and handle errors gracefully. The
Monieswitch API returns detailed error messages in the response body.
6. Best Practices
- Keep your API keys secret using environment variables.
- Validate all API responses before using the data.
- Read the API Reference for all available endpoints and parameters.
- Use try/catch blocks to handle exceptions and network errors.
Next Steps
- Explore more endpoints in the API Reference
- Learn how to accept payments with Monieswitch in Python (guide)
- Set up webhooks for real-time notifications