Skip to content

About

A lightweight pure-Python library for sending and receiving 1:1 direct messages on the Session messenger network

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

pysession-client

A lightweight pure-Python library for sending and receiving 1:1 direct messages on the Session messenger network, given your 13-word recovery phrase and a recipient's Session ID.

Session has no public API. This is what one would look like if it did — the protocol reimplemented from scratch (no libsession-util bindings, no Electron/Node) and exposed as plain Python calls. Verified end-to-end against the live production network.

from pysession_client import Client

client = Client("your thirteen word recovery phrase goes here")
client.send("05...recipient session id...", "Hello world!")

Installation

pip install pysession-client

Requires Python 3.10+. From a checkout: pip install -e .

Package Version Used for
pynacl >=1.5.0 libsodium bindings — Ed25519/X25519 keys, signing, sealed-box encryption
cryptography >=41.0.0 AES-256-GCM (onion transport), AES-256-CBC+HMAC (attachment encryption)
requests >=2.31.0 HTTP to seed nodes and onion-routed service nodes

That's the whole dependency list — the protobuf wire format is hand-encoded in proto_wire.py, so there's no protobuf package or protoc install to deal with.

Quick start

Initialize

Your Session ID is derived from the recovery phrase — there's no login, no server handshake, nothing to register.

from pysession_client import Client

client = Client("your thirteen word recovery phrase goes here")
print(client.session_id)   # 05... — your own Session ID

Send a message

recipient = "05...recipient session id..."

client.send(recipient, "Hello world!")

Anyone can send to anyone: no contact request or prior handshake is needed.

Receive messages

receive() fetches and decrypts everything waiting in your own swarm.

for msg in client.receive():
    if msg["type"] != "message":
        continue    # typing indicator, read receipt, or timer change
    print(msg["sender_session_id"], "says:", msg["body"])

Always check "type" before treating an entry as something a user typed. Pass a previous message's "hash" as receive(last_hash=...) to fetch only what arrived since — this library doesn't remember it for you (see below).

Send and receive files

with open("cat.png", "rb") as f:
    client.send_attachment(recipient, f.read(),
                            content_type="image/png", file_name="cat.png",
                            caption="look at this cat")

Attachments arrive as pointers; fetch the bytes with download_attachment():

for msg in client.receive():
    for attachment in msg["attachments"]:
        data = client.download_attachment(attachment)
        print(attachment["file_name"], len(data), "bytes")

Going further

Disappearing messages, typing indicators, read receipts, and voice notes are all one call each — see ADVANCED.md:

client.send(recipient, "self-destructing", expire_seconds=30, expire_after_read=True)
client.send_typing(recipient)
client.send_read_receipt(recipient, [msg["timestamp"]])

What's included

Feature Client method(s)
Recovery phrase → Session ID derivation Client(mnemonic), .session_id
Sending plain-text 1:1 direct messages .send()
Sending / receiving file attachments (incl. voice notes) .send_attachment(), .download_attachment()
Receiving & decrypting your own swarm, with paging .receive()
Disappearing messages (delete-after-send / delete-after-read) .send(), .send_attachment(), .set_disappearing_timer()
Typing indicators .send_typing()
Read receipts .send_read_receipt()
Full onion-routed swarm transport (bootstrap, swarm lookup, store, signed retrieve) used internally by all of the above
Automatic retries over fresh onion paths, cached swarm lookups used internally by all of the above
Offline self-test python -m pysession_client._selftest

What pysession-client will never do

These are not missing features and not a roadmap — they're the design. pysession-client is a transport library: it puts encrypted bytes into the network and takes them back out. Everything stateful belongs to the application you build on top, which is free to use whatever database, cache, or file format suits it.

Never Why it's yours, not ours
Conversation history Nothing is persisted between calls. receive() returns what the swarm currently holds; storing it is an application decision.
Seen-message tracking The library has no idea which messages you've already processed. You keep the last "hash" and pass it back as last_hash.
Local expiry enforcement Received messages report the sender's disappearing-message timer; nothing here runs a background deleter. Acting on the timer is your UI's job.
Any local storage at all No database, no cache directory, no config file. A Client holds keys and in-memory network routing state, and that's it.

This is what keeps the library small enough to audit and reusable in contexts — a bot, a bridge, a script, a test harness — that would each want a completely different persistence layer.

Not yet implemented

Feature Notes
Group / closed-group messaging Only 1:1 DMs are supported today

Known limitations

  • TLS certificate verification is disabled for service-node connections (network.VERIFY_TLS). This is expected for this network — Session authenticates nodes by their ed25519/x25519 keys at the protocol layer, not via the CA system — but worth knowing if you're auditing this code.
  • Client is not thread-safe. Its snode-pool and swarm caches are plain attributes, so concurrent calls can race (benignly — the worst case is a duplicated lookup). Use one Client per thread, or your own lock.
  • receive() holds its results in memory. It pages until your swarm is drained, so a large backlog is a large list — pass max_messages and resume from the last "hash" if that matters.

Documentation

Document What's in it
API.md Every public method, its arguments, and the exact shape of what it returns
ADVANCED.md Disappearing messages, typing indicators, read receipts, attachments and content types, voice notes, and the protocol's type/flag values
ARCHITECTURE.md How the network actually works — key derivation, message encryption, onion routing, wire formats, and what every internal module does
CHANGELOG.md Release history

License

GPLv3 — see LICENSE.

About

A lightweight pure-Python library for sending and receiving 1:1 direct messages on the Session messenger network

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages