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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay