Logo

dev-resources.site

for different kinds of informations.

Posting on Bluesky Social using Python in 1 minute

Published at
9/26/2023
Categories
bluesky
python
atprotocol
api
Author
incodable
Categories
4 categories in total
bluesky
open
python
open
atprotocol
open
api
open
Author
9 person written this
incodable
open
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

bluesky Article's
30 articles in total
Favicon
How I Built a Profitable Bluesky Tool Directory in 7 Days
Favicon
I feel like Elon killed Twitter and I plan to be more "developerly active " on BlueSky! https://bsky.app/profile/mahanaz.bsky.social But I want to get back to posting, sharing, and connecting, so that's why I'm here! :)
Favicon
How to add comment from BlueSky to static/vue/nuxt project
Favicon
How to Build a GenAI Bluesky Bot with Langflow, TypeScript, and Node.js
Favicon
✍️ Cross-Posting Astro Blog Posts to BlueSky Using GPT-4 🧠
Favicon
How decentralized is Bluesky really?
Favicon
Thoughts on bluesky Vs X for technical discussions?
Favicon
How to use GitHub to be verified on Bluesky
Favicon
How to post a link with embed card on Bluesky with JavaScript
Favicon
Fetching Liked Posts Using the Bluesky API
Favicon
Free Insights for any Bluesky 🦋 profile
Favicon
Custom Bluesky Handle on AWS with Terraform/OpenTofu
Favicon
How to Build a BlueSky RSS-like Bot with AWS Lambda and Terraform
Favicon
Skymood - Watch Bluesky's heartbeat through emojis in real-time 🌟
Favicon
The Journey of CDK.dev: From Static Site to Bluesky
Favicon
AT Protocol services
Favicon
Building my own Zero Dawn platform
Favicon
Creating a Bot for Bluesky Social
Favicon
How Web5 and Bluesky are Building the Next Layer of the Web - A Comparative Analysis
Favicon
So, you want to publish in Bluesky with a python program?
Favicon
Posting on Bluesky Social using Python in 1 minute
Favicon
How to Create Bluesky BOT using Dart and Firehose
Favicon
How to Update Your Profile from Dart/Flutter App using Bluesky API
Favicon
A nova rede social Bluesky
Favicon
Let's Post to Bluesky Social easily with Dart and Flutter
Favicon
Easily use Firehose API on Bluesky Social with Dart and Flutter
Favicon
Bringing custom domains back to Glitch and bots to Bluesky 🤖🌤️
Favicon
Build bots on Bluesky with Node.js and GitHub Actions
Favicon
What is Bluesky Social Network? And why are developers excited about it?
Favicon
We're on Bluesky ☁️

Featured ones: