Posting to Bluesky via the API from PHP – Part One

NOTE: This post is mainly about how I developed the php2Bluesky library. If you are looking for information on using the library in your own code please see here: https://github.com/williamsdb/php2Bluesky/

With all the uncertainty surrounding the future of X (née Twitter), I decided to take a look at Bluesky which somewhat ironically has its roots in Twitter where it was started as an internal project. Bluesky is still in beta and is invite-only. I worry about its long-term given that ultimately it too has to make money, something that Twitter has singularly failed to do. None of this, of course, affects the topic today which is posting to Bluesky via the API.

Basic Concepts

The first thing to say is that I am using the following library to actually post to BlueSky. What I am going to be looking at is how to format the parameters that you need to send to be able to:

  • posting text only
  • posting images
  • handling links
  • add in a web card

It is clear that Bluesky is much less polished than X which reflects the age of it but it does mean that you have to put in a lot more work doing things that work out the box on X.

Getting Started

Unlike X, the authentication for the Bluesky authentication is pretty simple. Head to Settings and then under “Advanced” select App passwords.

From the next page click the Add App Password button and give you app a name. The name is arbitary and really only seems to be for your reference.

When you click Create App Password the key you need to access the api will displayed. It will look something like ab1c-defg-hij2-k3l4. Make a note of this as once you click Done you won’t be able to view it again.

Connecting to BlueSky

As well as your App Password generated above you will also need your Bluesky handle to access. This looks something like: spokenlikeageek.bsky.social. Now you can create a connection to Bluesky.

 function bluesky_connect($handle, $password)
 {

   $connection = new BlueskyApi($handle, $password);
   return $connection;

 }

Making your First Post

Now that we have an active connection to Bluesky API we can build the parameters to send a post. The following is the basic array of parameters that we will be using and will build upon in the subsequent posts. You can take this as is and simply replace the ‘Hello World!’ text with what you would like to post.

    // build the arguments
    $args = [
      'collection' => 'app.bsky.feed.post',
      'repo' => $connection->getAccountDid(),
      'record' => [
        'text' => 'Hello World!',
        'langs' => ['en'],
        'createdAt' => date('c'),
        '$type' => 'app.bsky.feed.post',
      ],
    ];

Once you have this set-up you can post this to Bluesky by making the following call. Remember to check the response to make sure that it has been accepted but if it has then you will see the post in your Bluesky account.

 // send to bluesky
 return $connection->request('POST', 'com.atproto.repo.createRecord', $args);

Once you have the account access setup and the library loaded sending the post to Bluesky is relatively straight forward. Using what we have here we can build on this to add more functionality. In the next post we look at how to upload images to Bluesky posts via the API.

3 comments on “Posting to Bluesky via the API from PHP – Part One

  1. Thanks for doing this.. your connection code gave out an error, not sure why… and I forget the error. I just used the code from the github read me and it worked fine.. wondering if this tutorial needs an update?

Leave a Reply

Your email address will not be published. Required fields are marked *