1. Go to this page and download the library: Download sandermuller/laravel-x402 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sandermuller / laravel-x402 example snippets
use X402\Laravel\Http\Middleware\RequirePaymentFromBots;
// Free for humans, 0.001 USDC for AI agents.
Route::get('/articles/{article}', ArticleController::class)
->middleware(RequirePaymentFromBots::using('0.001'));
use X402\Laravel\Http\Middleware\RequirePayment;
Route::get('/premium', PremiumController::class)
->middleware(RequirePayment::using('0.01')); // 0.01 USDC on Base
use X402\Laravel\Contracts\Priceable;
class Article extends Model implements Priceable
{
public function x402Price(): string
{
return $this->premium ? '0.10' : '0.01';
}
}
Route::get('/articles/{article}', ArticleController::class)
->middleware(RequirePayment::using('0.01')); // base price
use X402\Laravel\Http\Middleware\RequirePaymentFromBots;
Route::get('/articles/{article}', ArticleController::class)
->middleware(RequirePaymentFromBots::using('0.001'));
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use X402\Laravel\Facades\X402;
X402::enforceWhen(fn (Request $r) =>
! Cache::has("x402:paid:{$r->ip()}:{$r->path()}")
);
// config/x402.php
'history' => [
'enabled' => env('X402_HISTORY', true),
'queue' => env('X402_HISTORY_QUEUE', null), // null = sync; set a queue name to defer
'connection' => null, // separate analytics DB if you want one
'table' => 'x402_payments',
],
use X402\Laravel\Models\Payment;
Payment::settled()->where('payer', '0xalice')->latest()->take(10)->get();
Payment::rejected()->whereBetween('created_at', [$from, $to])->count();
use Illuminate\Http\Request;
use X402\Laravel\Facades\X402;
X402::capturePaymentContext(fn (Request $r): array => [
'user_id' => $r->user()?->id,
'tenant_id' => $r->user()?->tenant_id,
'request_id' => $r->headers->get('X-Request-Id'),
]);
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use X402\Laravel\Facades\X402;
X402::resourceFormatter(function (string $url): string {
try {
return Route::getRoutes()->match(Request::create($url))->getName() ?? $url;
} catch (NotFoundHttpException) {
return $url;
}
});