Pubky SDK
Ergonomic building blocks for Pubky apps: one facade (Pubky) plus focused actors for sessions, storage API, signer helpers, and QR 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 *;
use ClientId;
# async
Key formats (display vs transport)
PublicKey has two string representations:
- Display format:
pubky<z32>(used for logs/UI and human-facing identifiers). - Transport/storage format: raw
z32(used for hostnames, headers, query params, serde/JSON, and database storage).
Use .z32() whenever you are building hostnames or transport values (for example _pubky.<z32> or the pubky-host header). Use Display/.to_string() when you want the prefixed identifier for people.
Reuse a single facade across your app
Use a shared Pubky (via cloning it, passing down as argument or behind OnceCell) instead of constructing one per request. This avoids reinitializing transports and keeps the same client available for repeated usage.
Mental model
Pubky- facade, always start here! Owns the transport and constructs actors.PubkySigner- local key holder. Cansignup,signin, approve QR auth, publish PKDNS.PubkySession- authenticated “as me” handle. Exposes session-scoped storage.GrantManager- account-level grant listing and revocation using an authenticated root session.PublicStorage- unauthenticated reads of others’ public data.Pkdns- resolve/publish_pubkyrecords.
Transport:
PubkyHttpClient: handles requests to pubky public-key hosts.
Examples
Storage API (session & public)
Session (authenticated):
use ;
# async
Public (read-only):
use ;
# async
See the Public Storage example.
Path rules:
- Session storage uses absolute paths like
"/pub/app/file.txt". - Public storage uses addressed form
pubky<user>/pub/app/file.txt(preferred) orpubky://<user>/.... - A storage path cannot be both an exact file and an implicit folder prefix. For example:
- if
/pub/app/fooexists, writing/pub/app/foo/bar.jsonreturns409 Conflict. - if descendants under
/pub/app/foo/exist, writing/pub/app/fooreturns409 Conflict.
- if
Convention: put your app’s public data under a domain-like folder in /pub, e.g. /pub/my-new-app/.
Resolve identifiers into transport URLs
Need to feed a public resource into a raw HTTP client? Use [resolve_pubky] to transform the human-facing identifier into the HTTPS homeserver URL:
# use resolve_pubky;
#
PKDNS (Pkarr)
Resolve another user’s homeserver (_pubky record), or publish your own via the signer.
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
pubky.start_grant_auth_flow(&caps, ..)(orpubky.start_cookie_auth_flow(&caps, ..)for the cookie variant). You can also usePubkyGrantAuthFlow::builder()/PubkyCookieAuthFlow::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, orawait_credential()for a rawGrantCredential/CookieCredentialthat you can persist, inspect, or lift into a session later viaPubkySession::from_{grant,cookie}_credential.
# use ;
# async
Approve an auth request
signer.approve_auth.await?;
See the fully functional Auth Flow Example.
Relay & reliability
- If you don’t specify a relay, the auth flow defaults 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
Tip: reuse
pubky.client()when customising the relay so the flow shares TLS and pkarr configuration with the rest of your application.
Features
json: enableStoragehelpers (.get_json()/.put_json()) and serde on certain types.
# Cargo.toml
[]
= { = "x.y.z", = ["json"] }
Testing locally
Spin up an ephemeral testnet (DHT + homeserver + relay) and run your tests fully offline:
# use ;
# async
Keypair and Session persistence
Encrypted Keypair secrets (.pkarr):
use Pubky;
#
Session secrets (.sess):
use ;
# async
Security: the
.sesssecret is a bearer token. Anyone holding it can act as the user within the granted capabilities. Treat it like a password.
Example code
Check more examples using the Pubky SDK.
JS bindings
Find a wrapper of this crate using wasm_bindgen in npmjs.com. Or build on pubky-sdk codebase under pubky-sdk/bindings/js.
License: MIT Relay: https://httprelay.io (open source; run your own for production)