Skip to main content

Overview

The @atproto/syntax package provides validation and parsing utilities for AT Protocol identifier formats including DIDs, handles, NSIDs, AT URIs, TIDs, and record keys.

Installation

DID (Decentralized Identifier)

Validation for W3C DID identifiers.

Validation Functions

boolean
Type guard that returns true if the input is a valid DID.
string
required
String to validate
void
Asserts that input is a valid DID. Throws InvalidDidError if invalid.

DID Format

Constraints:
  • Must start with did:
  • Method name must be lowercase letters
  • Method-specific identifier can contain: a-zA-Z0-9._:%-
  • Cannot end with : or %
  • Maximum length: 2048 characters
Examples:

Handle

Validation for domain-based handles.

Validation Functions

boolean
Type guard that returns true if the input is a valid handle.
void
Asserts that input is a valid handle. Throws InvalidHandleError if invalid.
string
Converts handle to lowercase (handles are case-insensitive).
boolean
Checks if handle’s TLD is allowed (not .local, .internal, etc.).

Handle Format

Constraints:
  • Valid domain name format (RFC-1035, RFC-3696)
  • Segments contain only: a-zA-Z0-9-
  • Segments cannot start or end with hyphen
  • Each segment: 1-63 characters
  • Total length: max 253 characters
  • At least 2 segments (e.g., user.domain)
  • TLD must start with letter
  • Case-insensitive
Disallowed TLDs: .local, .arpa, .invalid, .localhost, .internal, .example, .alt, .onion Examples:

NSID (Namespaced Identifier)

Validation for Lexicon schema identifiers.

NSID Class

NSID
Parses an NSID string. Throws InvalidNsidError if invalid.
NSID
Creates an NSID from authority and name.
string
required
Domain authority (e.g., ‘example.com’)
string
required
Method name (e.g., ‘post’)
boolean
Returns true if the string is a valid NSID.
Properties:
  • authority: string - Reversed domain (e.g., ‘example.com’)
  • name: string - Method name
  • segments: readonly string[] - All segments

Validation Functions

NSID Format

Constraints:
  • Reversed domain notation + name
  • Segments contain: a-zA-Z0-9-
  • Cannot start/end with hyphen
  • First segment cannot start with digit
  • Last segment (name) must be letters/digits only (no hyphens)
  • Each segment: 1-63 characters
  • Total length: max 317 characters (253 + 1 + 63)
  • At least 3 segments
Examples:

AT URI

Validation and parsing for AT Protocol URIs.

AtUri Class

AtUri
Parses an AT URI. Optionally resolves relative URIs against a base.
string
required
AT URI to parse
string | AtUri
Base URI for resolving relative references
AtUri
Constructs an AT URI from parts.
string
required
Handle or DID
string
Collection NSID
string
Record key
Properties:
  • protocol: string - Always 'at:'
  • origin: string - at://{host}
  • host: string - DID or handle
  • hostname: string - Alias for host
  • pathname: string - Path portion
  • collection: string - Collection NSID
  • rkey: string - Record key
  • hash: string - Fragment
  • searchParams: URLSearchParams - Query parameters
  • did: DidString - DID (throws if host is not DID)
Methods:
  • toString(): AtUriString - Converts to string

AT URI Format

Format:
Examples:

TID (Timestamp Identifier)

Validation for timestamp-based identifiers.
boolean
Type guard for TID validation.
void
Asserts input is a valid TID. Throws InvalidTidError if invalid.

TID Format

Constraints:
  • Exactly 13 characters
  • Base32-sortable encoding
  • First character: [234567abcdefghij]
  • Remaining characters: [234567abcdefghijklmnopqrstuvwxyz]
Examples:

Record Key

Validation for repository record keys.
boolean
Type guard for record key validation.
void
Asserts input is a valid record key. Throws InvalidRecordKeyError if invalid.

Record Key Format

Constraints:
  • Allowed characters: a-zA-Z0-9_~.:-
  • Length: 1-512 characters
  • Cannot be . or ..
Examples:

Datetime

Validation for ISO 8601 datetime strings.
boolean
Returns true if input is a valid ISO 8601 datetime.
void
Asserts input is a valid ISO 8601 datetime. Throws if invalid.

Complete Example

Error Classes

All validation functions throw specific error types:
  • InvalidDidError
  • InvalidHandleError
  • InvalidNsidError
  • InvalidTidError
  • InvalidRecordKeyError
  • InvalidDatetimeError

Additional Resources