Sending Webhooks

Sending webhooks is simple using the Frontegg Events SDK.

import { EventsClient } from '@frontegg/client'

// Init the events SDK with the clientId and api key
const eventsClient = new EventsClient();
await eventsClient.init('YOUR-CLIENT-ID', 'YOUR-API-KEY');

eventsClient.trigger({
 // A unique event key for the event
  eventKey: 'event-key',
  
  /* Title and description are required properties 
     and will be sent in the request payload */
  properties: { 
    title: 'Welcome To Our App',
    description: 'This is our new app',
  },
  
  /* Add additional properties to the request payload
     or use webhook:true to send only default properties */
  channels: {
    webhook: { 
      someKey: 'some value', 
    },
  },
  
  // Trigger the event for a specific tenantId.
  tenantId: 'my-tenant-id',
});
require_once './vendor/autoload.php';
require_once './src/Frontegg/autoload.php';
use Frontegg\Events\Type\ChannelsConfig;
use Frontegg\Events\Type\DefaultProperties;
use Frontegg\Events\Type\TriggerOptions;
use Frontegg\Events\Type\WebHookBody;
use Frontegg\Frontegg;
use Psr\Http\Message\RequestInterface;

$clientId = 'YOUR_CLIENT_ID';
$apikey = 'YOUR_API_KEY';
$config = [
    'clientId' => $clientId,
    'clientSecret' => $apikey,
    'contextResolver' => function(RequestInterface $request) {
        return [
            'tenantId' => 'THE-TENANT-ID',
            'userId' => 'test-user-id',
            'permissions' => [],
        ];
    },
];

$frontegg = new Frontegg($config);
$triggerOptions = new TriggerOptions(
    'policy.changed',
    new DefaultProperties(
        title: 'Policy Changed',
        description: 'New policy was changed',
        [
            'name' => 'Policy 4',
            'id' => '11223456783245234',
            'priority' => '111',
        ]
    ),
    new ChannelsConfig(
        new WebHookBody()
    ),
    'YOUR_TENANT_ID'
);
$response = $frontegg->triggerEvent($triggerOptions);