DEV Community

Incodable
Incodable

Posted on

4 1

Posting on Bluesky Social using Python in 1 minute

Here I will explain quickly how to make a post on Bluesky using its underlying technology (ATProtocol).

First, you need to implement a method to login to Bluesky by creating a session using your regular username and password:

def bsky_login_session(host: str, handle: str, password: str) -> dict:
    resp = requests.post(
        host + "/xrpc/com.atproto.server.createSession",
        json={"identifier": handle, "password": password},
    )
    resp.raise_for_status()
    return resp.json()
Enter fullscreen mode Exit fullscreen mode

Then you will create a post. Let's say a simple post containing a text and an image:

def create_image_post(host: str, handle: str, password: str, text: str, image_url: str):
    session = bsky_login_session(host, handle, password)

    now = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")

    post = {
        "$type": "app.bsky.feed.post",
        "text": text,
        "createdAt": now,
        "embed": upload_images(host, session["accessJwt"], image_url)
    }

    # Post payload
    resp = requests.post(
        host + "/xrpc/com.atproto.repo.createRecord",
        headers={"Authorization": "Bearer " + session["accessJwt"]},
        json={
            "repo": session["did"],
            "collection": "app.bsky.feed.post",
            "record": post,
        },
    )
    resp.raise_for_status()
Enter fullscreen mode Exit fullscreen mode

Now let's test it:

if __name__ == '__main__':
    host = 'https://bsky.social'
    handle = 'yourhandel.bsky.social'
    password = 'your password'

    create_image_post(host, handle, password, 'Cats are awesome', 'images/my_cat.jpg')
Enter fullscreen mode Exit fullscreen mode

As simple as that! You could make your own personalized Bluesky bots which post stuff on the site. It's pretty straightforward and easy to set up, isn't it?

Happy coding
Al

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay