API Reference¶
Complete API reference for all X402 Go packages.
Core Package (openlibx402-core)¶
PaymentRequest¶
Represents a 402 payment request from a server.
Structure¶
Methods¶
| Method | Signature | Returns | Description |
|---|---|---|---|
IsExpired() |
IsExpired() bool |
bool |
Check if payment request has expired |
ToJSON() |
ToJSON() (string, error) |
(string, error) |
Convert to JSON string |
Functions¶
| Function | Signature | Returns | Description |
|---|---|---|---|
PaymentRequestFromJSON() |
PaymentRequestFromJSON(jsonStr string) (*PaymentRequest, error) |
(*PaymentRequest, error) |
Parse from JSON string |
PaymentAuthorization¶
Represents a signed payment authorization for retry requests.
Structure¶
Methods¶
| Method | Signature | Returns | Description |
|---|---|---|---|
ToHeaderValue() |
ToHeaderValue() (string, error) |
(string, error) |
Encode as base64 JSON for HTTP header |
ToJSON() |
ToJSON() (string, error) |
(string, error) |
Convert to JSON string |
Functions¶
| Function | Signature | Returns | Description |
|---|---|---|---|
PaymentAuthorizationFromHeader() |
PaymentAuthorizationFromHeader(headerValue string) (*PaymentAuthorization, error) |
(*PaymentAuthorization, error) |
Parse from HTTP header |
SolanaPaymentProcessor¶
Handles Solana blockchain operations.
Constructor¶
Methods¶
| Method | Signature | Returns | Description |
|---|---|---|---|
CreatePaymentTransaction() |
CreatePaymentTransaction(ctx context.Context, paymentReq *PaymentRequest, amount string, keypair solana.PrivateKey) (*solana.Transaction, error) |
(*solana.Transaction, error) |
Create payment transaction |
SignAndSendTransaction() |
SignAndSendTransaction(ctx context.Context, tx *solana.Transaction, keypair solana.PrivateKey) (string, error) |
(string, error) |
Sign and broadcast transaction |
VerifyTransaction() |
VerifyTransaction(ctx context.Context, txHash string, recipient string, amount string, tokenMint string) (bool, error) |
(bool, error) |
Verify transaction on-chain |
GetTokenBalance() |
GetTokenBalance(ctx context.Context, walletAddress string, tokenMint string) (float64, error) |
(float64, error) |
Get SPL token balance |
Close() |
Close() |
- | Close processor and cleanup resources |
Functions¶
| Function | Signature | Returns | Description |
|---|---|---|---|
GetDefaultRPCURL() |
GetDefaultRPCURL(network string) string |
string |
Get default RPC URL for network |
Error Types¶
X402Error¶
Base error type.
PaymentRequiredError¶
Raised when 402 response is received.
PaymentExpiredError¶
Payment request has expired.
InsufficientFundsError¶
Wallet lacks sufficient funds.
PaymentVerificationError¶
Payment verification failed.
TransactionBroadcastError¶
Failed to broadcast transaction.
InvalidPaymentRequestError¶
Invalid payment request format.
Client Package (openlibx402-client)¶
X402Client (Explicit Mode)¶
Manual payment control HTTP client.
Constructor¶
Methods¶
| Method | Signature | Returns | Description |
|---|---|---|---|
Get() |
Get(ctx context.Context, url string, auth *core.PaymentAuthorization) (*http.Response, error) |
(*http.Response, error) |
Execute GET request |
Post() |
Post(ctx context.Context, url string, body []byte, auth *core.PaymentAuthorization) (*http.Response, error) |
(*http.Response, error) |
Execute POST request |
Put() |
Put(ctx context.Context, url string, body []byte, auth *core.PaymentAuthorization) (*http.Response, error) |
(*http.Response, error) |
Execute PUT request |
Delete() |
Delete(ctx context.Context, url string, auth *core.PaymentAuthorization) (*http.Response, error) |
(*http.Response, error) |
Execute DELETE request |
Do() |
Do(ctx context.Context, req *http.Request, auth *core.PaymentAuthorization) (*http.Response, error) |
(*http.Response, error) |
Execute any HTTP request |
PaymentRequired() |
PaymentRequired(resp *http.Response) bool |
bool |
Check if response is 402 Payment Required |
ParsePaymentRequest() |
ParsePaymentRequest(resp *http.Response) (*core.PaymentRequest, error) |
(*core.PaymentRequest, error) |
Parse PaymentRequest from 402 response |
CreatePayment() |
CreatePayment(ctx context.Context, paymentReq *core.PaymentRequest, customAmount string) (*core.PaymentAuthorization, error) |
(*core.PaymentAuthorization, error) |
Create and broadcast payment |
Close() |
Close() |
- | Close client and cleanup |
X402AutoClient (Automatic Mode)¶
Automatic payment handling HTTP client.
Constructor¶
Options¶
Methods¶
| Method | Signature | Returns | Description |
|---|---|---|---|
Get() |
Get(ctx context.Context, url string) (*http.Response, error) |
(*http.Response, error) |
GET (auto-pays if needed) |
Post() |
Post(ctx context.Context, url string, body []byte) (*http.Response, error) |
(*http.Response, error) |
POST (auto-pays if needed) |
Put() |
Put(ctx context.Context, url string, body []byte) (*http.Response, error) |
(*http.Response, error) |
PUT (auto-pays if needed) |
Delete() |
Delete(ctx context.Context, url string) (*http.Response, error) |
(*http.Response, error) |
DELETE (auto-pays if needed) |
Close() |
Close() |
- | Close client and cleanup |
net/http Middleware (openlibx402-nethttp)¶
Initialization¶
Config¶
Middleware¶
PaymentRequiredOptions¶
Helper Functions¶
Echo Middleware (openlibx402-echo)¶
Initialization¶
Config¶
Middleware¶
PaymentRequiredOptions¶
Helper Functions¶
HTTP Headers¶
Request Headers¶
X-Payment-Authorization¶
Payment authorization header included in requests after 402 response.
Format:
Response Headers¶
402 Payment Required¶
Status code: 402
Response body (JSON):
Error Codes¶
Payment Required (402)¶
Payment Expired¶
Insufficient Funds¶
Payment Verification Failed¶
Transaction Broadcast Failed¶
Invalid Payment Request¶
Constants and Defaults¶
Solana Networks¶
Default RPC Endpoints¶
| Network | URL |
|---|---|
| devnet | https://api.devnet.solana.com |
| testnet | https://api.testnet.solana.com |
| mainnet | https://api.mainnet-beta.solana.com |
Default Values¶
Type Aliases¶
Common Patterns¶
Create Client¶
Explicit Mode:
Automatic Mode:
Initialize Middleware¶
net/http:
Echo:
Protect Endpoint¶
net/http:
Echo:
Access Payment Info¶
net/http:
Echo: