Who is this for?

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

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

1

Set Up PHP

PHP 7.4+ installed. Download from php.net.
2

Create Monieswitch Account

A Monieswitch account. Register at Monieswitch Dashboard.
3

Get API Key

API Key from your Monieswitch dashboard.

1. Install Dependencies

We recommend using Guzzle for HTTP requests. Install via Composer:
composer require guzzlehttp/guzzle vlucas/phpdotenv

2. Configure Your API Key

Create a .env file in your project directory:
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_details.php
<?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('/api/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();
    }
}

create_payment_link.php
<?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,
    'currency' => 'NGN',
    'description' => 'Test payment link',
    'customer' => [
        'email' => '[email protected]'
    ]
];

try {
    $response = $client->post('/api/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

Always check for HTTP status codes and handle exceptions. The Monieswitch API returns detailed error messages in the response body.
  • 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.

Next Steps

  • Explore more endpoints in the API Reference
  • Learn how to accept payments with Monieswitch in mobile apps (guide)
  • Set up webhooks for real-time notifications