Skip to main content

electrum-client

Async Python client for the Electrum protocol (JSON-RPC 2.0 over TCP/SSL, newline-delimited), plus higher-level helpers for tracking onchain addresses, transactions, and blocks.

  • Request/response correlation by id, subscription dispatch, automatic keepalive pings
  • server.version handshake sent automatically on connect, as required by the spec
  • Response payloads parsed into typed pydantic models
  • Raw transaction / block header parsing (via embit)
  • Reconnecting trackers for address balances/history, single transactions, and new blocks

Install

pip install electrum-client

Usage

import asyncio
from electrum_client import ElectrumClient

async def main():
    async with ElectrumClient("ssl://electrum.blockstream.info:50002") as client:
        print(await client.get_height())
        print(await client.server_banner())

asyncio.run(main())

Scripthashes and balances

from electrum_client import ElectrumClient, scripthash_from_address

async with ElectrumClient("ssl://electrum.blockstream.info:50002") as client:
    scripthash = scripthash_from_address("bc1q...")
    balance = await client.get_balance(scripthash)
    history = await client.get_history(scripthash)
    utxos = await client.listunspent(scripthash)

Subscriptions

def on_change(params):
    print("scripthash status changed:", params)

async with ElectrumClient("ssl://electrum.blockstream.info:50002") as client:
    await client.subscribe_scripthash(scripthash, callback=on_change)
    await asyncio.sleep(60)  # keep the connection open to receive notifications

Trackers

AddressTracker, TransactionTracker, and BlockTracker wrap a reconnecting ElectrumClient connection and dispatch typed events (OnchainAddressEvent, OnchainTxEvent, BlockInfo) to an async callback — useful for driving websockets or other push-based consumers.

from electrum_client import AddressTracker

tracker = AddressTracker("ssl://electrum.blockstream.info:50002")
tracker.add("bc1q...")

async def on_event(event):
    print(event.address, event.confirmed, event.unconfirmed)

await tracker.run(callback=on_event, is_active=lambda: True)

Parsing helpers

from electrum_client import parse_raw_tx, parse_block_header

tx = parse_raw_tx(raw_tx_hex)
header = parse_block_header(header_hex, height)

Development

Requires uv.

uv sync
make format   # black + ruff --fix
make check    # black --check, ruff check, mypy
make test     # unit tests

Regtest integration tests

The regtest suite runs the client against bitcoind in Docker, indexed by two independent Electrum servers so the client is exercised against more than one implementation:

  • electrs (electrs-esplora) — fast, but doesn't implement server.features or blockchain.scripthash.get_mempool
  • fulcrum — fully spec-compliant, used to cover the protocol paths electrs can't (see tests/regtest/test_fulcrum.py)
make regtest-up
uv run pytest tests/regtest
make regtest-down

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

electrum_client-0.1.0.tar.gz (64.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

electrum_client-0.1.0-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file electrum_client-0.1.0.tar.gz.

File metadata

  • Download URL: electrum_client-0.1.0.tar.gz
  • Upload date:
  • Size: 64.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for electrum_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ccbe9540a323b1369ec2632d8a5fdcb4278536f44b09370b35048811b3355aba
MD5 71a0e4d8ffe105579b95e66ccb916b83
BLAKE2b-256 fe52c5d862ced8261b9d44d300531ece2772bc33aceb2215d26f50c9549f11ba

See more details on using hashes here.

File details

Details for the file electrum_client-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: electrum_client-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for electrum_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b4de30016915b4cb503835f7b89a0a00b7d22005827a8f84f87d8190924d36f8
MD5 490cfb1c5370a2d76e49fd0e9400b252
BLAKE2b-256 b75a913f050ff09adddf02ed1d74c222d94baf63cd6df5b5aa0eae674d1879f8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page