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 viaPubkyAuthFlow(keyless apps) or (2) by signin from aPubkySigner(keychain apps).PubkyAuthFlowauth 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 SessionStorage and PublicStorage to read/write data on Pubky homeservers.
Public
Access public data from any user without authenticating.
# use *;
# async
See the Public Storage example.
Session
Read and write data on your own homeserver (authenticated).
# use *;
# async
| Property | Session mode (session.storage()) |
Public mode (PublicStorage::new()) |
|---|---|---|
| Auth | Yes (session cookie auto-attached for your 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 rejects with 401/403) |
| Cookie leakage across users | Never (session paths are always scoped to your user) | N/A |
| Typical use | Your app acting “as the user” | Fetch someone else’s public content |
Resources & addressing
Use absolute paths for session-scoped I/O ("/pub/…"), and user-qualified forms when reading public data.
# use *;
# use IntoPubkyResource;
#
Convention: put your app’s public data under a domain-like folder in /pub, e.g. /pub/mycoolnew.app/.
PKDNS (_pubky) Pkarr publishing
Publish and retrieve pkarr record.
# use *;
# async
Pubky QR auth for third-party and keyless apps
Request an authorization URL and await approval.
Typical usage
- Start an auth flow with
PubkyAuthFlow::start(&caps)(or use the builder to set a custom relay). - Show
authorization_url()(QR/deeplink) to the signing device (e.g., Pubky Ring — iOS / Android). - Await
await_approval()to obtain a session-boundPubkySession.
# use *;
# async
See the fully working Auth Flow Example in /examples/auth_flow.
Relay & reliability
- If you don’t specify a relay,
PubkyAuthFlowdefaults to a Synonym-hosted relay. If that relay is down, logins won’t complete. - For production and larger apps, run your own relay (MIT, Docker): https://httprelay.io.
The channel is derived as
base64url(hash(secret)); the token is end-to-end encrypted with thesecretand cannot be decrypted by the relay.
Custom relay example
# use *;
# async
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)