buyer

package module
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2025 License: MIT Imports: 18 Imported by: 0

README

x402-buyer

x402 buyer GitHub Actions Workflow Status x402 buyer GitHub License GitHub Release Conventional Commits

Package buyer produces http.Client's that can make x402 payments for HTTP content and services.

Install

Include this library in your project using the following command:

go get github.com/selesy/x402-buyer

Usage

Create an http.Client as shown below, then use it to make HTTP requests as usual. If an x402 payment is required, it will be made by the client and the response will be returned as usual.

package main

import (
    "io"
    "log/slog"
    "os"

    "github.com/lmittmann/tint"

    buyer "github.com/selesy/x402-buyer"
)

func main() {
    const (
        privateKeyEnvVar = "X402_BUYER_PRIVATE_KEY"
        url              = "https://x402.smoyer.dev/premium-joke"
    )

    log := slog.New(tint.NewHandler(os.Stderr, &tint.Options{
        Level: slog.LevelDebug,
    }))

    client, err := buyer.ClientForPrivateKeyHexFromEnv(privateKeyEnvVar, buyer.WithLogger(log))
    if err != nil {
        slog.Error("failed to create client", tint.Err(err))
        os.Exit(1)
    }

    resp, err := client.Get(url)
    if err != nil {
        log.Error("failed to make HTTP request", tint.Err(err))
        os.Exit(1)
    }

    defer func() {
        if err := resp.Body.Close(); err != nil {
            log.Error("failed to close response body", tint.Err(err))
        }
    }()

    body, err := io.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }

    log.Info("HTTP response", slog.String("body", string(body)), slog.Int("code", resp.StatusCode))

    for k, vs := range resp.Header {
        for _, v := range vs {
            log.Debug("HTTP response header", slog.String("key", k), slog.String("value", v))
        }
    }
}

Full documentation for this library is available as Go docs.

Contributing

Development

This project strives to maintain minimal external dependencies. If you have a feature that requires specific libraries, let’s discuss whether a new Go module should be created in a sub-directory.

The tools required to develop this project and to run the pre-commit checks are defined in the .tool-versions file.

asciidoctorj 3.0.0
golang 1.24.4
golangci-lint 2.4.0
pre-commit 4.2.0
pandoc 3.7.0.2
python 3.10.4

If you’re using asdf, simply run asdf install. Otherwise, install the listed tools in the manner required by your operating system. Once the required tools are installed, install the pre-commit hooks by running pre-commit install --install-hooks. Test your environment by running pre-commit run --all-files.

License

This project is distributed under the MIT License.

Documentation

Overview

Package buyer produces http.Client that can make x402 payments for HTTP content and services.

It is anticipated that this software will commonly be used to allow AI agents to pay for the services they need. When allowing automated payments on your behalf, care should be taken to limit your financial exposure.

Defaults

  • If the WithClient option is not specified, the http.DefaultClient is used with the http.DefaultTransport.
  • If the WithLogger Option is not specified, a No-Op logger is used.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientForKeyStore added in v0.2.0

func ClientForKeyStore(ks *keystore.KeyStore, acct accounts.Account, pass []byte, opts ...Option) (*http.Client, error)

ClientForKeyStore returns an http.Client capable of making payments using cryptocurrency from an Ethereum account in an Ethereum keystore.

func ClientForPrivateKey

func ClientForPrivateKey(priv *ecdsa.PrivateKey, opts ...Option) (*http.Client, error)

ClientForPrivateKey returns an http.Client capable of making payments using cryptocurrency from the Ethereum account associated with the provided ECDSA private key (which is expected to be using the Ethereum secp256k1 curve.)

func ClientForPrivateKeyHex

func ClientForPrivateKeyHex(privHex string, opts ...Option) (*http.Client, error)

ClientForPrivateKeyHex is like ClientForPrivateKey except that the private key is parsed from the provided hexadecimal string.

func ClientForPrivateKeyHexFromEnv

func ClientForPrivateKeyHexFromEnv(name string, opts ...Option) (*http.Client, error)

ClientForPrivateKeyHexFromEnv is like ClientForPrivateKeyHex except that hexadecimal string is read from the environment variable selected by name.

func ClientForSigner

func ClientForSigner(signer api.Signer, opts ...Option) (*http.Client, error)

ClientForSigner returns an http.Client capable of making x402 paybments using the provided api.Signer.

Types

type Option

type Option func(*config) error

Option represents a means of altering the default configuration of the buyer's http.RoundTripper.

func WithClient

func WithClient(client *http.Client) Option

WithClient is an Option that allows the user to provide a custom http.Client whose http.RoundTripper will be wrapped to allow x402 payments.

If not provided, http.DefaultClient will be used and internally, the http.DefaultTransport will be wrapped with the payment middleware. This option is ignored when provided as an argument to NewTransport.

func WithLogger

func WithLogger(log *slog.Logger) Option

WithLogger is an Option that allows the user to provide an slog.Logger that can be used to observe the internal operation of the buyer's http.RoundTripper.

If not provided, a No-Op logger is used. Under normal operation, this library writes one line of INFO-level logging for each payment that's made. Debug- level logging provides a log record for each step in the payment process.

type Transport

type Transport struct {
	// contains filtered or unexported fields
}

Transport is an http.RoundTripper that is capable of making x402 payments to access HTTP-based content or services on the Internet.

func NewTransport

func NewTransport(next http.RoundTripper, signer api.Signer, opts ...Option) (*Transport, error)

NewTransport creates an http.RoundTripper that is capable of making x402 payments using the provided api.Signer by wrapping the underlying http.Transport provided by the next argument.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

Directories

Path Synopsis
internal
pkg
api

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL