PHP SDK

Official SDK for PHP and Laravel applications.

Installation

composer require notification-system/sdk

Requirements

  • • PHP 8.0 or later
  • • Composer
  • • ext-json, ext-curl

Quick Start

<?php

require_once 'vendor/autoload.php';

use NotificationSystem\Client;

// Initialize client
$client = new Client(getenv('NOTIFICATION_API_KEY'));

// Send notification
$response = $client->sendNotification([
    'type' => ['email', 'sms'],
    'content' => 'Hello World!',
    'target' => ['user@example.com', '+1234567890'],
]);

echo "Notification ID: " . $response['id'] . "\n";
echo "Status: " . $response['status'] . "\n";

Laravel Integration

Add to your config/services.php:

// config/services.php
return [
    // ...
    'notification_system' => [
        'api_key' => env('NOTIFICATION_API_KEY'),
        'timeout' => 30,
    ],
];

Register the service provider:

// app/Providers/AppServiceProvider.php
use NotificationSystem\Client;

public function register()
{
    $this->app->singleton(Client::class, function ($app) {
        return new Client(
            config('services.notification_system.api_key'),
            config('services.notification_system.timeout')
        );
    });
}

API Methods

$client->sendNotification(array $options)

Send a notification to one or more channels.

$response = $client->sendNotification([
    'type' => ['email', 'sms', 'slack'],
    'content' => 'Your order has been shipped!',
    'target' => ['user@example.com', '+1234567890', '#orders'],
    'email' => [
        'subject' => 'Order Shipped',
        'html' => '<h1>Your order is on the way!</h1>',
    ],
    'metadata' => [
        'orderId' => '12345',
    ],
]);
$client->getNotification(string $id)

Get details of a specific notification.

$notification = $client->getNotification('ntf_abc123');
echo "Status: " . $notification['status'];
$client->listNotifications(?int $page, ?int $perPage, ?string $status, ?string $channel)

List notifications with pagination and filters.

$result = $client->listNotifications(
    page: 1,
    perPage: 20,
    status: 'delivered',
    channel: 'email'
);

foreach ($result['notifications'] as $notification) {
    echo "ID: {$notification['id']}, Status: {$notification['status']}\n";
}
$client->createIntegration(string $projectId, array $data)

Create a platform integration with its own API keys.

$integration = $client->createIntegration('project-id', [
    'name' => 'Production Backend',
    'platform_type' => 'backend',
    'environment' => 'production',
    'sdk_language' => 'php',
]);
$client->createApiKey(string $integrationId, ?string $description)

Create a key scoped to one platform integration.

$apiKey = $client->createApiKey('integration-id', 'Backend worker key');
echo $apiKey['api_key']['key'];

Error Handling

<?php

use NotificationSystem\Client;
use NotificationSystem\Exceptions\ApiException;
use NotificationSystem\Exceptions\NetworkException;

try {
    $response = $client->sendNotification([...]);
} catch (ApiException $e) {
    echo "API error: " . $e->getMessage() . " (status: " . $e->getStatusCode() . ")\n";
} catch (NetworkException $e) {
    echo "Network error: " . $e->getMessage() . "\n";
}

Laravel Notification Channel

Use as a Laravel notification channel:

<?php
// app/Notifications/OrderShipped.php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use NotificationSystem\Laravel\NotificationChannel;
use NotificationSystem\Laravel\NotificationMessage;

class OrderShipped extends Notification
{
    public function via($notifiable)
    {
        return [NotificationChannel::class];
    }

    public function toNotificationSystem($notifiable)
    {
        return (new NotificationMessage)
            ->type(['email', 'sms'])
            ->content('Your order has been shipped!')
            ->email([
                'subject' => 'Order Shipped',
                'html' => view('emails.order-shipped', ['order' => $this->order])->render(),
            ]);
    }
}

// Usage
$user->notify(new OrderShipped($order));