Skip to main content

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
Every record and API in AT Protocol is governed by a Lexicon schema.

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:
Key Components:
  • lexicon: Schema version (currently 1)
  • id: Unique identifier in reverse-DNS format
  • defs: Named definitions within the schema
  • main: The primary definition (used when referencing by ID alone)

Lexicon Identifiers

Lexicons use reverse-DNS naming:
Format: {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:
Usage:

Managing Lexicons

Adding Lexicons

Retrieving Lexicons

Removing Lexicons

Validation Results

For non-throwing validation:

Best Practices

Always use reverse-DNS naming to avoid collisions. Use a domain you control.
Validate data as soon as it enters your system to catch errors early.
Use constraints like maxLength, maxGraphemes, and required to enforce data quality.
Define common types once and reference them to maintain consistency.
Include clear description fields - they serve as API documentation.

Error Handling

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