Created
September 10, 2023 18:08
-
-
Save vhxs/20f2fbc0da08c07317f9d935dbc1f765 to your computer and use it in GitHub Desktop.
Minimal example of using Bluesky's API to post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os | |
from datetime import datetime, timezone | |
# Fetch the current time | |
# Using a trailing "Z" is preferred over the "+00:00" format | |
now = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z") | |
# Required fields that each post must include | |
post = { | |
"$type": "app.bsky.feed.post", | |
"text": "Hey from a Python script using Bluesky's API. This is a post that isn't the usual Hello World post.", | |
"createdAt": now, | |
} | |
# create a session | |
resp = requests.post( | |
"https://bsky.social/xrpc/com.atproto.server.createSession", | |
json={ | |
"identifier": os.environ["BLUESKY_HANDLE"], | |
"password": os.environ["BLUESKY_APP_PASSWORD"] | |
}, | |
) | |
resp.raise_for_status() | |
session = resp.json() | |
# post! | |
resp = requests.post( | |
"https://bsky.social/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment