Skip to content

radiustechsystems/x402-r2-gateway

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

x402 R2 Gateway

A Cloudflare Worker that implements payment-gated access to R2 resources using the x402 protocol.

Features

  • Payment verification using USDC before granting access to R2 resources
  • Configurable pricing (default: $0.10 USDC)
  • Support for Base L2 (default) and Radius testnet networks
  • Flexible path protection (default: all files)
  • Simple drop-in solution for protecting R2 buckets

Setup

  1. Install dependencies:
npm install
  1. Copy the example wrangler.toml file:
cp wrangler.toml.example wrangler.toml
  1. Configure your R2 bucket in wrangler.toml:
[[r2_buckets]]
binding = "R2_BUCKET"
bucket_name = "your-actual-bucket-name"
  1. Set environment variables:
# Required: Payment address where users send USDC
wrangler secret put PAYMENT_ADDRESS

# Optional configuration (defaults shown)
wrangler secret put PRICE_USDC # Default: "0.10"
wrangler secret put NETWORK_RPC # Default: "https://mainnet.base.org"
wrangler secret put PROTECTED_PATHS # Default: "/*"

Configuration Options

Networks

  • Ethereum mainnet: https://mainnet.infura.io/v3/YOUR_KEY
  • Base L2 (default): https://mainnet.base.org
  • Radius testnet: https://rpc.testnet.radiustech.xyz
  • Polygon: https://polygon-rpc.com
  • Arbitrum: https://arb1.arbitrum.io/rpc

Protected Paths

  • /* - Protect all files (default)
  • /premium/* - Protect only files under /premium/
  • /file1.pdf,/folder/* - Protect specific files/folders (comma-separated)

Deployment

npm run deploy

How it Works

  1. User requests a protected resource from your R2 bucket
  2. Worker checks if payment is required for the requested path
  3. If payment required, returns 402 status with payment details
  4. User sends USDC payment to specified address
  5. On subsequent request with valid payment proof, access is granted
  6. File is served directly from R2 bucket

Payment Flow

The worker returns a 402 Payment Required response with an X-Accept-Payment header containing:

  • Amount in USDC
  • Payment address
  • Network RPC endpoint

Clients can use x402-compatible libraries to handle the payment automatically.

About

Cloudflare Worker that implements payment-gated access to R2 resources using x402

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published