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

# Accepting Payments with Monieswitch in PHP

> A step-by-step guide to integrating Monieswitch APIs for payments and payouts using PHP.

### Who is this for?

<Info>
  PHP developers who want to accept payments, create payment links, or automate
  payouts using Monieswitch APIs.
</Info>

## Overview

This guide covers:

* Setting up your PHP 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 PHP">
    **PHP 7.4+** installed. Download from
    [php.net](https://www.php.net/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 [Guzzle](https://docs.guzzlephp.org/en/stable/) for HTTP requests. Install via Composer:

```bash theme={"system"}
composer require guzzlehttp/guzzle vlucas/phpdotenv
```

***

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

```php merchant_details.php theme={"system"}
<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;
use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__ . '/../../../../');
$dotenv->load();

$apiBase = 'https://nini.monieswitch.com';
$apiKey = $_ENV['MONIESWITCH_API_KEY'];

$client = new Client([
    'base_uri' => $apiBase,
    'headers' => [
        'Authorization' => "Bearer $apiKey",
        'Content-Type'  => 'application/json'
    ]
]);

try {
    $response = $client->get('/merchant/details');
    $data = json_decode($response->getBody(), true);
    echo "Merchant Details:\n";
    print_r($data);
} catch (\GuzzleHttp\Exception\RequestException $e) {
    if ($e->hasResponse()) {
        echo "API Error: " . $e->getResponse()->getStatusCode() . "\n";
        echo $e->getResponse()->getBody();
    } else {
        echo "Request Error: " . $e->getMessage();
    }
}
```

***

## 4. Creating a Payment Link

```php create_payment_link.php theme={"system"}
<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;
use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__ . '/../../../../');
$dotenv->load();

$apiBase = 'https://nini.monieswitch.com';
$apiKey = $_ENV['MONIESWITCH_API_KEY'];

$client = new Client([
    'base_uri' => $apiBase,
    'headers' => [
        'Authorization' => "Bearer $apiKey",
        'Content-Type'  => 'application/json'
    ]
]);

$payload = [
    'amount' => 5000,
    'channel' => 'API',
    'name' => 'Product',
    'currency' => 'NGN',
    'description' => 'Test payment link',
    'email' => 'customer@example.com'
];

try {
    $response = $client->post('/payment-links', [
        'json' => $payload
    ]);
    $data = json_decode($response->getBody(), true);
    echo "Payment Link Created:\n";
    print_r($data);
} catch (\GuzzleHttp\Exception\RequestException $e) {
    if ($e->hasResponse()) {
        echo "API Error: " . $e->getResponse()->getStatusCode() . "\n";
        echo $e->getResponse()->getBody();
    } else {
        echo "Request Error: " . $e->getMessage();
    }
}
```

***

## 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 mobile apps ([guide](/guides/integrate-monieswitch-mobile-apps))
* Set up [webhooks](/features/payments/webhook) for real-time notifications
