Skip to content

Instantly share code, notes, and snippets.

@steinbring
Last active December 30, 2023 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steinbring/eedd750d9c9c25665ca80e3e6e8fc923 to your computer and use it in GitHub Desktop.
Save steinbring/eedd750d9c9c25665ca80e3e6e8fc923 to your computer and use it in GitHub Desktop.
You can use this node script to post to Bluesky through it's API. You just need to set the username and app password and execute the script locally by running 'node index.js'.
const https = require('https');
function createBlueskyPost() {
const BLUESKY_HANDLE = 'XXX';
const BLUESKY_APP_PASSWORD = 'YYY';
const data = JSON.stringify({
identifier: BLUESKY_HANDLE,
password: BLUESKY_APP_PASSWORD,
});
const options = {
hostname: 'bsky.social',
port: 443,
path: '/xrpc/com.atproto.server.createSession',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
},
};
const req = https.request(options, (res) => {
let body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
const session = JSON.parse(body);
postToBluesky(session.accessJwt, session.did);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(data);
req.end();
}
function postToBluesky(accessJwt, did) {
const post = {
$type: 'app.bsky.feed.post',
text: 'Playing around with the BlueSky API and Node',
createdAt: new Date().toISOString(),
};
const data = JSON.stringify({
repo: did,
collection: 'app.bsky.feed.post',
record: post,
});
const options = {
hostname: 'bsky.social',
port: 443,
path: '/xrpc/com.atproto.repo.createRecord',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
'Authorization': 'Bearer ' + accessJwt,
},
};
const req = https.request(options, (res) => {
let body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
console.log('Post created:', body);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(data);
req.end();
}
createBlueskyPost();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment