What are Lexicons?
Lexicons are AT Protocol’s schema definition language, similar to JSON Schema or OpenAPI. They define:- Record types - Structure of data stored in repositories
- API endpoints - XRPC queries, procedures, and subscriptions
- Validation rules - Type checking, constraints, and formats
Why Lexicons?
Interoperability
Lexicons ensure that different implementations of AT Protocol can understand each other’s data and APIs. A post created by one client can be read by any other client.Type Safety
Schemas provide strong typing and validation, catching errors before they propagate through the network.Versioning
Lexicons use namespaced identifiers, allowing multiple schemas to coexist and evolve independently.Documentation
Schemas serve as machine-readable documentation for APIs and data structures.Lexicon Structure
A Lexicon document is a JSON file with the following structure:lexicon: Schema version (currently1)id: Unique identifier in reverse-DNS formatdefs: Named definitions within the schemamain: The primary definition (used when referencing by ID alone)
Lexicon Identifiers
Lexicons use reverse-DNS naming:{authority}.{name}.{name}...
- Authority: Domain you control (reversed)
- Names: Hierarchical categorization
Definition Types
Record Definitions
Define data that can be stored in repositories:Query Definitions
Define read-only API endpoints:Procedure Definitions
Define API endpoints that modify state:Subscription Definitions
Define websocket event streams:Using Lexicons in Code
The@atproto/lexicon package provides validation:
Validating Records
Validating XRPC Calls
Data Types
Lexicons support rich data types:Primitive Types
String Formats
String Constraints
Numeric Constraints
Arrays
Objects
Blobs
For binary data like images:References
Reference other definitions:Unions
One of several types:Real-World Example: Post Record
Here’s the actual Lexicon for a Bluesky post:Managing Lexicons
Adding Lexicons
Retrieving Lexicons
Removing Lexicons
Validation Results
For non-throwing validation:Best Practices
Use namespaced IDs
Use namespaced IDs
Always use reverse-DNS naming to avoid collisions. Use a domain you control.
Validate early
Validate early
Validate data as soon as it enters your system to catch errors early.
Define strict schemas
Define strict schemas
Use constraints like
maxLength, maxGraphemes, and required to enforce data quality.Use references for reusability
Use references for reusability
Define common types once and reference them to maintain consistency.
Document descriptions
Document descriptions
Include clear
description fields - they serve as API documentation.Error Handling
Related Topics
- Repositories - Records are stored in repositories
- Overview - AT Protocol architecture
- Authentication - API authentication
Additional Resources
@atproto/lexicon Package
NPM package documentation
Lexicon Specification
Official Lexicon language specification
Lexicon Repository
Browse official AT Protocol and Bluesky lexicons
Schema Validator
Online Lexicon validator tool