Pubky SDK (Rust)
Ergonomic building blocks for Pubky apps: a tiny HTTP/PKARR client, session-bound agent, a storage API, signer helpers, and a pairing-based auth flow for keyless apps.
Rust implementation of Pubky SDK.
Install
# Cargo.toml
[]
= "0.x" # this crate
# Optional helpers used in examples:
# pubky-testnet = "0.x"
Quick start
use *; // pulls in the common types
# async
Concepts at a glance
High level actors:
PubkySessionsession-bound identity agent. This is the heart of your Pubky application. Usesession.storage()for reads/writes to the user's homeserver storage. You can get a working session in two ways: (1) requesting auth viaPubkyAuthRequest(keyless apps) or (2) by signin from aPubkySigner(keychain apps).PubkyAuthRequestauth flow for authenticating keyless apps from a PubkySigner.PubkySignerhigh-level signer (keypair holder) withsignup,signin, publishing, and auth request approval.PubkyStoragesimple file-like API:get/put/delete, plusexists(),stats()andlist().Pkdnsresolve/publish_pubkyPkarr records (read-only viaPkdns::new(), publishing when created from aPubkySigner).
Transport:
PubkyHttpClientstateless transport: handles requests to pubky public-key hosts.
Examples
Storage API (session & public)
Use a PubkyStorage to access and write data into Pubky Homeservers.
Public
Use to access public data from any user without need to authenticate.
# use *;
# async
Session
Use to write data into your own homeserver.
# use *;
# async
| Property | Session mode (PubkyStorage::new_from_session()) |
Public mode (PubkyStorage::new_public()) |
|---|---|---|
| Auth | Yes (session cookie auto-attached for this user’s homeserver) | No |
| Path form | Absolute, session-scoped (e.g. /pub/my.app/file.txt) |
User-qualified (e.g. <user>/pub/my.app/file.txt) |
| Reads | Yes | Yes (public data only) |
| Writes | Yes | No (server will reject with 401/403) |
| Cookie leakage across users | Never | N/A |
| Typical use | Your app acting “as the user” | Fetch someone else’s public content without a session |
Resources & addressing
Use absolute paths for session-scoped I/O ("/pub/…"), or user-qualified forms when public:
# use *;
#
By convention, prefer to place your app data in a new public /pub top level folder with domain-like name. Example /pub/mycoolnew.app/
PKDNS (_pubky) Pkarr publishing
Publish and retrieve pkarr record.
# use *;
# async
Pubky QR auth for third-party and keyless apps.
Request auth url and await approval. Typical usage:
- Create an auth request with [
PubkyAuthRequest::new]. - Call [
PubkyAuthRequest::subscribe] to start background polling and obtain thepubkyauth://URL. - Show the returned URL (QR/deeplink) to the signing device (e.g., Pubky Ring iOS/Android).
- Await [
AuthSubscription::wait_for_approval] to obtain a session-bound [PubkySession].
# use *;
# async
See this fully working Auth Flow Example
Relay & reliability
- If you don’t pass a relay, [
PubkyAuthRequest] defaults to a Synonym-hosted instance. If that relay is down, logins won’t complete. - For production and bigger apps, run your own relay (MIT, dockerable): https://httprelay.io
Derives the channel as
base64url(hash(secret)); the token is end-to-end encrypted with thesecret. SeePubkyAuthRequest::new_with_relaydocs for further info.
Features
jsonenablestorage::jsonhelpers (get_json/put_json) and serde on certain types.
Testing locally
Spin up an ephemeral testnet (DHT + homeserver + relay) and run your tests fully offline:
# use ;
# async
Session persistence (scripts that restart)
Export a compact bearer token and import it later to avoid re-auth:
# use *;
# async
Security: the cookie secret is a bearer token. Anyone holding it can act as the user within the granted capabilities. Treat it like a password.
Design notes
- Blocking vs managed pairing: prefer
subscribe()/wait_for_approval()(starts polling immediately when you get the URL) to avoid missing approvals. If you manually fetch the URL before polling, you can race the signer and miss the one-shot response. - Stateless client, stateful session:
PubkyHttpClientnever holds identity;PubkySessiondoes.
Example code
Check more examples using the Pubky SDK.
JS bindings
Find a wrapper of this crate using wasm_bindgen in pubky-sdk/bindings/js.
License: MIT Relay: https://httprelay.io (open source; run your own for production)