> ## Documentation Index
> Fetch the complete documentation index at: https://docs.monieswitch.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrating Monieswitch API with Node.js

> A comprehensive guide to connecting, authenticating, and interacting with the Monieswitch API using Node.js.

### Who is this for?

<Info>
  This guide is for developers who want to connect their Node.js applications to
  Monieswitch, automate payments, manage wallets, or build custom integrations.
</Info>

## 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

<Steps>
  <Step title="Set Up Node.js">
    **Node.js** installed (v14 or higher recommended). Download from
    [nodejs.org](https://nodejs.org/).
  </Step>

  <Step title="Create Monieswitch Account">
    **A Monieswitch account**. Sign up at [Monieswitch
    Dashboard](https://dashboard.monieswitch.com/register).
  </Step>

  <Step title="Get API Key">**API Key** from your Monieswitch dashboard.</Step>
</Steps>

***

## 1. Install Dependencies

We recommend using [axios](https://www.npmjs.com/package/axios) for HTTP requests and [dotenv](https://www.npmjs.com/package/dotenv) for environment variables.

```bash theme={"system"}
npm install axios dotenv
```

***

## 2. Configure Your API Key

Create a `.env` file in your project root:

```env theme={"system"}
MONIESWITCH_API_KEY=your_api_key_here
```

<Warning>Never commit your `.env` file or API keys to source control.</Warning>

***

## 3. Making Your First API Request

Here’s a sample script to fetch your merchant details from Monieswitch:

```javascript merchant.js theme={"system"}
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:

```javascript paymentLink.js theme={"system"}
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: "customer@email.com",
      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

<Warning>
  Always check for HTTP status codes and handle errors gracefully. The
  Monieswitch API returns detailed error messages in the response body.
</Warning>

***

## 6. Best Practices

* **Keep your API keys secret** using environment variables.
* **Validate all API responses** before using the data.
* **Read the [API Reference](/api-reference/introduction)** 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](/api-reference/introduction)
* Learn how to accept payments with Monieswitch in Python ([guide](/guides/integrate-monieswitch-python))
* Set up [webhooks](/features/payments/webhook) for real-time notifications
