Skip to content

API reference

TonyD365-Agent edited this page Nov 21, 2026 · 5 revisions

API reference (1.x)

Everything below is stable within 1.x. Names starting with _ are private.

lanpair: high level (1.1+)

Host(name, *, app="lanpair", port=0, state_dir=None, identity=None, announce=True, bind="0.0.0.0")

Shows pairing offers; many devices pair with it. See Host and Device.

Member
.start() / .close() / with Host(...) as host: Start receiving and announcing; stop
.pairing_offer(*, ttl=300, max_attempts=5, address=None) -> PairingOffer New one-time offer; cancels the previous one
.wait_for_pairing(timeout=None) -> PeerInfo | None The next device that pairs
.devices -> list[PeerInfo] Paired devices
.send(device_id, body) To one device, up to 1 MiB (TCP above ~6 KB, 1.2+); LookupError if not paired, ValueError if too large, OSError if a large message can't be delivered
.receive(timeout=None) -> Message | None / .messages() Verified messages
.unpair(device_id) Forget a device
.device_id, .port, .dropped Own id, port (UDP and TCP), refused messages so far

Device(name, ...) (same options)

Member
.pair(offer: str | PairingOffer, *, timeout=10) -> PeerInfo Pair with a scanned offer; PairingError if refused
.host -> PeerInfo | None The paired host
.send(body) To the host
.receive(), .messages(), .unpair(), .start(), .close() As for Host

Message(sender: PeerInfo, body) and PeerInfo(device_id, name, identity, address)

PairingOffer

Member
.to_uri() -> str / PairingOffer.from_uri(text) The lanpair://pair?... text in the QR code
.qr() / .print_qr() QR code object (segno) / print it in the terminal; needs lanpair[qr]
.host, .name, .address, .expires, .app, .expired()

PairingError: an invalid, expired or used offer, or a refused pairing. lanpair.pairing.PairingSession and make_request let you pair over your own transport.

lanpair: building blocks

Identity

This device's keys. The private keys never leave the process; repr() shows only the device id.

Member
Identity.generate() New random identity
Identity.from_seed(seed: bytes) Restore from a 32-byte seed
.seed() -> bytes The 32-byte seed, for a key store
.public -> PublicIdentity The public half
.sign(data) -> bytes Ed25519 signature
.exchange(peer_box_key) -> bytes X25519 shared secret (used internally)

PublicIdentity(key, box_key)

Another device's public keys. Frozen, hashable, safe to share and store.

Member
.device_id -> str 16 hex characters derived from the signing key
.to_bytes() -> bytes 64 bytes, for QR codes and storage
PublicIdentity.from_bytes(raw) Inverse of to_bytes; ValueError if not 64 bytes
.verify(data, signature) -> bool Ed25519 verification

Sealer(identity, clock=time.time)

.seal(recipient: PublicIdentity, body, *, box_key=None) -> bytes: encrypts and signs body (any JSON value) for recipient. Thread-safe; one sealer serves every recipient. With box_key= (1.3+), the body is encrypted to that X25519 public key, the recipient's current session key, instead of its long-term key: see Forward secrecy in How it works.

Opener(me, peers={}, clock=time.time, max_skew=30.0, max_size=1 MiB, store=MemorySeqStore())

Verifies and decrypts messages for me: Identity.

Member
.add_peer(peer: PublicIdentity) Trust messages from peer
.remove_peer(device_id) Stop trusting it and forget its sequence number
.open(raw: bytes) -> (PublicIdentity, body) Or EnvelopeError; see How it works
.open_detailed(raw) -> Opened(sender, body, session_key) 1.3+. Also says which session key decrypted it (None: the long-term key)
.add_session_key(key: X25519PrivateKey) -> bytes 1.3+. Also accept messages encrypted to this key; returns its public key. Keep it in memory only
.remove_session_key(public) 1.3+. Forget it: messages sealed to it can no longer be opened

EnvelopeError

The only exception Opener.open raises for bad input. str(e) is one of the reasons in How it works.

Sequence stores

Class
MemorySeqStore() Default. Forgets on restart; Opener then refuses 30 s of old messages
JsonFileSeqStore(path) Survives restarts; atomic writes; ValueError if the file is not a lanpair one
SeqStore Protocol for your own (persistent, get, set, forget)

Key store (needs lanpair[keyring])

Function
load_or_create_identity(name="default") Stored identity, or a new one stored first
lanpair.keystore.load_identity(name) Stored identity or None
lanpair.keystore.save_identity(identity, name) Store, replacing
lanpair.keystore.delete_identity(name) Remove (no error if missing)
KeyStoreError Missing keyring, locked or unreachable store, or a corrupt entry

All take service="lanpair" as a keyword to use another key store service name.

lanpair.discovery (needs lanpair[discovery])

Member
Announcer(identity: PublicIdentity, name, port, *, addresses=None, app="lanpair") Announces until .close(); also a context manager
browse(seconds=2.0, *, app="lanpair") -> list[FoundDevice] Everything that answers
find(device_id, seconds=2.0, *, app="lanpair") -> FoundDevice | None One device by id
FoundDevice device_id, name, addresses, port, .address (host:port)
service_type(app) -> str _<app>._tcp.local.; app is 1-15 lowercase letters, digits, inner hyphens
DiscoveryError zeroconf missing, or announcing failed

Use your own app name so devices of different applications don't see each other.

Clone this wiki locally