1. Go to this page and download the library: Download solanaguide/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 that cost $0.001 USDC per request
Route::get('/api/weather', WeatherController::class)
->middleware('x402:0.001');
// With a description
Route::get('/api/premium', PremiumController::class)
->middleware('x402:0.01,Premium weather data');
use Symfony\Component\Routing\Annotation\Route;
#[Route('/api/weather')]
public function weather(): Response
{
// Set _x402_price on the request attributes
return $this->json(['temp' => 72]);
}
// app/Http/Kernel.php — apply to all web routes
protected $middlewareGroups = [
'web' => [
// ... existing middleware
\SolanaGuide\X402\Middleware\PaymentRequiredMiddleware::class,
],
];