x402 0.1.0 copy "x402: ^0.1.0" to clipboard
x402: ^0.1.0 copied to clipboard

Unified package for the x402 protocol with support for multiple blockchains.

example/main.dart

import 'dart:io';

import 'package:x402/x402.dart';

void main() async {
  final evmSigner = EvmSigner.fromHex(
    chainId: 84532, // Base Sepolia
    privateKeyHex: 'YOUR_EVM_PRIVATE_KEY',
  );

  final svmSigner = await SvmSigner.fromHex(
    privateKeyHex: 'YOUR_SVM_PRIVATE_KEY',
    network: SolanaNetwork.devnet,
  );

  final client = X402Client(
    signers: [evmSigner, svmSigner],
    onPaymentRequired: (requirement, resource, signer) async {
      final paymentCondition = int.parse(requirement.amount) <= 5000000;
      if (paymentCondition) {
        stdout.writeln('✅ Payment Approved for ${resource.url}');
        return true;
      } else {
        stdout.writeln('❌ Payment Denied for ${resource.url}: Amount too high');
        return false;
      }
    },
  );

  try {
    final response =
        await client.get(Uri.parse('https://api.example.com/premium-content'));

    if (response.statusCode == 200) {
      stdout.writeln('✅ Success! Received premium content:');
      stdout.writeln(response.body);
      // 402 is handled internally by the X402Client
    } else {
      throw Exception(
          'Request failed with status ${response.statusCode}: ${response.body}');
    }
  } catch (e) {
    stdout.writeln('❌ Error during request: $e');
  } finally {
    client.close();
  }
}
0
likes
150
points
107
downloads

Publisher

unverified uploader

Weekly Downloads

Unified package for the x402 protocol with support for multiple blockchains.

Topics

#x402 #payment #web3 #blockchain #http

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

x402_core, x402_evm, x402_svm

More

Packages that depend on x402