Skip to main content
This guide shows how to make x402 payments from the buyer’s perspective. Use the SDK to handle payment flows, interact with paywalled resources, and process 402 Payment Required responses.

Prerequisites

  • Node.js and npm installed
  • A SKALE Chain endpoint
  • Understanding of x402 protocol
  • Familiarity with TypeScript
  • A wallet with funds (USDC or supported token)

Overview

When accessing a paywalled resource, the flow is:
  1. Make a request to the protected endpoint
  2. Receive a 402 Payment Required response with payment requirements
  3. Create and sign a payment authorization
  4. Retry the request with the payment signature header

Environment Setup

Create a .env file with the following variables:

Implementation

Step 1: Install Dependencies

Step 2: Define Your Chain

Create a chain configuration for SKALE:

Step 3: Create the Payment Client

Step 4: Access Paywalled Resources

Complete Working Example

Run the Client


Testing Your Client

Make sure you have a server running (see Accept Payments), then run your client:
The client will:
  1. Make a request to the protected endpoint
  2. Receive the 402 response with payment requirements
  3. Sign a payment authorization using your wallet
  4. Retry the request with the payment header
  5. Receive the premium content

Best Practices

  1. Use Environment Variables: Never hardcode private keys
  2. Handle Errors Gracefully: Always wrap payment logic in try-catch
  3. Check Balances: Ensure wallet has sufficient funds before making payments
  4. Log Transactions: Keep track of payments for debugging and accounting

Security Considerations

  • Never expose private keys in client-side code
  • Use secure key management (environment variables, secrets manager)
  • Validate payment amounts before approving
  • Consider implementing spending limits

Resources