
Unlocking the AT Protocol: Your Guide to Building Serverless Bluesky Apps with Cloudflare
The rise of decentralized social networks is creating exciting new opportunities for developers. At the forefront is the Authenticated Transfer Protocol (AT Protocol), the foundational technology behind Bluesky. This new ecosystem allows for portable identities and composable data, inviting developers to build unique applications on an open social graph.
But how do you build a robust, scalable application on this emerging protocol without getting bogged down in server management? The answer lies in combining the power of the AT Protocol with a modern serverless platform like Cloudflare.
This guide provides a blueprint for building your own serverless ATProto applications, leveraging the speed, scalability, and cost-efficiency of Cloudflare’s developer stack.
Why Go Serverless for Decentralized Social?
The AT Protocol empowers users by giving them control over their data, which is stored on a Personal Data Server (PDS). While your application doesn’t host user data, it still needs its own logic, state management, and a way to interact with the network.
This is where a serverless architecture shines. By using tools like Cloudflare Workers, D1 databases, and Queues, you can build applications that are:
- Infinitely Scalable: Automatically handles traffic spikes without any manual intervention.
- Cost-Effective: You only pay for the resources you actually use.
- High-Performance: Code runs on a global edge network, close to your users, ensuring low latency.
- Simple to Manage: Forget about patching servers or managing infrastructure. Focus on writing code.
A Blueprint for Your Serverless ATProto App
Let’s break down the core components and architecture required to build a functional application, such as a custom client or a status-tracking tool, on the AT Protocol using Cloudflare.
1. Handling User Authentication Securely
The first step for any application is authenticating the user. In the ATProto world, this is done using a user’s handle (e.g., your-handle.bsky.social
) and a dedicated app password.
The authentication flow looks like this:
- A user enters their handle and a pre-generated app password into your app’s interface.
- Your Cloudflare Worker receives these credentials.
- The Worker sends a request to the user’s PDS to create an authenticated session.
- If successful, the PDS returns session tokens (access and refresh tokens).
Actionable Security Tip: Never store a user’s app password. Once you receive the session tokens from the PDS, you should immediately discard the app password. Store the secure session tokens in your Cloudflare D1 database to make authenticated requests on the user’s behalf.
2. Storing Application State with Cloudflare D1
While the AT Protocol is stateless from your app’s perspective, your application will almost certainly need to manage its own state. This could include user preferences, application-specific data, or the session tokens mentioned above.
Cloudflare D1, a serverless SQL database, is the perfect solution for this. It provides the persistence of a traditional database without the management overhead. You can use it to store user sessions, link a user’s decentralized identifier (DID) to their profile in your app, and save any other data your service requires.
For a cleaner and more type-safe development experience, consider using a TypeScript Object-Relational Mapper (ORM) like Drizzle ORM. Drizzle integrates seamlessly with D1 and makes querying your database as simple as writing TypeScript functions.
3. Creating and Publishing Content to the Network
The core function of most social apps is posting content. To do this on the AT Protocol, your application needs to create a valid record and submit it to the user’s PDS.
The process, handled entirely by your Cloudflare Worker, involves several key steps:
- Receive Content: Your app’s frontend sends the post content (text, images, etc.) to your Worker endpoint.
- Format the Record: The Worker constructs a JSON object that conforms to the ATProto schema for a post. This includes the text, timestamp, and any rich content like links or images.
- Sign the Record: Using the user’s stored session credentials, the Worker authenticates with the PDS.
- Publish to PDS: The Worker sends the properly formatted and signed record to the user’s Personal Data Server, which then adds it to the user’s personal data repository and broadcasts it to the network.
4. Boosting Responsiveness with Cloudflare Queues
Interacting with external services like a PDS can sometimes be slow. If a user has to wait for this entire process to complete, the user experience can feel sluggish.
Cloudflare Queues allow you to offload non-critical tasks to run in the background. Instead of making your user wait, you can immediately return a “success” message while a background process handles the heavy lifting.
Here’s how you can use it to improve the posting experience:
- A user hits “post” in your app.
- The primary Cloudflare Worker validates the input and immediately returns a positive response to the user, making the UI feel instant.
- Simultaneously, this Worker sends a message containing the post data to a Cloudflare Queue.
- A separate background Worker listens to this queue, picks up the message, and performs the slower task of creating the record and publishing it to the PDS.
This asynchronous pattern ensures a fast, responsive frontend while guaranteeing that the background tasks are completed reliably.
The Modern Stack for Decentralized Development
Building on the AT Protocol is more accessible than ever with the right tools. A lightweight web framework like Hono, designed specifically for edge environments like Cloudflare Workers, can further streamline your API development.
By combining the decentralized promise of the AT Protocol with the scalable, performant, and easy-to-manage serverless tools from Cloudflare, developers can move quickly to build the next generation of social applications. This powerful stack removes the barriers of traditional infrastructure, allowing you to focus on creating innovative and valuable experiences for users in a new, open social web.
Source: https://blog.cloudflare.com/serverless-atproto/