buyer

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: MIT Imports: 18 Imported by: 0

README

= x402-buyer

image:https://pkg.go.dev/badge/github.com/selesy/x402-buyer.svg[link=https://pkg.go.dev/github.com/selesy/x402-buyer]
image:https://img.shields.io/github/actions/workflow/status/selesy/x402-buyer/pre-commit.yaml[GitHub Actions Workflow Status]
image:https://goreportcard.com/badge/github.com/selesy/x402-buyer[link=https://goreportcard.com/report/github.com/selesy/x402-buyer]
image:https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square[link=https://github.com/RichardLitt/standard-readme]
image:https://img.shields.io/github/license/selesy/x402-buyer[GitHub License]
image:https://img.shields.io/github/v/release/selesy/x402-buyer[GitHub Release]

Package `buyer` produces [http.Client](https://pkg.go.dev/net/http#Client)'s that can make [x402](https://x402.org) payments for HTTP content and services.

== Install

Include this library in your project using the following command:

[source,bash]
----
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.

[source,go]
----
include::example/private-key/main.go[]
----

Full documentation for this library is available as https://pkg.go.dev/github.com/selesy/x402-buyer[Go docs].

== Contributing

* Please report issues using https://github.com/selesy/x402-buyer/issues[GitHub Issues].
* PRs are happily considered when submitted to https://github.com/selesy/x402-buyer/pulls[GitHub Pull requests].
* Other questions or discussions can be submitted to https://github.com/selesy/x402-buyer/discussions[GitHub Discussions].

=== 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.

[source]
----
include::.tool-versions[]
----

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 https://github.com/selesy/x402-buyer/blob/main/LICENSE[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.3.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