PHP code example of frain / convoy

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

    

frain / convoy example snippets


use Convoy\Client\Api\EventsApi;
use Convoy\Client\Configuration;
use Convoy\Client\Model\ModelsCreateEvent;

$config = (new Configuration())
    ->setHost('https://us.getconvoy.cloud/api')
    ->setAccessToken($apiKey); // the client adds the Bearer prefix

// Pin the API version this client was generated from.
$http = new \GuzzleHttp\Client([
    'headers' => ['X-Convoy-Version' => '2025-11-24'],
]);

$events = new EventsApi($http, $config);
$events->createEndpointEvent($projectId, (new ModelsCreateEvent())
    ->setEndpointId('endpoint-id')
    ->setEventType('invoice.paid')
    ->setData(['amount' => 100, 'currency' => 'USD']));

use Convoy\Convoy;

$convoy = new Convoy([
    "uri" => "https://us.getconvoy.cloud/api/v1",
    "api_key" => "your_api_key",
    "project_id" => "your_project_id"
]);

$endpointData = [
    "name" => "default-endpoint",
    "url" => "https://example.com/webhooks/convoy",
    "description" => "Default Endpoint",
    "secret" => "endpoint-secret"
];

$response = $convoy->endpoints()->create($endpointData);
$endpointId = $response["data"]["uid"];

$subscriptionData = [
    "name" => "event-sub",
    "endpoint_id" => $endpointId
];

$response = $convoy->subscriptions()->create($subscriptionData);

$eventData = [
    "endpoint_id" => $endpointId,
    "event_type" => "payment.success",
    "data" => [
        "status" => "Completed",
        "description" => "Transaction Successful"
    ]
];

$response = $convoy->events()->create($eventData);

$response = $convoy->events()->fanout(["owner_id" => "owner-1", "event_type" => "payment.success", "data" => []]);
$response = $convoy->events()->broadcast(["event_type" => "payment.success", "data" => []]);

use Convoy\Webhook;

$webhook = new Webhook("endpoint-secret");

try {
    $valid = $webhook->verify($rawRequestBody, $_SERVER["HTTP_X_CONVOY_SIGNATURE"]);
} catch (\Convoy\Exceptions\WebhookVerificationException $e) {
    $valid = false;
}

if ($valid !== true) {
    http_response_code(400);
    exit;
}

// signature is valid; process the event