laravelLaravel / PHP Integration

Protect a Laravel (or plain PHP) application with Guardian in three short steps.

  1. Load the Guardian agent on your frontend and get a requestId.

  2. Send that requestId to your backend.

  3. From PHP, fetch the processed event by id and decide what to do.

1. Frontend

Option A: JavaScript / TypeScript

If your frontend uses a bundler (Vite, webpack, Mix, etc.), install the SDK and call it where it matters.

npm install @guardianstack/guardian-js
// frontend/guardian.ts
import { loadAgent } from '@guardianstack/guardian-js';

// 1) Initialize once at app startup
const guardian = await loadAgent({
  siteKey: 'site_XXXXXXXX',
});

// 2) Trigger an identification exactly where it matters (login, signup, checkout)
const res = await guardian.get();

// 3) Extract the requestId and send it to your backend for risk evaluation
const requestId = res?.requestId;

Option B: Blade or plain PHP (no bundler)

If you do not use a JavaScript bundler, load the @guardianstack/guardian-js package directly from jsDelivr. It serves the published npm build, no local install required.

Then, on the page where you want to protect an action:

You can pin to an exact version for reproducible builds:

2. Backend (Laravel / PHP)

Your backend receives the requestId and calls Guardian to fetch the processed event.

Guardian processes events asynchronously, so the first fetch can return 404 for a moment. Retry a few times with a short delay until the event is ready.

Add your secret to .env:

Fetch the event with Laravel's HTTP client:

Use it in a controller:

Which signals should you gate on?

The example above blocks on bot, VPN and tampering, but the right combination of signals depends on what you are actually protecting. We publish a dedicated guide for each common use case, with recommended thresholds and decision logic:

Pick the guide closest to your use case and adapt the PHP checks above to match its recommendations.

Plain PHP (no Laravel)

If you are not using Laravel, the same call with cURL:

That's it

  • Frontend gets a requestId from guardian.get().

  • Backend fetches the event with Authorization: Bearer <secret>, retrying on 404.

  • Check the detection fields (botDetection, vpn, tampering, etc.) and decide.

Your secret stays on the server. The site key is safe on the frontend.

Last updated

Was this helpful?