Skip to main content
The @atproto/xrpc package is a low-level TypeScript client library for making XRPC (Cross-organizational RPC) calls to AT Protocol services. It handles request/response encoding, Lexicon validation, and error handling.

Installation

Overview

XRPC is the RPC protocol used by AT Protocol. It defines how clients and servers communicate over HTTP, with schema validation powered by Lexicons. Key features:
  • Schema-based request/response validation
  • Automatic encoding/decoding of request bodies
  • Built-in error handling
  • Support for custom fetch handlers
  • Type-safe method calls
Most applications should use @atproto/api which provides a higher-level interface. Use @atproto/xrpc directly when you need low-level control or are working with custom Lexicons.

Quick Start

XrpcClient

The main class for making XRPC calls.

Constructor

FetchHandler | FetchHandlerObject | FetchHandlerOptions
required
Fetch handler configuration. Can be:
  • A string URL (base service URL)
  • A custom fetch function
  • An object with service URL and optional fetch function
  • A FetchHandler function
Lexicons | Iterable<LexiconDoc>
required
Lexicon definitions for schema validation. Can be:
  • A Lexicons instance
  • An array of LexiconDoc objects

Simple URL Constructor

Custom Fetch Constructor

Object Constructor

Making XRPC Calls

call Method

string
required
The NSID (Namespaced Identifier) of the method to call (e.g., com.atproto.repo.getRecord)
QueryParams
Query parameters for the request
unknown
Request body data (for procedures)
CallOptions
Additional options:
  • encoding - Content-Type for the request body
  • signal - AbortSignal for request cancellation
  • headers - Additional headers

Query Example

Procedure Example

Header Management

Setting Headers

(key: string, value: string | (() => string | null)) => void
Set a header that will be included in all requests. The value can be a string or a function that returns a string or null.

Removing Headers

(key: string) => void
Remove a previously set header

Clearing All Headers

() => void
Remove all custom headers

Response Types

XRPCResponse

true
Always true for successful responses
any
The response body, parsed according to the content type
HeadersMap
Response headers as a key-value object

Error Handling

XRPCError

All XRPC errors are instances of XRPCError:
false
Always false for errors
ResponseType
Numeric response type enum value
string | undefined
Error code from the server (e.g., InvalidRequest, RateLimitExceeded)
string
Error message
HeadersMap | undefined
Response headers if available

ResponseType Enum

Error Handling Example

Custom Fetch Handlers

Fetch handlers allow you to customize request behavior, add authentication, implement retries, etc.

Basic Fetch Handler

Authenticated Fetch Handler

Retry Fetch Handler

Call Options

Encoding

Specify the content type for request bodies:

Abort Signals

Cancel requests using AbortController:

Per-Request Headers

Working with Lexicons

Lexicon Structure

A lexicon defines the schema for an XRPC method:

Multiple Lexicons

Adding Lexicons at Runtime

Types

QueryParams

Query parameters can be any serializable values:

HeadersMap

CallOptions

Gettable

A value that can be static or computed via a function.

Client (Deprecated)

The Client class is deprecated. Use XrpcClient instead.
For backwards compatibility, the legacy Client class is still available:

Advanced Usage

Streaming Requests

For streaming request bodies (e.g., file uploads):

Binary Responses

The client automatically handles different response types based on Content-Type headers:

Validation Errors

When response validation fails:

Best Practices

1. Reuse Client Instances

Create one client instance and reuse it:

2. Handle Errors Appropriately

3. Use TypeScript

Leverage TypeScript for type safety:

Examples

Complete Example with Authentication

See Also