Documentation
¶
Index ¶
- type Config
- type CrawlerDetector
- type FacilitatorClient
- type PaymentPayload
- type PaymentRecord
- type PaymentRequirements
- type PaymentRequirementsResponse
- type PaymentStore
- type SQLiteStore
- type SettleRequest
- type SettlementResponse
- type VerifyRequest
- type VerifyResponse
- type X402
- func (X402) CaddyModule() caddy.ModuleInfo
- func (x *X402) Cleanup() error
- func (x *X402) Provision(ctx caddy.Context) error
- func (x *X402) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error
- func (x *X402) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
- func (x *X402) Validate() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// PayTo is the EVM address that receives USDC payments.
PayTo string `json:"pay_to"`
// Price in USDC (human-readable, e.g. "0.01").
Price string `json:"price"`
// Network is the blockchain network identifier (e.g. "base", "base-sepolia").
Network string `json:"network"`
// FacilitatorURL is the base URL of the x402 facilitator service.
FacilitatorURL string `json:"facilitator_url,omitempty"`
// FacilitatorKey is the API key for the facilitator (optional, for authenticated facilitators).
// Redacted from JSON serialization via custom MarshalJSON on the parent struct.
FacilitatorKey string `json:"-"`
// ExemptPaths are path patterns that bypass the paywall.
ExemptPaths []string `json:"exempt_paths,omitempty"`
// CloudflareCompat enables crawler-price/crawler-charged header emission.
CloudflareCompat bool `json:"cloudflare_compat"`
// DBPath is the path to the SQLite audit trail database.
DBPath string `json:"db_path,omitempty"`
// DryRun when true skips actual settlement (useful for testing).
DryRun bool `json:"dry_run"`
// Description is a human-readable description of the protected resource.
Description string `json:"description,omitempty"`
// MaxTimeoutSeconds is the max time allowed for payment completion.
MaxTimeoutSeconds int `json:"max_timeout_seconds,omitempty"`
// CustomCrawlerPatterns are additional regex patterns to match crawler UAs.
CustomCrawlerPatterns []string `json:"custom_crawler_patterns,omitempty"`
}
Config holds all directive settings parsed from the Caddyfile.
type CrawlerDetector ¶
type CrawlerDetector struct {
// contains filtered or unexported fields
}
CrawlerDetector holds compiled regex patterns for matching AI crawler User-Agents.
func NewCrawlerDetector ¶
func NewCrawlerDetector(customPatterns []string) (*CrawlerDetector, error)
NewCrawlerDetector creates a detector with built-in patterns plus any custom ones.
func (*CrawlerDetector) IsCrawler ¶
func (d *CrawlerDetector) IsCrawler(userAgent string) bool
IsCrawler returns true if the User-Agent matches any known AI crawler pattern.
func (*CrawlerDetector) MatchedPattern ¶
func (d *CrawlerDetector) MatchedPattern(userAgent string) string
MatchedPattern returns the first matching pattern name, or empty string.
type FacilitatorClient ¶
type FacilitatorClient struct {
// contains filtered or unexported fields
}
FacilitatorClient handles HTTP communication with the x402 facilitator service.
func NewFacilitatorClient ¶
func NewFacilitatorClient(baseURL, apiKey string) *FacilitatorClient
NewFacilitatorClient creates a new facilitator HTTP client.
func (*FacilitatorClient) Settle ¶
func (c *FacilitatorClient) Settle(ctx context.Context, payload *PaymentPayload, reqs PaymentRequirements) (*SettlementResponse, error)
Settle calls the facilitator /settle endpoint.
func (*FacilitatorClient) Verify ¶
func (c *FacilitatorClient) Verify(ctx context.Context, payload *PaymentPayload, reqs PaymentRequirements) (*VerifyResponse, error)
Verify calls the facilitator /verify endpoint.
type PaymentPayload ¶
type PaymentPayload struct {
X402Version int `json:"x402Version"`
Scheme string `json:"scheme"`
Network string `json:"network"`
Payload json.RawMessage `json:"payload"`
}
PaymentPayload is the decoded X-PAYMENT header (base64 JSON).
type PaymentRecord ¶
type PaymentRecord struct {
ID int64 `json:"id"`
Timestamp time.Time `json:"timestamp"`
Path string `json:"path"`
UserAgent string `json:"user_agent"`
CrawlerName string `json:"crawler_name"`
Payer string `json:"payer"`
Amount string `json:"amount"`
Network string `json:"network"`
TxHash string `json:"tx_hash"`
Success bool `json:"success"`
ErrorReason string `json:"error_reason,omitempty"`
RemoteAddr string `json:"remote_addr"`
DryRun bool `json:"dry_run"`
}
PaymentRecord represents a single payment event in the audit trail.
type PaymentRequirements ¶
type PaymentRequirements struct {
Scheme string `json:"scheme"`
Network string `json:"network"`
MaxAmountRequired string `json:"maxAmountRequired"`
Asset string `json:"asset"`
PayTo string `json:"payTo"`
Resource string `json:"resource"`
Description string `json:"description"`
MimeType string `json:"mimeType,omitempty"`
OutputSchema interface{} `json:"outputSchema"`
MaxTimeoutSeconds int `json:"maxTimeoutSeconds"`
Extra map[string]interface{} `json:"extra"`
}
PaymentRequirements defines a single acceptable payment method.
type PaymentRequirementsResponse ¶
type PaymentRequirementsResponse struct {
X402Version int `json:"x402Version"`
Error string `json:"error"`
Accepts []PaymentRequirements `json:"accepts"`
}
PaymentRequirementsResponse is the 402 response body per x402 spec v1.
type PaymentStore ¶
type PaymentStore interface {
// Init sets up the store (create tables, etc.).
Init() error
// Record inserts a payment record.
Record(rec PaymentRecord) error
// Close shuts down the store.
Close() error
}
PaymentStore is the interface for persisting payment audit records.
type SQLiteStore ¶
type SQLiteStore struct {
// contains filtered or unexported fields
}
SQLiteStore implements PaymentStore using pure-Go SQLite.
func NewSQLiteStore ¶
func NewSQLiteStore(dbPath string) *SQLiteStore
NewSQLiteStore creates a new SQLite-backed payment store.
func (*SQLiteStore) Close ¶
func (s *SQLiteStore) Close() error
Close closes the database connection. Safe to call multiple times.
func (*SQLiteStore) Init ¶
func (s *SQLiteStore) Init() error
Init opens the database and creates the payments table if needed.
func (*SQLiteStore) Record ¶
func (s *SQLiteStore) Record(rec PaymentRecord) error
Record inserts a payment record into the database.
type SettleRequest ¶
type SettleRequest = VerifyRequest
SettleRequest is the request body for POST /settle (same structure as verify).
type SettlementResponse ¶
type SettlementResponse struct {
Success bool `json:"success"`
ErrorReason string `json:"errorReason,omitempty"`
Transaction string `json:"transaction"`
Network string `json:"network"`
Payer string `json:"payer"`
}
SettlementResponse is included in the X-PAYMENT-RESPONSE header.
type VerifyRequest ¶
type VerifyRequest struct {
PaymentPayload *PaymentPayload `json:"paymentPayload"`
PaymentRequirements PaymentRequirements `json:"paymentRequirements"`
}
VerifyRequest is the request body for POST /verify.
type VerifyResponse ¶
type VerifyResponse struct {
IsValid bool `json:"isValid"`
InvalidReason string `json:"invalidReason,omitempty"`
Payer string `json:"payer,omitempty"`
}
VerifyResponse is the response from POST /verify.
type X402 ¶
type X402 struct {
Config
// contains filtered or unexported fields
}
X402 is a Caddy HTTP middleware that implements the x402 payment protocol, requiring AI crawlers to pay USDC for content access.
func (X402) CaddyModule ¶
func (X402) CaddyModule() caddy.ModuleInfo
CaddyModule returns the Caddy module information.
func (*X402) UnmarshalCaddyfile ¶
UnmarshalCaddyfile implements caddyfile.Unmarshaler.
x402 {
pay_to 0xADDRESS
price 0.01
network base
facilitator_url https://x402.org/facilitator
facilitator_key {env.CDP_API_KEY}
exempt /robots.txt
exempt /.well-known/*
cloudflare_compat on
db_path /var/lib/caddy/x402-payments.db
dry_run off
description "My site content"
max_timeout 60
crawler_pattern MyBot
}