PHP code example of pyrx / synapse

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

    

pyrx / synapse example snippets


use PyrxSynapse\Client;

$client = new Client(
    apiKey: 'psk_live_your_api_key',
    workspaceId: 'ws_your_workspace_id',
);

// Track an event
$result = $client->track(
    externalId: 'user_123',
    eventName: 'purchase',
    attributes: ['amount' => 99.99, 'currency' => 'USD'],
);
echo $result['event_id']; // "evt_..."

// Identify a contact
$contact = $client->identify(
    externalId: 'user_123',
    email: '[email protected]',
    firstName: 'Jane',
    lastName: 'Doe',
    properties: ['plan' => 'pro'],
    tags: ['paying', 'onboarded'],
);

// Send a transactional email
$email = $client->sendEmail(
    templateSlug: 'welcome',
    to: ['email' => '[email protected]'],
    attributes: ['name' => 'Jane'],
);

$client = new Client(
    apiKey: 'psk_live_...',          // Required
    workspaceId: 'ws_...',           // Required
    baseUrl: 'https://synapse-api.pyrx.tech', // Optional (default)
    timeout: 30,                     // Optional, seconds (default: 30)
    maxRetries: 3,                   // Optional (default: 3)
);

// Environment is auto-detected from the API key prefix
echo $client->environment; // "live", "test", or "unknown"

// Single event
$result = $client->track(
    externalId: 'user_123',
    eventName: 'page_view',
    attributes: ['page' => '/pricing'],
    idempotencyKey: 'idk_unique_123',
    occurredAt: '2026-01-15T10:30:00Z',
);

// Batch events
$result = $client->trackBatch([
    ['external_id' => 'user_1', 'event_name' => 'login'],
    ['external_id' => 'user_2', 'event_name' => 'signup'],
]);
echo $result['accepted']; // 2

// Identify (create or update)
$contact = $client->identify(
    externalId: 'user_123',
    email: '[email protected]',
    firstName: 'Jane',
);

// Batch identify
$result = $client->identifyBatch(
    contacts: [
        ['external_id' => 'u1', 'email' => '[email protected]'],
        ['external_id' => 'u2', 'email' => '[email protected]'],
    ],
    onConflict: 'update',
);

// List contacts
$list = $client->contacts->list(page: 1, perPage: 20, search: 'jane');

// Get a contact
$contact = $client->contacts->get('contact_id');

// Update a contact
$updated = $client->contacts->update('user_123', ['email' => '[email protected]']);

// Delete a contact
$client->contacts->delete('user_123');

// List templates
$templates = $client->templates->list();

// Get a template
$template = $client->templates->get('welcome');

// Create a template
$template = $client->templates->create([
    'name' => 'Welcome Email',
    'slug' => 'welcome',
    'subject' => 'Welcome, {{name}}!',
    'body_html' => '<h1>Hello {{name}}</h1>',
]);

// Update a template
$updated = $client->templates->update('welcome', ['subject' => 'Hey {{name}}!']);

// Preview a template
$preview = $client->templates->preview('welcome', [
    'attributes' => ['name' => 'Jane'],
]);
echo $preview['html'];

// Delete a template
$client->templates->delete('old-template');

use PyrxSynapse\Webhooks;

$payload = file_get_contents('php://input');
$headers = [
    'svix-id' => $_SERVER['HTTP_SVIX_ID'] ?? '',
    'svix-timestamp' => $_SERVER['HTTP_SVIX_TIMESTAMP'] ?? '',
    'svix-signature' => $_SERVER['HTTP_SVIX_SIGNATURE'] ?? '',
];

try {
    $event = Webhooks::verify($payload, $headers, 'whsec_your_webhook_secret');
    // $event is the parsed JSON payload
    echo $event['type']; // e.g., "contact.created"
} catch (\InvalidArgumentException $e) {
    http_response_code(400);
    echo $e->getMessage();
}

use PyrxSynapse\Errors\SynapseError;
use PyrxSynapse\Errors\SynapseAuthError;
use PyrxSynapse\Errors\SynapseRateLimitError;
use PyrxSynapse\Errors\SynapsePlanLimitError;
use PyrxSynapse\Errors\SynapseValidationError;

try {
    $client->track('user_1', 'event');
} catch (SynapseAuthError $e) {
    // 401 or 403 (not plan limit)
    echo "Auth error: {$e->getMessage()} (status: {$e->status})";
} catch (SynapseRateLimitError $e) {
    // 429
    echo "Rate limited. Retry after {$e->retryAfter} seconds.";
} catch (SynapsePlanLimitError $e) {
    // 403 with code "plan_limit_reached"
    echo "Plan limit: {$e->current}/{$e->maximum} {$e->limitType} on {$e->plan} plan";
} catch (SynapseValidationError $e) {
    // 422
    foreach ($e->errors as $error) {
        echo "{$error['field']}: {$error['message']}\n";
    }
} catch (SynapseError $e) {
    // Any other API error
    echo "Error: {$e->getMessage()} (status: {$e->status}, code: {$e->errorCode})";
}