Skip to content

Go Implementation

Welcome to the OpenLibx402 Go implementation documentation. This section covers all Go packages, libraries, middleware, and examples for building X402-enabled APIs and clients.

Overview

The Go implementation provides:

  • Core Library - X402 protocol implementation with models, errors, and Solana payment processing
  • Client Library - HTTP client with automatic and explicit payment handling
  • Middleware - Integration with popular Go web frameworks (net/http, Echo)
  • Examples - Complete working examples for server and client implementations

Quick Navigation

📚 Libraries

🔗 Middleware

🚀 Examples

📖 Getting Started

🔧 Reference

Key Features

One-Line Integration - Add payment requirements with minimal code 🤖 Automatic Payments - Clients automatically handle payment flows ⚡ Instant Settlement - ~200ms on Solana 💰 Micropayments - Support payments as low as $0.001 🔐 Secure - SSRF protection, payment verification, nonce-based replay protection 🌐 Framework Agnostic - Works with net/http, Echo, and custom implementations

Import Paths

// Core protocol
import "github.com/openlibx402/go/openlibx402-core"

// HTTP client
import "github.com/openlibx402/go/openlibx402-client"

// net/http middleware
import nethttp "github.com/openlibx402/go/openlibx402-nethttp"

// Echo middleware
import echox402 "github.com/openlibx402/go/openlibx402-echo"

Installation

# Core package (required)
go get github.com/openlibx402/go/openlibx402-core

# Client (for consuming paid APIs)
go get github.com/openlibx402/go/openlibx402-client

# Middleware for your framework
go get github.com/openlibx402/go/openlibx402-nethttp
# or
go get github.com/openlibx402/go/openlibx402-echo

The X402 Flow

┌─────────────┐         ┌──────────────┐         ┌────────────┐
│  AI Agent   │  ─1─→   │  API Server  │         │ Blockchain │
│   (Client)  │         │   (Server)   │         │  (Solana)  │
└─────────────┘         └──────────────┘         └────────────┘
     │                        │                        │
     │  GET /data             │                        │
     ├───────────────────────→│                        │
     │                        │                        │
     │  402 Payment Required  │                        │
     │  + Payment Details     │                        │
     │←───────────────────────┤                        │
     │                        │                        │
     │  Create & Broadcast    │                        │
     │  Payment Transaction   │                        │
     ├────────────────────────┼───────────────────────→│
     │                        │                        │
     │                        │   Verify Transaction   │
     │                        │←───────────────────────┤
     │                        │                        │
     │  GET /data             │                        │
     │  + Payment Auth Header │                        │
     ├───────────────────────→│                        │
     │                        │                        │
     │  200 OK + Data         │                        │
     │←───────────────────────┤                        │

Support

For issues, questions, or contributions: - GitHub Issues - GitHub Discussions - X402 Protocol

Next Steps

Start with the Installation guide and choose your path: