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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

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

Okay