Kevin Eaton

Writing · Laravel & PHP

Taming CRM and ad-platform payloads

A webhook from a CRM or an ad platform is a deep array of strings that you do not control. Most codebases describe its shape a dozen times without ever writing it down. Here is how I write it down once.

Marketing software spends much of its life reading other people’s JSON. A lead form posts a webhook. A CRM sends a contact export. An email provider calls back about a bounce. Each one arrives as a deep array where the email is three levels down, every number is a string, and the timestamp is in whichever format that vendor likes.

The usual way to deal with it looks like this, and I have written plenty of it:

$email  = $payload['contact']['fields']['email'] ?? null;
$budget = (int) ($payload['contact']['fields']['monthly_budget'] ?? 0);

Each line is fine. The trouble is how many there are and where they live. The controller reads the email one way. A queued job reads it again. A listener reads the budget and casts it. A report reads the timestamp and parses it with a different function. The shape of that vendor’s payload has been described a dozen times across the codebase, each time implicitly, and never in one place you could point to.

Then the vendor renames a field. You find out which of the dozen places you missed when one of them starts writing nulls.

Write the shape down once

The fix is not clever. Declare, in one class, where each field lives and how to read it.

use Keaton\DataDefinition\DataDefinition;

class Lead extends DataDefinition
{
    protected array $aliases = [
        'email'  => 'contact.fields.email',
        'budget' => 'contact.fields.monthly_budget',
        'source' => 'campaign.utm_source',
        'at'     => 'created_time',
    ];

    protected array $casts = [
        'budget' => 'int',
        'at'     => 'datetime',
    ];
}

Then read the raw webhook body as if it had been typed all along:

$lead = Lead::make([
    'created_time' => '2026-03-02T09:15:00Z',
    'campaign' => ['utm_source' => 'google'],
    'contact' => ['fields' => [
        'email' => 'ana@example.com',
        'monthly_budget' => '2500',
    ]],
]);

echo $lead->email, ' via ', $lead->source, "\n";
echo $lead->at->format('j M Y'), "\n";
var_dump($lead->budget);
ana@example.com via google
2 Mar 2026
int(2500)

When the vendor renames a field, one line changes. A new developer who asks what this vendor sends can read the class.

Failing loudly

Look again at the hand-written version: (int) ($payload[...] ?? 0). If the vendor sends "n/a" for the budget, that line gives you 0. No error. A lead with an unknown budget is now a lead with a budget of zero, and it will sit in a report as a real number.

In this package a value that a cast cannot read throws, and the exception names the field. A monthly_budget of "n/a" is an error at the door. You decide what to do with it there, with the payload in front of you, and it does not quietly become a zero three systems downstream.

The same goes for statuses. Cast a vendor’s status string to a backed enum, and a value that is not one of your cases throws. The day the vendor adds a status, you hear about it the first time it appears.

Read-only on purpose

The object is immutable. Assigning to it throws, and with() hands back a changed copy.

That is a deliberate limit. This is a payload you received. It is evidence of what the vendor sent. If code somewhere in the request can modify it in place, then by the time you log it or store it, it may no longer be what arrived. When there is an argument later about what the vendor sent, you want the original, untouched.

What it is not

It does not validate and it does not transform. If the data is yours to define, or you need validation rules, use spatie/laravel-data, which is very good at that and does far more.

If you only read a few values, Laravel’s own Fluent is enough. It reads dot-paths and, from Laravel 11.34, has typed getters such as integer() and date(). It is writable and has no aliases, and its integer() gives 0 for "n/a" where this package throws.

Data Definition is for the narrower case: a payload you receive and do not control, that you want to read by short typed names and change nothing.

Status

It is a small package with no service provider, config file or migration. It needs PHP 8.2 or newer and the Laravel components from 11.33.2 up. It is open source, and it installs with composer require keaton-php/laravel-data-definition. The package page has the details.

NextKeeping lead lists clean