Skip to main content

Overview

The @atproto/repo package provides TypeScript implementation of AT Protocol repositories and the Merkle Search Tree (MST) data structure. Repositories are signed, content-addressed key/value stores that hold user data records.

Installation

Core Concepts

Repository Structure

An AT Protocol repository consists of:
  • Commit: Signed metadata pointing to the current data state
  • MST (Merkle Search Tree): Content-addressed tree structure storing record keys and CIDs
  • Blocks: CBOR-encoded record data and MST nodes
Repositories use content addressing (CIDs) to reference data, creating a verifiable, immutable history of changes.

Merkle Search Tree (MST)

The MST is an ordered, deterministic tree structure where:
  • Keys are sorted alphabetically
  • Insertion order doesn’t affect the final tree structure
  • Each key is hashed to determine its layer in the tree
  • Subtrees are referenced by their CID
This enables efficient diffing, syncing, and verification of repository state.

Main Classes

Repo

The main repository class for creating and updating repositories.
Promise<Repo>
Creates a new repository.
RepoStorage
required
Block storage implementation
string
required
DID of the repository owner
Keypair
required
Signing key for commits
RecordCreateOp[]
default:"[]"
Optional initial records to create
Promise<Repo>
Loads an existing repository from storage.
RepoStorage
required
Block storage containing the repository
CID
Optional commit CID. If not provided, loads the root commit from storage.
Instance Methods:
Promise<Repo>
Applies record operations and creates a new commit. Returns a new Repo instance with updated state.
RecordWriteOp | RecordWriteOp[]
required
One or more record operations (create, update, or delete)
Keypair
required
Signing key for the new commit
Promise<CommitData>
Formats a commit without applying it to storage. Useful for preparing commits for transmission.
Promise<Repo>
Applies a pre-formatted commit to storage.
Promise<RepoRecord | null>
Retrieves a record from the repository.
string
required
Collection NSID (e.g., ‘app.bsky.feed.post’)
string
required
Record key
Properties:
  • did: string - Repository DID
  • cid: CID - Current commit CID
  • commit: Commit - Current commit object
  • data: MST - Current MST root
  • storage: RepoStorage - Block storage

MST

Merkle Search Tree implementation.
Promise<MST>
Creates a new MST.
MST
Loads an MST from a CID. This is a lazy load - data is fetched on demand.
Instance Methods:
Promise<MST>
Adds or updates a key/value pair. Returns a new MST instance.
Promise<MST>
Updates an existing key. Throws if key doesn’t exist.
Promise<MST>
Deletes a key. Returns a new MST instance.
Promise<CID | null>
Retrieves the CID for a key.
AsyncIterable<{ key: string; value: CID }>
Lists entries in the tree.
number
Maximum number of entries to return
string
Start listing after this key
string
Stop listing before this key
Promise<CID>
Gets the CID of this MST node.

Storage Implementations

MemoryBlockstore

In-memory block storage for testing and development.

RepoStorage Interface

Interface for implementing custom storage backends.

Record Operations

RecordWriteOp Types


CAR File Utilities

exportCarSlice

Exports a repository slice as a CAR (Content Addressable aRchive) file.
Promise<Uint8Array>
Exports blocks as a CAR file.
ReadableBlockstore
required
Block storage
CID
required
Root commit CID to export
CID | null
Previous commit CID for incremental export

importCarSlice

Imports blocks from a CAR file.

Data Structures

BlockMap

Map of CIDs to block bytes.

CidSet

Set of CIDs.

Complete Example

Additional Resources