1. Go to this page and download the library: Download johnguoy/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/ */
// routes/api.php
use Illuminate\Support\Facades\Route;
// Basic: 1 cent per request, defaults from config
Route::get('/api/data', fn () => response()->json(['result' => 'secret']))
->middleware('x402:0.01');
// Custom price with explicit asset
Route::get('/api/premium', [ReportController::class, 'daily'])
->middleware('x402:0.10,USDC');
// Specify price, asset, and a non-default facilitator
Route::get('/api/pro', [ProController::class, 'data'])
->middleware('x402:1.00,USDC,coinbase');
// Group: same price for a whole section of your API
Route::middleware('x402:0.05')->group(function () {
Route::get('/api/report/daily', [ReportController::class, 'daily']);
Route::get('/api/report/weekly', [ReportController::class, 'weekly']);
});
use JohnGuoy\LaravelX402\Facades\X402;
// Resolve a facilitator and call it manually
$facilitator = X402::facilitator('coinbase');
$result = $facilitator->verify($paymentPayload, $