@atproto/api package is the primary client library for building applications on AT Protocol and Bluesky. It provides high-level APIs for authentication, content management, social features, and moderation.
Installation
Core Concepts
The package is built around three main classes:Agent- The primary client for making API callsRichText- For handling rich text with mentions, links, and hashtags- Moderation APIs - For implementing content moderation
Quick Start
Basic Setup with Credentials
OAuth-Based Authentication
For production applications, OAuth is recommended:Agent
TheAgent class is the main interface for interacting with AT Protocol services.
Constructor
SessionManager | FetchHandler | FetchHandlerOptions
required
Session manager instance or fetch handler configuration
Authentication Properties
string | undefined
The DID of the authenticated user, or
undefined if not authenticatedstring
Gets the authenticated user’s DID, or throws an error if not authenticated
SessionManager
The session manager instance used by this agent
Feed & Content Methods
(params?, opts?) => Promise<Response>
Fetch the authenticated user’s home timeline
(params, opts?) => Promise<Response>
Fetch posts from a specific user
(params, opts?) => Promise<Response>
Fetch a post thread with replies
(params, opts?) => Promise<Response>
Fetch multiple posts by URI
(record: Partial<PostRecord>) => Promise<Response>
Create a new post
(postUri: string) => Promise<void>
Delete a post
Social Interactions
(uri: string, cid: string, via?) => Promise<Response>
Like a post
(likeUri: string) => Promise<void>
Remove a like
(uri: string, cid: string, via?) => Promise<Response>
Repost a post
(repostUri: string) => Promise<void>
Remove a repost
(did: string, via?) => Promise<Response>
Follow a user
(followUri: string) => Promise<void>
Unfollow a user
Profile Management
(params, opts?) => Promise<Response>
Fetch a user profile
(params, opts?) => Promise<Response>
Fetch multiple user profiles
(updateFn) => Promise<void>
Update the authenticated user’s profile
Blob Upload
(data: BlobData, opts?) => Promise<Response>
Upload a blob (image, video, etc.)
Notifications
(params?, opts?) => Promise<Response>
List notifications for the authenticated user
(params?, opts?) => Promise<Response>
Get the count of unread notifications
(seenAt?: string) => Promise<Response>
Mark notifications as seen
Moderation
(actor: string) => Promise<Response>
Mute a user
(actor: string) => Promise<Response>
Unmute a user
Preferences
() => Promise<BskyPreferences>
Fetch user preferences including moderation settings
(mutedWord) => Promise<void>
Add a muted word
(mutedWord) => Promise<void>
Remove a muted word
Labelers
(labelerDids: string[]) => void
Configure labeler DIDs for this agent instance
(prefs) => Promise<LabelDefinitions>
Fetch label definitions from configured labelers
Proxying
(value: string | null) => void
Configure AT Protocol proxy header
(serviceType, did) => Agent
Create a cloned agent with proxy configuration
Direct Lexicon Access
The agent provides direct access to all AT Protocol and Bluesky lexicons:RichText
TheRichText class handles rich text with proper UTF-8/UTF-16 conversion, facet detection for mentions and links, and text manipulation.
Constructor
string
required
The text content
Facet[]
Optional pre-existing facets (mentions, links, tags)
boolean
Whether to clean up excessive newlines
Properties
string
The UTF-16 encoded text string
number
The UTF-8 byte length of the text
number
The grapheme cluster length (visual characters)
Facet[] | undefined
The facets (mentions, links, tags) in the text
Facet Detection
(agent: Agent) => Promise<void>
Auto-detect and resolve facets (mentions and links)
() => void
Auto-detect facets without resolving mentions to DIDs
Text Manipulation
(insertIndex: number, text: string) => RichText
Insert text at a specific position, updating facet indices
(startIndex: number, endIndex: number) => RichText
Delete text in a range, updating facet indices
Iteration
() => Generator<RichTextSegment>
Iterate over text segments (plain text and facets)
Creating Posts with Rich Text
Rendering Rich Text
Moderation
The moderation system helps you apply content filtering, labeling, and user preferences.Moderating Content
Moderation Functions
(subject, opts) => ModerationDecision
Get moderation decisions for a post
(subject, opts) => ModerationDecision
Get moderation decisions for a profile
(subject, opts) => ModerationDecision
Get moderation decisions for a notification
Moderation UI Contexts
The moderation system supports different UI contexts:contentList- For feed viewscontentView- For detail viewscontentMedia- For media embedsavatar- For avatar imagesprofileList- For profile listsprofileView- For profile detail views
CredentialSession
TheCredentialSession class manages username/password-based authentication.
Constructor
URL
required
The AT Protocol service URL (e.g.,
https://bsky.social)typeof globalThis.fetch
Custom fetch implementation
AtpPersistSessionHandler
Callback for persisting session data
Methods
(opts: AtpAgentLoginOpts) => Promise<Response>
Login with identifier and password
() => Promise<void>
Logout and clear the session
(session: AtpSessionData) => Promise<Response>
Resume a previously saved session
(data) => Promise<Response>
Create a new account
AtpAgent (Legacy)
For backwards compatibility,AtpAgent is still available:
Type Exports
The package exports comprehensive TypeScript types for all Lexicons:Validation
All record types include validation methods:Configuration
Global Agent Configuration
Custom Headers
Custom Fetch
Error Handling
Related Packages
- @atproto/xrpc - Low-level XRPC client
- @atproto/oauth-client - OAuth authentication
- @atproto/lexicon - Lexicon schema validation
- @atproto/syntax - AT Protocol syntax utilities