Skip to main content

Overview

The @atproto/lexicon package provides TypeScript implementation of the Lexicon schema language for AT Protocol. Lexicon is used to define and validate data schemas for records, XRPC endpoints, and other protocol elements.

Installation

Core Concepts

Lexicon schemas define:
  • Record types: Structure of user data (posts, profiles, likes, etc.)
  • XRPC procedures: Request/response schemas for API methods
  • XRPC queries: Parameter and response schemas for API queries
  • Data types: Primitives, objects, arrays, unions, and references
Schemas use familiar concepts like types, required/optional fields, string formats, and validation constraints.

Main Classes

Lexicons

The main class for managing and validating against Lexicon schemas.
Lexicons
Creates a new Lexicon collection. Optionally initializes with an iterable of schema documents.
Iterable<LexiconDoc>
Initial schema documents to add
Methods:
void
Adds a Lexicon schema document to the collection.
LexiconDoc
required
Schema document to add
void
Removes a schema document by its NSID.
string
required
Schema NSID to remove
LexiconDoc | undefined
Retrieves a schema document by NSID.
LexUserType | undefined
Retrieves a specific definition from a schema. Can reference nested definitions using # notation.

Record Validation

void
Validates that a value conforms to a record schema. Throws ValidationError on failure.
string
required
Record type NSID
unknown
required
Record data to validate
ValidationResult
Validates a record and returns a result object instead of throwing.

XRPC Validation

void
Validates XRPC query parameters.
void
Validates XRPC procedure input body.
void
Validates XRPC response body.
void
Validates XRPC subscription message.

Schema Types

Primitive Types

Lexicon supports several primitive types:
String Formats:
  • datetime - ISO 8601 datetime
  • uri - URI
  • at-uri - AT URI (at://…)
  • did - DID identifier
  • handle - Handle (domain name)
  • at-identifier - DID or handle
  • nsid - Namespaced identifier
  • cid - Content identifier
  • language - BCP-47 language tag
  • tid - Timestamp identifier
  • record-key - Record key

Complex Types

Object

Array

Reference

Union


IPLD Types

Bytes


Blob


Blob Utilities

BlobRef

Represents a blob reference in records.
BlobRef
Creates a blob reference.
string | CID
required
Content identifier
string
required
MIME type (e.g., ‘image/jpeg’)
number
required
Size in bytes
boolean
Type guard to check if a value is a blob reference.

Type Definitions

LexiconDoc

LexRecord

Type definition for a record schema.

LexXrpcProcedure

LexXrpcQuery


Serialization Utilities

lexToIpld

Converts a value to IPLD format (for CBOR encoding).

ipldToLex

Converts an IPLD value back to Lexicon format.

Complete Example

Additional Resources