PHP code example of nexiom / connect-php

1. Go to this page and download the library: Download nexiom/connect-php 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/ */

    

nexiom / connect-php example snippets


use Nexiom\Connect\NexiomConnect;

$nexiomConnect = new NexiomConnect('nc_your_api_key_here');

// Optional custom host:
// $nexiomConnect = new NexiomConnect(
//     apiKey: 'nc_...',
//     baseUrl: 'https://api-connect.nxiom.com/api',
// );

use Nexiom\Connect\Exceptions\NexiomException;

try {
    $email = $nexiomConnect->emails->send([
        'from' => '[email protected]',
        'fromName' => 'Your team',
        'to' => '[email protected]',
        'subject' => 'Welcome',
        'html' => '<p>Thanks for joining us.</p>',
        'text' => 'Thanks for joining us.',
    ]);

    echo $email['messageId'];
} catch (NexiomException $error) {
    error_log($error->getMessage());
}

$nexiomConnect->emails->send(
    [
        'from' => '[email protected]',
        'to' => '[email protected]',
        'subject' => 'Order received',
        'text' => 'We received your order.',
    ],
    ['idempotencyKey' => 'order-123-confirmation'],
);

$email = $nexiomConnect->emails->send([
    'from' => '[email protected]',
    'to' => '[email protected]',
    'subject' => 'Your trial ends tomorrow',
    'text' => 'Your trial ends tomorrow.',
    'scheduledAt' => new DateTimeImmutable('+1 day'),
]);

$nexiomConnect->emails->reschedule($email['messageId'], [
    'scheduledAt' => '2026-10-06T09:00:00+01:00',
]);

$nexiomConnect->emails->cancel($email['messageId']);

$page = $nexiomConnect->emails->list(['status' => 'bounced', 'limit' => 50]);

foreach ($page['items'] as $delivery) {
    echo $delivery['id'], ' ', $delivery['recipient'], ' ', $delivery['status'], PHP_EOL;
}

// Next page: pass $page['nextCursor'] as 'cursor'; it is null on the last page.

$page = $nexiomConnect->emails->suppressions->list(['search' => 'example.com', 'limit' => 50]);

foreach ($page['items'] as $suppression) {
    echo $suppression['email'], ' ', $suppression['reason'], PHP_EOL;
}

// Next page: pass $page['nextCursor'] as 'cursor' while $page['hasMore'] is true.

$contact = $nexiomConnect->contacts->create([
    'email' => '[email protected]',
    'firstName' => 'Ada',
    'lastName' => 'Lovelace',
    'properties' => ['company' => 'Example'],
]);

echo $contact['id'];

$property = $nexiomConnect->contacts->properties->create([
    'name' => 'company',
    'type' => 'string',
    'fallbackValue' => 'Unknown',
]);

echo $property['id'], ' ', $property['key'];

$templates = $nexiomConnect->templates->list(['status' => 'published', 'limit' => 50]);

$template = $nexiomConnect->templates->get($templates['items'][0]['id']);

foreach ($template['published_version']['variables'] ?? [] as $variable) {
    echo $variable['key'], $variable['

$domain = $nexiomConnect->domains->create(['domain' => 'mail.example.com']);

foreach ($domain['dns_records'] as $record) {
    echo $record['record_type'], ' ', $record['name'], ' ', $record['value'], PHP_EOL;
}

// After publishing the records with your DNS provider:
$domain = $nexiomConnect->domains->verify($domain['id']);

echo $domain['status'];

$message = $nexiomConnect->messaging->send(
    [
        'channel' => 'whatsapp',
        'from' => '+213555000111',
        'to' => '+213555123456',
        'template' => [
            'id' => 'wat_your_template_id',
            'variables' => [
                'body' => [1 => 'Amina', 2 => 'A-1234'],
            ],
        ],
    ],
    ['idempotencyKey' => 'order-A-1234-shipped'],
);

echo $message['id'], ' ', $message['status'];

// routes/api.php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Nexiom\Connect\Exceptions\NexiomWebhookVerificationException;
use Nexiom\Connect\NexiomConnect;

Route::post('/webhooks/nexiom', function (Request $request) {
    $nexiomConnect = new NexiomConnect(config('services.nexiom.key'));

    try {
        $event = $nexiomConnect->webhooks->constructEvent(
            $request->getContent(),
            $request->headers->all(),
            config('services.nexiom.webhook_secret'),
        );
    } catch (NexiomWebhookVerificationException $exception) {
        return response($exception->reason, 400);
    }

    if ($event['type'] === 'email.bounced') {
        logger()->warning('Bounced: ' . $event['data']['recipient']);
    }

    return response()->noContent();
});

$nexiomConnect = new NexiomConnect(
    apiKey: 'nc_...',
    timeout: 10,
    maxRetries: 3,
);

$contacts = $nexiomConnect->contacts->list([], ['timeout' => 5]);

use Nexiom\Connect\Exceptions\ErrorKind;
use Nexiom\Connect\Exceptions\NexiomException;

try {
    $nexiomConnect->contacts->get('ct_123');
} catch (NexiomException $error) {
    if ($error->kind === ErrorKind::Api && $error->status === 404) {
        // The contact does not exist.
    }
}