A little over three months ago, I wrote about how I added ATproto integration to my blog. In simple terms, all it took was a relatively straightforward call to the Bluesky service to register my blog as a publication, and then adding an extra call every time a new post is published. By storing the response I get back from Bluesky, I can embed it into the rendered HTML of the post and tie everything together.
Unfortunately, like most things cobbled together by hand, it broke. So today I have been fixing and improving things.
There were two problems with my initial implementation.
One, the JWT (JSON Web Token) used to authorise my post-publication call to Bluesky was only valid for two hours. I didn’t realise it at the time, but this meant only my initial write-up blog post was properly registered—everything I’ve published since then has failed because my auth token had expired.
And two, the identification data stored in my .env configuration was tied to my live publication, meaning that I couldn’t test the publishing flow in development without polluting my ATproto publication. (There are ways to delete items from your publication manually, but I’d rather have a properly set up development environment than workarounds.)
Tackling the second problem first, the simplest way to test in development is just to create a new Bluesky account that you only use during testing. Bluesky apparently used to have a developer environment at one point (the “Federation Developer Sandbox”) but it was shut down a few years ago in favour of just using the live network.
By creating a developer account, I was able to generate a new identifier (DID) and publication record, which I added to the copy of .env that exists on my local machine. So now when I run the import script, it’s the dummy/test publication that gets updated, not my real one.
I also added an entirely mocked interface for ATproto calls and responses, just in case I ever want to really dig into what’s being sent and received (or if Bluesky is down for whatever reason).
The other problem of expired auth tokens was slightly trickier. With the token stored in the site’s .env file, it wasn’t going to be possible to update it easily from within a function call — I didn’t want to add a bunch of file manipulation to my web app. Instead I got rid of the token altogether and replaced it with the authentication data for the Bluesky account, enabling me to add a getAtprotoAccessToken() function that requests a fresh token each time the import script runs.
So now everything runs as expected and should carry on doing so without needing to update anything; plus I can test and play with blog post imports when developing without needing to worry about what messy data I’m publishing to my ATproto PDS on Bluesky.
All of the code for the whole application is on GitHub, but these are the relevant commits: