alchemy

package module
v0.0.0-...-32810c0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 6 Imported by: 0

README

Alchemy Go SDK

Unofficial Go SDK for Alchemy blockchain API.

Installation

go get github.com/ABT-Tech-Limited/alchemy-go

Quick Start

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/ABT-Tech-Limited/alchemy-go"
    "github.com/ABT-Tech-Limited/alchemy-go/data"
    "github.com/ABT-Tech-Limited/alchemy-go/types"
)

func main() {
    client, err := alchemy.New(alchemy.Config{
        APIKey:  os.Getenv("ALCHEMY_API_KEY"),
        Network: alchemy.EthMainnet,
    })
    if err != nil {
        log.Fatal(err)
    }

    ctx := context.Background()

    // Get current block number
    blockNum, _ := client.Node.BlockNumber(ctx)
    fmt.Printf("Block: %d\n", blockNum)

    // Get wallet balance
    addr := types.MustParseAddress("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
    balance, _ := client.Wallet.GetBalance(ctx, addr)
    fmt.Printf("Balance: %s ETH\n", balance.Formatted)

    // Get asset transfers
    params := data.NewAssetTransfersParams().
        SetToAddress(addr).
        SetCategories([]data.AssetTransferCategory{data.CategoryERC20})
    transfers, _ := client.Data.GetAssetTransfers(ctx, params)
    fmt.Printf("Transfers: %d\n", len(transfers.Transfers))
}

Supported Networks

Network Constant
Ethereum Mainnet alchemy.EthMainnet
Ethereum Sepolia alchemy.EthSepolia
Polygon Mainnet alchemy.PolygonMainnet
Arbitrum Mainnet alchemy.ArbitrumMainnet
Optimism Mainnet alchemy.OptimismMainnet
Base Mainnet alchemy.BaseMainnet

License

MIT

Documentation

Overview

Package alchemy provides a Golang SDK for the Alchemy API.

The SDK covers Node API (JSON-RPC), Data API (Transfers, NFTs, Tokens), and Wallet API for blockchain data access.

Example usage:

client, err := alchemy.New(alchemy.Config{
    APIKey:  os.Getenv("ALCHEMY_API_KEY"),
    Network: alchemy.EthMainnet,
})
if err != nil {
    log.Fatal(err)
}

// Get block number
blockNum, _ := client.Node.BlockNumber(ctx)

// Get asset transfers
transfers, _ := client.Data.GetAssetTransfers(ctx, &data.AssetTransfersParams{
    ToAddress: &address,
    Category:  []data.AssetTransferCategory{data.CategoryERC20},
})

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingAPIKey = &ConfigError{Message: "API key is required"}
)

Common configuration errors.

Functions

This section is empty.

Types

type Alchemy

type Alchemy struct {

	// Node provides access to JSON-RPC methods (eth_*, debug_*, etc.).
	Node *node.Client

	// Data provides access to enhanced data APIs (transfers, tokens, NFTs).
	Data *data.Client

	// Wallet provides high-level wallet operations.
	Wallet *wallet.Client
	// contains filtered or unexported fields
}

Alchemy is the main client for the Alchemy API.

func New

func New(cfg Config) (*Alchemy, error)

New creates a new Alchemy client with the given configuration.

func (*Alchemy) Config

func (a *Alchemy) Config() Config

Config returns a copy of the current configuration.

func (*Alchemy) Network

func (a *Alchemy) Network() Network

Network returns the current network.

func (*Alchemy) WithNetwork

func (a *Alchemy) WithNetwork(network Network) (*Alchemy, error)

WithNetwork creates a new Alchemy client for a different network. This returns a new client instance; the original client is not modified.

type Config

type Config struct {
	// APIKey is the Alchemy API key (required).
	APIKey string

	// Network is the blockchain network to connect to (default: EthMainnet).
	Network Network

	// BaseURL overrides the default API endpoint.
	// If empty, the endpoint is derived from Network.
	BaseURL string

	// Timeout is the request timeout (default: 30s).
	Timeout time.Duration

	// MaxRetries is the maximum number of retry attempts (default: 3).
	MaxRetries int

	// RetryDelay is the initial delay between retries (default: 1s).
	RetryDelay time.Duration

	// RetryMaxDelay is the maximum delay between retries (default: 30s).
	RetryMaxDelay time.Duration

	// HTTPClient is a custom HTTP client to use.
	// If nil, a default client is created.
	HTTPClient *http.Client

	// Debug enables debug logging.
	Debug bool
}

Config holds the configuration for the Alchemy client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with default values.

func (*Config) GetBaseURL

func (c *Config) GetBaseURL() string

GetBaseURL returns the base URL for API requests.

func (*Config) GetHTTPClient

func (c *Config) GetHTTPClient() *http.Client

GetHTTPClient returns the HTTP client to use.

func (*Config) Validate

func (c *Config) Validate() error

Validate validates the configuration and returns an error if invalid.

func (Config) WithDefaults

func (c Config) WithDefaults() Config

WithDefaults returns a copy of the config with default values applied for any zero-valued fields.

type ConfigError

type ConfigError struct {
	Message string
}

ConfigError represents a configuration error.

func (*ConfigError) Error

func (e *ConfigError) Error() string

type Network

type Network string

Network represents a blockchain network supported by Alchemy.

const (
	EthMainnet Network = "eth-mainnet"
	EthSepolia Network = "eth-sepolia"
	EthHolesky Network = "eth-holesky"
	EthHoodi   Network = "eth-hoodi"
)

Ethereum networks.

const (
	PolygonMainnet Network = "polygon-mainnet"
	PolygonAmoy    Network = "polygon-amoy"
)

Polygon networks.

const (
	ArbitrumMainnet Network = "arb-mainnet"
	ArbitrumSepolia Network = "arb-sepolia"
	ArbitrumNova    Network = "arbnova-mainnet"
)

Arbitrum networks.

const (
	OptimismMainnet Network = "opt-mainnet"
	OptimismSepolia Network = "opt-sepolia"
)

Optimism networks.

const (
	BaseMainnet Network = "base-mainnet"
	BaseSepolia Network = "base-sepolia"
)

Base networks.

const (
	ZkSyncMainnet Network = "zksync-mainnet"
	ZkSyncSepolia Network = "zksync-sepolia"
)

zkSync networks.

const (
	PolygonZkEvmMainnet Network = "polygonzkevm-mainnet"
	PolygonZkEvmCardona Network = "polygonzkevm-cardona"
)

Polygon zkEVM networks.

const (
	LineaMainnet Network = "linea-mainnet"
	LineaSepolia Network = "linea-sepolia"
)

Linea networks.

const (
	ScrollMainnet Network = "scroll-mainnet"
	ScrollSepolia Network = "scroll-sepolia"
)

Scroll networks.

const (
	BlastMainnet Network = "blast-mainnet"
	BlastSepolia Network = "blast-sepolia"
)

Blast networks.

const (
	AvalancheMainnet  Network = "avax-mainnet"
	AvalancheFuji     Network = "avax-fuji"
	BNBMainnet        Network = "bnb-mainnet"
	BNBTestnet        Network = "bnb-testnet"
	FantomMainnet     Network = "fantom-mainnet"
	FantomTestnet     Network = "fantom-testnet"
	GnosisMainnet     Network = "gnosis-mainnet"
	GnosisChiado      Network = "gnosis-chiado"
	CeloMainnet       Network = "celo-mainnet"
	CeloAlfajores     Network = "celo-alfajores"
	MantleMainnet     Network = "mantle-mainnet"
	MantleSepolia     Network = "mantle-sepolia"
	WorldChainMainnet Network = "worldchain-mainnet"
	WorldChainSepolia Network = "worldchain-sepolia"
	ZoraMainnet       Network = "zora-mainnet"
	ZoraSepolia       Network = "zora-sepolia"
	BerachainBartio   Network = "berachain-bartio"
	FlowMainnet       Network = "flow-mainnet"
	FlowTestnet       Network = "flow-testnet"
)

Other networks.

func AllNetworks

func AllNetworks() []Network

AllNetworks returns a list of all supported networks.

func MainnetNetworks

func MainnetNetworks() []Network

MainnetNetworks returns a list of all mainnet networks.

func (Network) BaseURL

func (n Network) BaseURL() string

BaseURL returns the base URL for the network's API endpoint.

func (Network) ChainID

func (n Network) ChainID() uint64

ChainID returns the chain ID for the network. Returns 0 if the network is unknown.

func (Network) IsEthereum

func (n Network) IsEthereum() bool

IsEthereum returns true if this is an Ethereum network.

func (Network) IsL2

func (n Network) IsL2() bool

IsL2 returns true if this is a Layer 2 network.

func (Network) IsMainnet

func (n Network) IsMainnet() bool

IsMainnet returns true if this is a mainnet network.

func (Network) IsTestnet

func (n Network) IsTestnet() bool

IsTestnet returns true if this is a testnet network.

func (Network) NFTURL

func (n Network) NFTURL() string

NFTURL returns the NFT API base URL for the network.

func (Network) NativeCurrency

func (n Network) NativeCurrency() string

NativeCurrency returns the native currency symbol for the network.

func (Network) String

func (n Network) String() string

String returns the network identifier string.

Directories

Path Synopsis
Package client provides HTTP and JSON-RPC client implementations for the Alchemy SDK.
Package client provides HTTP and JSON-RPC client implementations for the Alchemy SDK.
Package data provides the Data API client for Alchemy's enhanced APIs.
Package data provides the Data API client for Alchemy's enhanced APIs.
Package errors provides error types for the Alchemy SDK.
Package errors provides error types for the Alchemy SDK.
examples
basic command
Package main demonstrates basic usage of the Alchemy SDK.
Package main demonstrates basic usage of the Alchemy SDK.
nfts command
Package main demonstrates querying NFTs using the Alchemy SDK.
Package main demonstrates querying NFTs using the Alchemy SDK.
transfers command
Package main demonstrates querying asset transfers using the Alchemy SDK.
Package main demonstrates querying asset transfers using the Alchemy SDK.
internal
hex
Package hex provides utilities for encoding and decoding hexadecimal values commonly used in Ethereum JSON-RPC APIs.
Package hex provides utilities for encoding and decoding hexadecimal values commonly used in Ethereum JSON-RPC APIs.
Package node provides the Node API client for JSON-RPC methods.
Package node provides the Node API client for JSON-RPC methods.
Package types provides common types used across the Alchemy SDK.
Package types provides common types used across the Alchemy SDK.
Package wallet provides high-level wallet operations using the Alchemy API.
Package wallet provides high-level wallet operations using the Alchemy API.

Jump to

Keyboard shortcuts

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