Skip to content

Conversation events in PHP

Send a live transcript from your PHP app. Sparkle sends a signed webhook the moment someone makes a decision, asks a question or commits to something.

Plain HTTP, no SDK to install. Test keys are never billed.

Integrate with any PHP application

Sparkle is an HTTP API, so it works from Laravel, Symfony, WordPress or a plain script: anything that can make an HTTPS request.

  1. Create a conversation with your webhook URL. Keep the webhook.secret it returns: it is shown once.
  2. Post each turn once it is final. Every POST carries your X-API-Key and a fresh Idempotency-Key.
Five-minute quickstart
send.php curl extension
<?php

const API = 'https://api.sparkle.ai/public/v1';

function post(string $path, array $body): ?array
{
    $ch = curl_init(API . $path);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => json_encode($body),
        CURLOPT_HTTPHEADER => [
            'X-API-Key: ' . getenv('SPARKLE_API_KEY'),
            'Idempotency-Key: ' . bin2hex(random_bytes(16)),
            'Content-Type: application/json',
        ],
    ]);
    return json_decode(curl_exec($ch), true);
}

$created = post('/conversations', [
    'title' => 'Acme renewal call',
    'webhook_url' => 'https://yourapp.com/hooks/sparkle',
]);

post("/conversations/{$created['conversation']['id']}/turns", [
    'turns' => [[
        'id' => 't2', 'final' => true, 'speaker' => 'dana', 'name' => 'Dana',
        'channel' => 'remote', 'start' => 21.0, 'end' => 23.4,
        'text' => "I'll get the SSO fix out by Friday.",
    ]],
]);

Receive moments with plain PHP

Each moment arrives as a moment.detected POST. Its Sparkle-Signature header is an HMAC of a timestamp and the body, keyed with the webhook's whsec_ secret.

  • Check the signature on the raw body, before parsing it.
  • Reject a timestamp more than 5 minutes from your clock.
  • Accept any matching v1: while a rotated secret overlaps, each delivery carries two.
  • Answer 2xx within 30 seconds. A retry repeats the event id, so skip one you have handled.

php://input holds the body as Sparkle sent it. In Laravel, read $request->getContent() and exclude the route from CSRF protection.

For a live UI, the conversation's event stream sends the same events as server-sent events.

webhook.php no framework, PHP 7.1+
<?php

function verify(string $secret, string $header, string $body): bool
{
    $t = '';
    $signatures = [];
    foreach (explode(',', $header) as $part) {
        [$key, $value] = array_pad(explode('=', trim($part), 2), 2, '');
        if ($key === 't') {
            $t = $value;
        } elseif ($key === 'v1') {
            $signatures[] = $value;
        }
    }
    if (!ctype_digit($t) || abs(time() - (int) $t) > 300) {
        return false;
    }
    $expected = hash_hmac('sha256', $t . '.' . $body, $secret);
    foreach ($signatures as $signature) {
        if (hash_equals($expected, $signature)) {
            return true;
        }
    }
    return false;
}

$body = file_get_contents('php://input');
$header = $_SERVER['HTTP_SPARKLE_SIGNATURE'] ?? '';
if (!verify((string) getenv('SPARKLE_WEBHOOK_SECRET'), $header, $body)) {
    http_response_code(400);
    exit('invalid signature');
}

$event = json_decode($body, true);
// A retry repeats $event['id']: skip one you have already handled.
if ($event['type'] === 'moment.detected') {
    $moment = $event['data'];
    error_log("{$moment['kind']} {$moment['speaker']['name']} {$moment['text']}");
}
http_response_code(204);

Send your first transcript

Create a test key, then run the samples above against it. Test conversations are never billed.