Skip to main content
OAuth is the recommended authentication method for AT Protocol applications. It provides better security, longer-lived tokens, and a standardized flow that works across different platforms.
OAuth replaces the deprecated app password authentication method. All new applications should use OAuth.

Why OAuth?

OAuth provides several advantages over app passwords:
  • Better security: No need to handle user passwords
  • Longer token lifetimes: Especially when using private key authentication
  • Standardized flow: Works the same across all AT Protocol servers
  • Automatic token refresh: Tokens refresh transparently
  • Revocation support: Users can revoke access without changing their password

Choosing an OAuth Client

AT Protocol provides OAuth clients for different environments:

Browser

@atproto/oauth-client-browser for single-page applications (SPAs)

Node.js

@atproto/oauth-client-node for backend services and desktop apps

Core

@atproto/oauth-client for custom implementations
If you have a backend server, use @atproto/oauth-client-node even for web apps. This provides better security and longer token lifetimes.

Browser OAuth Setup

For single-page applications without a backend.

Installation

Client Metadata

First, create and host your client metadata as a JSON file:
client-metadata.json
The client_id must be the URL where this metadata file is hosted. It must be publicly accessible.

Initializing the Client

Privacy Note: Using bsky.social as a handle resolver will send user handles and IP addresses to Bluesky. For better privacy, run your own PDS or handle resolver service.

Handle Resolver Options

You can use different handle resolvers:

Initialize on App Load

Call init() once when your app loads:

Sign In Flow

Using the Session

After authentication, create an Agent with the session:

Session Management

Listen for session changes:

Node.js OAuth Setup

For backend services, traditional web apps, and desktop applications.

Installation

Generate Keys

For backend services, you need to generate private keys:

Client Setup (Backend Service)

Express Integration

Native App Setup

For desktop apps (Electron, etc.):
Client metadata for native apps:
Native apps use token_endpoint_auth_method: "none" because they cannot safely store private keys. This results in shorter token lifetimes.

Advanced Features

Silent Sign-In

Attempt to sign in without user interaction:

Request Locking

Prevent concurrent token refreshes across multiple instances:

Custom Locales

Development and Testing

Localhost Development

AT Protocol OAuth supports loopback clients for development:
Loopback limitations:
  • Must use http://127.0.0.1:<port> or http://[::1]:<port>
  • No custom client metadata
  • Very short token lifetimes (typically 1 day)
  • No silent sign-in
For serious development, use a tunneling service like ngrok to expose your localhost and use full OAuth client metadata.

Error Handling

Best Practices

1

Use backend OAuth when possible

Backend OAuth provides better security and longer token lifetimes. Use browser OAuth only for pure client-side apps.
2

Implement session listeners

Always listen for session deletion events to handle token expiration gracefully.
3

Store sessions securely

Use appropriate storage for your platform:
  • Browser: IndexedDB (automatic with browser client)
  • Backend: Encrypted database
  • Desktop: OS keychain (e.g., keytar)
4

Handle offline users

Tokens can expire while users are offline. Implement proper error handling:
5

Use state parameter

Use the state parameter to maintain context across the OAuth flow:

Security Considerations

Never expose private keys: Keep your private keys secret and never commit them to version control.
Validate redirect URIs: Ensure your OAuth callback URL matches exactly what’s in your client metadata.
Use HTTPS in production: OAuth requires HTTPS for all redirect URIs except loopback addresses.

Migration from App Passwords

If you’re migrating from app password authentication:

Next Steps

Using the API

Learn how to use the Agent API with your OAuth session

Rich Text

Work with mentions and links in posts

Moderation

Implement content moderation

Development Setup

Set up your local development environment