PHP code example of moshipp / mosend-sdk

1. Go to this page and download the library: Download moshipp/mosend-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

moshipp / mosend-sdk example snippets


use Mosend\MosendClient;

$mosend = new MosendClient([
    'apiKey' => getenv('MOSEND_API_KEY'),   // mk_live_<prefix>.<secret>
    'orgId'  => getenv('MOSEND_ORG_ID'),    // UUID de tu organización
]);

// Enviar una plantilla
$msg = $mosend->messages->send([
    'phoneNumberId' => '<phone-uuid>',
    'to'            => '573001234567',       // E.164 sin '+'
    'type'          => 'template',
    'templateId'    => '<uuid-de-la-plantilla>',
    'variables'     => ['Juan', 'FAC-2026-0042'],
]);
echo $msg['id'], ' ', $msg['metaMessageId'], PHP_EOL;

// Paginar conversaciones (maneja el cursor por vos)
foreach ($mosend->conversations->iterate(['status' => 'open']) as $conv) {
    echo $conv['id'], PHP_EOL;
}

use Mosend\Exception\MosendRateLimitException;
use Mosend\Exception\MosendApiException;

try {
    $mosend->messages->send([...]);
} catch (MosendRateLimitException $e) {
    error_log('rate limit; reintentar en ' . $e->getRetryAfterSec() . 's');
} catch (MosendApiException $e) {
    error_log($e->getStatus() . ' ' . $e->getMessage() . ' meta=' . $e->getMetaCode());
}

use Mosend\Webhooks\WebhookVerifier;
use Mosend\Exception\MosendWebhookSignatureException;

$raw = file_get_contents('php://input');           // body CRUDO
$sig = $_SERVER['HTTP_X_MOSEND_SIGNATURE'] ?? null;

try {
    $event = WebhookVerifier::parseEvent($raw, $sig, getenv('MOSEND_WEBHOOK_SECRET'));
    // $event['event'] => 'message.new' | 'message.status' | ...
    // deduplicá por $event['deliveryId']
} catch (MosendWebhookSignatureException $e) {
    http_response_code(401);
    exit;
}

$mosend->messages->send([...], ['idempotencyKey' => 'order-42-greeting']);

$mosend = new MosendClient([
    'apiKey'  => '...',
    'orgId'   => '...',
    'retries' => ['max' => 3, 'on' => [429, 502, 503]],   // opt-in, con backoff
]);
bash
composer install
composer test          # PHPUnit
php scripts/smoke.php   # verificación rápida sin composer