Skip to content

Installation

Get started with OpenLibx402 Go packages in minutes.

Prerequisites

  • Go: 1.21 or later
  • Solana Wallet: For testing (testnet/devnet tokens recommended)
  • Git: For cloning the repository

Package Installation

Install the packages you need:

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

# Client for consuming 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

Option 2: Development Installation (for Contributors)

# Clone the repository
git clone https://github.com/openlibx402/openlibx402.git
cd openlibx402

# Navigate to a package
cd packages/go/openlibx402-core

# Install dependencies
go mod tidy

# Build and test
go build
go test ./...

Verifying Installation

Verify with Go

go list -m github.com/openlibx402/go/openlibx402-core

Output should show:

github.com/openlibx402/go/openlibx402-core v0.1.0

Verify with pkg.go.dev

Visit: https://pkg.go.dev/github.com/openlibx402/go/openlibx402-core

Import Statements

Once installed, you can import in your code:

1
2
3
4
5
6
package main

import (
    "github.com/openlibx402/go/openlibx402-core"
    "github.com/openlibx402/go/openlibx402-client"
)

Or with aliases:

1
2
3
4
import (
    nethttp "github.com/openlibx402/go/openlibx402-nethttp"
    echox402 "github.com/openlibx402/go/openlibx402-echo"
)

Environment Setup

For Server Development

Create a .env file:

# Your Solana wallet address
X402_PAYMENT_ADDRESS="YOUR_SOLANA_WALLET_ADDRESS"

# USDC token mint (devnet: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v)
X402_TOKEN_MINT="EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"

# Network (solana-devnet, solana-mainnet)
X402_NETWORK="solana-devnet"

# RPC endpoint
X402_RPC_URL="https://api.devnet.solana.com"

# Server port
PORT=8080

Load in your code:

1
2
3
4
import "os"

paymentAddress := os.Getenv("X402_PAYMENT_ADDRESS")
tokenMint := os.Getenv("X402_TOKEN_MINT")

For Client Development

1
2
3
4
5
# Your wallet's private key (base58 encoded)
export X402_PRIVATE_KEY="your-base58-private-key"

# Optional: RPC endpoint
export X402_RPC_URL="https://api.devnet.solana.com"

Getting Devnet Tokens

To test on Solana devnet:

  1. Create a Solana wallet:

    solana-keygen new
    

  2. Get devnet SOL:

    solana airdrop 2 $(solana address) --url devnet
    

  3. Get devnet USDC:

  4. Use spl-token
  5. Or visit a devnet faucet

Troubleshooting

Module not found error

go: github.com/openlibx402/go/openlibx402-core@latest:
reading go.sum: no matching version found

Solution: Clear module cache and retry:

go clean -modcache
go get github.com/openlibx402/go/openlibx402-core@latest

Version mismatch

different modules with the same import path

Solution: Ensure you're using the correct import path:

1
2
3
4
5
# Wrong (old path)
import "github.com/openlibx402/openlibx402/go/openlibx402-core"

# Correct (simplified path)
import "github.com/openlibx402/go/openlibx402-core"

Missing Go version

Solution: Update Go:

1
2
3
4
# macOS with Homebrew
brew upgrade go

# Or download from https://golang.org/dl/

Next Steps

After installation:

  1. Choose Your Path:
  2. Server Quick Start - Build a payment-required API
  3. Client Quick Start - Build a payment client

  4. Explore Examples:

  5. net/http Server Example
  6. Echo Server Example

  7. Learn More:

  8. Core Library
  9. Client Library

Support