Freeform Freeform for Craft

Developer

Custom Webhook Types Pro

You can create your own Webhook types and add them to Freeform.

Setup Guide Revised in 5.0+

The creation of a custom Webhook integration in Freeform will require using a custom Craft module. This guide assumes you already have that knowledge. If not, please check out the guide we have.

User Guide:

View the guide on how to build a custom module.

To add a custom webhook integration, you will need to create a new class which must implement the \Solspace\Freeform\Library\Integrations\Types\Webhooks\WebhookIntegrationInterface interface.

To make things easier for you, you can extend the \Solspace\Freeform\Library\Integrations\Types\Webhooks\WebhookIntegration class, which already has a URL property as well as being capable of storing and exposing various other base properties, such as id, enabled, handle, name, etc.

Then you would implement the ::trigger(Form $form) method to handle posting your webhooks.

For the sake of this example let's assume you just want your webhook to post the form ID, name and submission ID to an endpoint at https://my.test.com. Here is what that class would look like.

<?php

namespace Modules\Webhooks;

use GuzzleHttp\Client;
use Solspace\Freeform\Attributes\Integration\Type;
use Solspace\Freeform\Form\Form;
use Solspace\Freeform\Library\Integrations\Types\Webhooks\WebhookIntegration;

// To be able to register this integration type, you must define 
// some metadata about it. Like specifying the readme and icon paths.
// Readme uses Markdown markup.

#[Type(
    name: 'My Basic Webhook',
    type: Type::TYPE_WEBHOOKS,
    readme: __DIR__.'/README.md',
    iconPath: __DIR__.'/icon.svg',
)]
class BasicWebhook extends WebhookIntegration
{
    public function trigger(Form $form): void
    {
        $submission = $form->getSubmission();
        
        // Create the payload that we will send
        $json = [
            'form' => [
                'id' => $form->getId(),
                'name' => $form->getName(),
            ],
            'submission' => [
                'id' => $submission->id,
            ],
        ];

        // Create a new Guzzle Client instance
        $client = new Client();

        // Get the URL the user has configured in their settings
        $url = $this->getUrl();
        
        try {
            // Post payload to the endpoint
            $client->post($url, ['json' => $json]);
        } catch (\Exception $e) {
            $this->processException($e, self::LOG_CATEGORY);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

Now that the integration type has been created, we need to register it with Freeform to make it available when creating new integrations. We do this by listening to the \Solspace\Freeform\Services\Integrations\IntegrationsService::EVENT_REGISTER_INTEGRATION_TYPES event.

<?php

namespace Modules;

use yii\base\Module;
use yii\base\Event;
use Modules\Webhooks\BasicWebhook;
use Solspace\Freeform\Services\Integrations\IntegrationsService;
use Solspace\Freeform\Events\Integrations\RegisterIntegrationTypesEvent;

class MyModule extends Module
{
    public function init(): void
    {
        parent::init();
        
        Event::on(
            IntegrationsService::class,
            IntegrationsService::EVENT_REGISTER_INTEGRATION_TYPES,
            function (RegisterIntegrationTypesEvent $event) {
                $event->addType(BasicWebhook::class);
            }
        )
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Once this is done, you should be able to see your webhook type as an option when creating a new Freeform Webhook integration.

Custom Webhook Type

Finished!