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

1

Set Up Node.js

Node.js installed (v14 or higher recommended). Download from nodejs.org.
2

Create Monieswitch Account

A Monieswitch account. Sign up at Monieswitch Dashboard.
3

Get API Key

API Key from your Monieswitch dashboard.

1. Install Dependencies

We recommend using axios for HTTP requests and dotenv for environment variables.
npm install axios dotenv

2. Configure Your API Key

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:
merchant.js

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}/api/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();

Here’s how to create a payment link using the Monieswitch API:
paymentLink.js

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: 5000,
      currency: "NGN",
      description: "Test payment link",
      customer: {
        email: "[email protected]",
      },
    };

    const response = await axios.post(
      `${API_BASE}/api/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