1. Go to this page and download the library: Download sandermuller/php-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 / php-x402 example snippets
use X402\Facilitator\CoinbaseFacilitator;
use X402\Protocol\PaymentRequired;
use X402\Schemes\Evm\ExactScheme;
use X402\Server\PaymentEnforcer;
use X402\Server\StaticPriceTable;
$priceTable = new StaticPriceTable();
$priceTable->set('/premium', new PaymentRequired(
scheme: 'exact',
network: 'eip155:8453', // Base mainnet
amount: '10000', // 0.01 USDC (6 decimals)
asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
payTo: '0xYourReceivingAddress',
));
$middleware = new PaymentEnforcer(
priceTable: $priceTable,
facilitator: CoinbaseFacilitator::default($psr18Client, $psr17),
nonceStore: $atomicNonceStore, // see Replay protection
schemes: ['exact' => new ExactScheme()],
responseFactory: $psr17,
streamFactory: $psr17,
);
use X402\Client\PayingClient;
use X402\Client\PrivateKeyWallet;
$client = new PayingClient(
inner: $psr18Client, // Guzzle, Symfony HttpClient, etc.
wallet: new PrivateKeyWallet($operatorPk), // or an AwsKmsWallet — see Wallets row in Surface
);
$response = $client->sendRequest($request); // 402 → sign → retry → 200
use Psr\Http\Message\ServerRequestInterface;
use X402\Server\PaymentEnforcer;
$middleware = new PaymentEnforcer(
// ... priceTable, facilitator, nonceStore, schemes, factories ...
shouldEnforce: fn (ServerRequestInterface $request): bool
=> str_starts_with($request->getUri()->getPath(), '/api/paid/'),
);
use X402\Server\BotDetector;
$detector = new BotDetector(extra: ['MyCustomCrawler']);
$middleware = new PaymentEnforcer(
// ...
shouldEnforce: static fn (ServerRequestInterface $request): bool
=> $detector->isBot($request->getHeaderLine('User-Agent')),
);
use X402\Server\PaymentResponseCache;
use X402\Server\PaymentResponseCacheOptions;
// Pipeline: PaymentResponseCache → PaymentEnforcer → handler
$pipeline = [
new PaymentResponseCache(
$psr16Cache, $psr17, $psr17,
schemes: ['exact' => new ExactScheme()],
options: new PaymentResponseCacheOptions(ttl: 3600),
),
$enforcer,
];
use X402\Facilitator\DispatchingFacilitator;
use X402\Facilitator\PaymentOutcome;
use X402\Facilitator\PaymentOutcomeKind;
use X402\PaymentHistory\PaymentRowBuilder;
$facilitator = new DispatchingFacilitator(
inner: CoinbaseFacilitator::default($psr18Client, $psr17),
onOutcome: function (PaymentOutcome $outcome, array $context) use ($db): void {
$db->insert('x402_payments', PaymentRowBuilder::fromOutcome($outcome, $context));
},
captureContext: fn (): array => ['user_id' => $request->getAttribute('user')?->id],
);
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.