Skip to main content

Create a webhook endpoint function

Set up an HTTPS endpoint function that can accept webhook requests with a POST method.

Set up your endpoint function so that it:

  1. Handles POST requests with a JSON payload consisting of an event object.

  2. Quickly returns a successful status code (2xx) prior to any complex logic that could cause a timeout. For example, you must return a 200 response before sending a Slack notification.

Example endpoint

This code snippet is a webhook function configured to check that the event type was received, to handle the event, and return a 200 response.

<?php
// The following example uses PHP to verify a webhook request:

// The freispace webhook secret, viewable from the Webhook details page. In a production environment, set the secret as an environment variable to prevent exposing it in code.
define('FREISPACE_SECRET', 'webhook_secret_key');

// Get the raw POST data
$data = file_get_contents('php://input');

// Get the HMAC signature from the request header
$signature_header = $_SERVER['HTTP_X_FREISPACE_SIGNATURE'];

// Return if the signature header is missing
if (!$signature_header) {
http_response_code(400);
echo json_encode(['message' => 'Missing signature']);
exit();
}

// Compute the HMAC signature of the request payload
$computed_signature = hash_hmac('sha256', $data, FREISPACE_SECRET);

// Verify the computed signature with the signature in the request header
if (!hash_equals($computed_signature, $signature_header)) {
http_response_code(403);
echo json_encode(['message' => 'Invalid signature']);
exit();
}

http_response_code(200);
echo json_encode(['message' => 'Webhook processed successfully']);

// Process the webhook payload
$payload = json_decode($data, true);
?>