Using lex
Lexicon schemas enable code generation with types and validation. Our SDKs each install a lex tool that lets you generate a type-safe client that knows which parameters each AT endpoint expects.
TypeScript
First, install the @atproto/lex package:
npm install -g @atproto/lex
This provides the lex command, which you can use to install Lexicons into a local project:
lex install app.bsky.feed.post app.bsky.feed.like
This creates:
lexicons.json- manifest tracking installed Lexicons and their versions (CIDs)lexicons/- directory containing the Lexicon JSON files
Finally, generate TypeScript schemas from the installed Lexicons:
lex build
This generates TypeScript files in ./src/lexicons. Now, you'll have these functions available to use in your code:
import { Client } from '@atproto/lex'
import * as app from './lexicons/app.js'
// Create an unauthenticated client instance
const client = new Client('https://public.api.bsky.app')
// Start making requests using generated schemas
const response = await client.call(app.bsky.actor.getProfile, {
actor: 'pfrazee.com',
})
For more guidance on working with lex, refer to the readme.