com.appendBroker
@atiproto/agent
Adds or promotes a PaymentBroker service entry on the authenticated user's DID document. Broker entries follow the W3C DID Core service shape and live in the document's service[] block. Any compliant PoS or broker resolves who handles payments by reading the DID document.
Signature
agent.com.appendBroker(broker: string,options?: AppendBrokerOptions,): Promise<void>interface AppendBrokerOptions {/** Promote ahead of the existing broker block (no effect when no brokers exist). */default?: boolean;/** Token from com.atproto.identity.requestPlcOperationSignature. */token?: string;/** Override the derived serviceEndpoint (default: https://<host> from did:web). */serviceEndpoint?: string;}
Service entry shape
Each broker becomes one standard W3C service entry on the user's DID document:
{id: "${broker}#payment-broker",type: "PaymentBroker",serviceEndpoint: "https://<host>"}
The serviceEndpoint is derived from the broker's did:web host. Pass options.serviceEndpoint to override (required when the broker is not a did:web).
Sort rules
- No brokers yet: the new entry is appended to the end of
service[], regardless of thedefaultflag. - New broker, default=false: inserted right after the last existing PaymentBroker entry. The broker block stays contiguous.
- New broker, default=true: inserted just before the previous first PaymentBroker. Non-broker entries (e.g.
atproto_pds) that precede the broker block stay where they are. - Existing broker, default=false: no-op (returns without writing).
- Existing broker, default=true: moves the entry to the front of the broker block (no-op when it's already first).
Other services on the DID document are preserved untouched.
Add a broker
await agent.com.appendBroker("did:web:broker.example");
Set the default broker
await agent.com.appendBroker("did:web:broker.example", {default: true,});
Email-confirmed PLC operations
Many PDSes gate PLC operations on an emailed token. The common pattern is to try the call first (it'll fail when a token is required), then request the email, prompt the user, and retry:
async function addBroker(agent, brokerDid: string) {try {// Attempt 1: PDSes that don't require email confirmation succeed here.await agent.com.appendBroker(brokerDid, { default: true });return;} catch (err) {// Heuristic: anything that looks like "token required" triggers the// email flow. Match on XRPC error name / message; adjust to your PDS.if (!needsPlcToken(err)) throw err;}// Attempt 2: trigger the email, prompt the user, retry with the token.await agent.com.atproto.identity.requestPlcOperationSignature();const token = await promptUserForToken({title: "Confirm broker change",message: "Check your email and paste the confirmation code.",});await agent.com.appendBroker(brokerDid, { default: true, token });}function needsPlcToken(err: unknown): boolean {if (!err || typeof err !== "object") return false;const e = err as { error?: string; message?: string };return (e.error === "InvalidToken" ||e.error === "AuthRequired" ||/token/i.test(e.message ?? ""));}
The exact error code depends on the PDS implementation; tighten needsPlcToken against the responses you observe in development.
Override the serviceEndpoint
When the broker is not a did:web, or its XRPC root lives at a path under the host, pass options.serviceEndpoint explicitly.
await agent.com.appendBroker("did:plc:broker-abc", {serviceEndpoint: "https://broker.example/payment",});
Reading brokers back
Use repo.profile.get to read the current broker list. The PoS resolves the user's DID document for you, filters service[] for entries typed PaymentBroker, and returns the broker DIDs (the portion before #payment-broker) in order.
const { data } = await agent.com.atiproto.repo.profile.get({did: agent.session!.did,});// data.brokers === ["did:web:broker.example", ...]
Related
- agent
- Broker Onboarding: end-to-end broker onboarding flow.
- com.atiproto.profile