Tl.
An XRPC client · 0.15.0 · MIT
atpxrpc
atpxrpc calls any XRPC method as a stored account. It logs in with an app password, keeps the session in a local file, and refreshes the access token when it expires, so a script can make the same call today and next month without handling a token. It also proxies a call through your PDS to another service, and makes one-off calls with an OAuth access token and a DPoP key.
§ 01
Install
Homebrew has prebuilt binaries for macOS and Linux. On crates.io, clap is a default feature of atpxrpc and the binary is the crate’s only one, so cargo install doesn’t need any flags beyond the version.
brew tap ngerakines/tap
brew install atpxrpc
# or
cargo install atpxrpc --version 0.15.0
atpxrpc --version prints atpxrpc 0.15.0.
§ 02
Logging in
Use an app password, not the account password. Make one on your PDS, and revoke it there when you’re done with the machine. login takes a handle or a DID. It resolves it, checks the PDS endpoint the DID document names against the endpoint policy in § 07, creates a session, and stores it. Each step is reported on stderr.
# A hidden prompt on stderr.
atpxrpc login ngerakines.me
# From the environment, for CI or a script.
ATPROTO_PASSWORD=xxxx-xxxx-xxxx-xxxx atpxrpc login ngerakines.me
# --show also prints the stored session as JSON.
atpxrpc login ngerakines.me --show
The password can also go after the identifier, but then it lands in shell history and in ps. The environment variable keeps it out of both.
§ 03
Making calls
Without a subcommand, the first argument is an XRPC method NSID and the rest are key=value query parameters. The verb comes from the arguments and nothing else. Without --body or --bytes the call is a query (GET). Either one makes it a procedure (POST). The two can’t be combined, and --content-type needs --bytes. Whether stdin is a terminal doesn’t matter, so a command behaves the same in a shell, a pipeline, cron, and ssh host cmd.
# Query (GET).
atpxrpc com.atproto.repo.getRecord \
repo=ngerakines.me collection=app.bsky.actor.profile rkey=self
# Procedure (POST), with a JSON body from a file, or from stdin with -.
atpxrpc com.atproto.repo.createRecord --body record.json
cat record.json | atpxrpc com.atproto.repo.createRecord --body -
# Raw bytes (POST), always from stdin, with a content type.
atpxrpc --bytes --content-type image/jpeg com.atproto.repo.uploadBlob < gadget.jpg
# Write the response to a file instead of stdout.
atpxrpc com.atproto.repo.listRecords \
repo=ngerakines.me collection=app.bsky.feed.post --out posts.json
Query parameters go out whether or not there’s a body, so a procedure that takes parameters gets both. An argument without an = is refused, not dropped.
The response is indented JSON on stdout. Progress and errors go to stderr, so atpxrpc … | jq works. Any status outside 2xx is a failure. Nothing reaches stdout or --out, the server’s body goes to stderr, and atpxrpc exits non-zero with error-atpxrpc-xrpc-1, naming the status and the body’s error and message. A 2xx without a body, from a method without output, doesn’t print anything.
--bytes is about the request. Its reply is the only one written back byte for byte. Every other response is decoded as JSON, so a GET that returns a blob can’t be saved raw.
§ 04
Subcommands and options
This is atpxrpc --help and each subcommand’s --help at 0.15.0.
| Subcommand | Does |
|---|---|
| login <IDENTIFIER> [PASSWORD] | Logs in with a handle or DID and an app password, and stores the session. The password comes from the argument, ATPROTO_PASSWORD, or a hidden prompt. --show prints the session as JSON on stdout. |
| logout <HANDLE> | Removes one account from the store. |
| accounts | Prints the store’s path on stderr, then the stored accounts on stdout, one per line, tab-separated: handle, DID, and PDS endpoint. |
| check-auth [HANDLE] | Calls com.atproto.server.getSession. If the token expired, it refreshes, saves, and asks again. It exits 0 only when getSession names the account’s DID. |
| proxy <AUDIENCE> <NSID> [PARAMS]... | Sends the call to your PDS with an atproto-proxy header naming <AUDIENCE>, written DID#serviceId. --manual does it by hand instead. It gets a service-auth token from com.atproto.server.getServiceAuth, resolves the audience to its endpoint, and calls it directly. Takes --body, --bytes, --content-type, and --out under the same rules as a bare call. |
| dpop <SUBJECT> <NSID> [PARAMS]... | Makes one call with an OAuth access token and the DPoP key bound to it, to the PDS of <SUBJECT>. It doesn’t read or write the account store. The token comes from --access-token, ATPROTO_ACCESS_TOKEN, or a prompt, and the key from ATPROTO_DPOP_KEY or a prompt. A key=value argument is a query parameter and header=name=value is a request header. Takes --body and --out. |
| help | Prints help for atpxrpc or a subcommand. |
| Argument | Does |
|---|---|
| [NSID] | The XRPC method, such as com.atproto.repo.getRecord. |
| [PARAMS]... | Query parameters as key=value, sent whether or not there’s a body. |
| --handle <HANDLE> | The stored account to use. |
| --out <OUT> | Write the response body to a file instead of stdout. |
| --body <BODY> | A JSON request body from a file, or - for stdin. Makes the call a procedure (POST). |
| --bytes | Send stdin as raw bytes. Forces POST. |
| --content-type <TYPE> | The Content-Type for --bytes, which it requires. Defaults to application/octet-stream. |
| -h, --help | Print help. --help adds the store, the call rules, examples, and the environment. |
| -V, --version | Print the version. |
--handle is global, but it has to come after a subcommand: atpxrpc check-auth --handle ngerakines.me. Before one, it hides the subcommand, and check-auth would be read as an NSID. atpxrpc refuses that instead of sending GET /xrpc/check-auth to your PDS. For a bare call, either position works.
§ 05
The account store, shared with atpmcp
atpxrpc keeps its accounts in one JSON file. atpmcp reads and writes the same file, through the library half of this crate, so an account added with atpxrpc login or atpmcp login works in both. atpmcp uses it for invoke_xrpc and upload_blob, and sends an authenticated request only to that account’s own PDS.
| Platform | Default path |
|---|---|
| Linux | ~/.config/atpxrpc/config.json |
| macOS | ~/Library/Application Support/atpxrpc/config.json |
| Windows | %APPDATA%\atpxrpc\config.json |
ATPXRPC_CONFIG replaces the default with an exact file path. Both programs honor it. atpxrpc accounts prints the path it resolved, and so does atpmcp login when it finishes. Ask them rather than trusting the table.
The store is plaintext
It holds each account’s app password, access token, and refresh token as written. On Unix the file is created with mode 0600 in the same call that opens it, so it never exists with a wider mode, and a failure to set the mode is an error. On Windows, and anywhere else that isn’t Unix, atpxrpc doesn’t set a permission, and the file is as private as the directory holding it.
To revoke a machine’s copy, delete the file. Then revoke the app password on your PDS too, because deleting the file doesn’t.
Every save writes a temporary file beside the store and renames it into place, so a reader sees the whole old file or the whole new one. A read-modify-write takes an advisory lock on a sibling .lock file first, so two processes can’t interleave their edits. The lock is Unix-only, like the 0600. A token refresh writes back only the two tokens, and skips the write if the account was logged in again while it ran. atpmcp refreshing in the background won’t put an old app password back over a login you ran in a terminal.
§ 06
Environment variables
| Variable | Effect |
|---|---|
| ATPXRPC_CONFIG | Path of the account store, replacing the platform default. |
| ATPROTO_PASSWORD | App password for login, instead of the prompt. |
| ATPROTO_ACCESS_TOKEN | OAuth access token for dpop, instead of the prompt. Also settable as --access-token. |
| ATPROTO_DPOP_KEY | DPoP private key, as a did:key, for dpop, instead of the prompt. |
| ATPXRPC_ALLOW_INSECURE_ENDPOINTS | Waives the endpoint policy in § 07. 1, true, yes, or on. |
| ATPXRPC_READ_TIMEOUT | Seconds a response may take to begin, and then between reads. Defaults to 60. It must be a whole number above zero, or atpxrpc refuses it with error-atpxrpc-env-1. |
| PLC_HOSTNAME | PLC directory hostname. Defaults to plc.directory. |
| USER_AGENT | Replaces the generated atpxrpc (<version>; +https://tangled.org/ngerakines.me/atproto-crates). |
| CERTIFICATE_BUNDLES | Extra CA certificate bundle paths, separated by ;. |
| DNS_NAMESERVERS | DNS nameserver addresses, separated by ;. |
The last two take a ;, not a : or a ,. A CERTIFICATE_BUNDLES written with the wrong separator isn’t an error. It’s one path that doesn’t exist, and a bundle that quietly doesn’t load.
§ 07
Endpoint policy and timeouts
A DID document’s serviceEndpoint is published by the DID’s subject, so whoever controls a handle controls where atpxrpc sends things, the app password included. Every endpoint atpxrpc resolves is checked before anything is sent to it. It refuses a scheme other than https, an IP address literal, a single-label host, a host with embedded credentials, and any port other than 443, with error-atpxrpc-resolve-3 naming the endpoint and the reason. The check runs where an endpoint is resolved, which is login, dpop, and proxy --manual. Ordinary calls reuse the endpoint checked at login.
A call, createSession, and a session refresh don’t follow redirects at all, because a 307 would send the body again, and on login that body is the app password. Identity resolution follows up to five redirects, since the handle specification allows them, and checks each hop against the same policy. A refused hop is error-atpxrpc-resolve-4.
The policy also refuses http://127.0.0.1:4800, where this workspace’s pds listens. For local development, set ATPXRPC_ALLOW_INSECURE_ENDPOINTS=1.
Every request gives up on a connection after 10 seconds, and on a response after 60 seconds without a byte. The 60 seconds is a read timeout, so a getRepo download that keeps arriving isn’t cut off. A --bytes upload gets one more second per 16 KiB of payload, because the clock runs from the start of the request until the response begins.
§ 08
Worked examples
Listing 4 uploads a photo and makes a record that points to it. It needs a stored account, so this page shows the commands without their output. It uses jo to build JSON.
GADGET=$(atpxrpc --handle ngerakines.me --bytes --content-type image/jpeg \
com.atproto.repo.uploadBlob < gadget.jpg)
jo repo=did:plc:cbkjy5n7bk3ax2wplmtjofq2 collection=dev.ngerakines.cat \
record=$(jo '$type'=dev.ngerakines.cat text="Gadget" cat=$GADGET) \
| atpxrpc --handle ngerakines.me com.atproto.repo.createRecord --body -
# A service-auth token for another service.
atpxrpc --handle ngerakines.me com.atproto.server.getServiceAuth \
aud=did:web:com.example.foo lxm=com.example.foo
# Through the PDS to the Bluesky AppView, then the same by hand.
atpxrpc proxy did:web:api.bsky.app#bsky_appview \
app.bsky.feed.getAuthorFeed actor=ngerakines.me
atpxrpc proxy did:web:api.bsky.app#bsky_appview \
app.bsky.feed.getAuthorFeed actor=ngerakines.me --manual
# One call with an OAuth token and its DPoP key, plus a header.
ATPROTO_ACCESS_TOKEN=... ATPROTO_DPOP_KEY=did:key:... \
atpxrpc dpop ngerakines.me app.bsky.feed.getTimeline limit=5 \
header=atproto-proxy=did:web:api.bsky.app#bsky_appview
The rest run without an account. Every output below is what atpxrpc 0.15.0 printed, against a store that didn’t hold any accounts.
$ ATPXRPC_CONFIG=./store.json atpxrpc accounts
Accounts are stored in ./store.json (plaintext).
No accounts configured.
$ atpxrpc com.atproto.repo.describeRepo repo=ngerakines.me
Error: error-atpxrpc-account-1 No accounts configured, run 'atpxrpc login' first
$ atpxrpc --handle ngerakines.me check-auth
Error: 'check-auth' is a subcommand, not an XRPC method NSID.
A global option before a subcommand hides it. Write `atpxrpc check-auth --handle <handle>` instead.
$ atpxrpc com.atproto.repo.describeRepo repo
Error: expected key=value, got repo
$ atpxrpc com.atproto.repo.createRecord --body record.json --bytes
error: the argument '--body <BODY>' cannot be used with '--bytes'