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

# Using Monieswitch APIs in Python

> A detailed guide to connecting, authenticating, and interacting with the Monieswitch API using Python.

### Who is this for?

<Info>
  Python developers who want to automate payments, manage wallets, or integrate
  Monieswitch into their backend services.
</Info>

## Overview

This guide covers:

* Setting up your Python environment
* Authenticating with the Monieswitch API
* Making your first API request
* Creating a payment link
* Handling errors and best practices

***

## Prerequisites

<Steps>
  <Step title="Set Up Python">
    **Python 3.7+** installed. Download from
    [python.org](https://www.python.org/downloads/).
  </Step>

  <Step title="Create Monieswitch Account">
    **A Monieswitch account**. Register 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 [requests](https://pypi.org/project/requests/) for HTTP requests and [python-dotenv](https://pypi.org/project/python-dotenv/) for environment variables.

```bash theme={"system"}
pip install requests python-dotenv
```

***

## 2. Configure Your API Key

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

```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:

```python merchant_details.py theme={"system"}
import os
import requests
from dotenv import load_dotenv

load_dotenv()

API_BASE = "https://nini.monieswitch.com"
API_KEY = os.getenv("MONIESWITCH_API_KEY")

def get_merchant_details():
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }
    try:
        response = requests.get(f"{API_BASE}/merchant/details", headers=headers)
        response.raise_for_status()
        print("Merchant Details:", response.json())
    except requests.exceptions.HTTPError as err:
        print("API Error:", err.response.status_code, err.response.json())
    except Exception as e:
        print("Request Error:", str(e))

if __name__ == "__main__":
    get_merchant_details()
```

***

## 4. Creating a Payment Link

```python create_payment_link.py theme={"system"}
import os
import requests
from dotenv import load_dotenv

load_dotenv()

API_BASE = "https://nini.monieswitch.com"
API_KEY = os.getenv("MONIESWITCH_API_KEY")

def create_payment_link():
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }
    payload = {
        "amount": 200,
        "channel": "API",
        "name": "Product Purchase",
        "description": "One-time setup fee",
        "currency": "NGN",
        "supportCard": true,
        "requiresName": true,
        "requiresPhone": true,
        "supportBankTransfer": true,
        "webhookURL": "https://yoursite.com/webhook/payment",
        "callbackURL": "https://yoursite.com/thank-you",
        "email":"customer@email.com"
    }
    try:
        response = requests.post(f"{API_BASE}/payment-links", json=payload, headers=headers)
        response.raise_for_status()
        print("Payment Link Created:", response.json())
    except requests.exceptions.HTTPError as err:
        print("API Error:", err.response.status_code, err.response.json())
    except Exception as e:
        print("Request Error:", str(e))

if __name__ == "__main__":
    create_payment_link()
```

***

## 5. Handling Errors and Best Practices

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

* 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.

***

## Next Steps

* Explore more endpoints in the [API Reference](/api-reference/introduction)
* Learn how to accept payments with Monieswitch in PHP ([guide](/guides/integrate-monieswitch-php))
* Set up [webhooks](/features/payments/webhook) for real-time notifications
