Bluesky - markhowellsmead/helpers GitHub Wiki

References

Create post

import { AtpAgent } from '@atproto/api';

const IDENTIFIER = ''; // Your Bluesky handle
const PASSWORD = ''; // Your application password

const agent = new AtpAgent({
	service: 'https://bsky.social',
});

await agent.login({
	identifier: IDENTIFIER,
	password: PASSWORD,
});

await agent.post({
	text: 'Hello world! I posted this via the API.',
	createdAt: new Date().toISOString(),
});

Delete old posts

…which were created before a certain date. You might need to run this multiple times if there are a lot of entries to delete. Script created 8.1.2025.

import { AtpAgent } from '@atproto/api';

const IDENTIFIER = ''; // Your Bluesky handle
const PASSWORD = ''; // Your application password
const ACTOR = 'did:plc:…………'; // I got it from the source code of my profile page, there may be a better way!
const DATELIMIT = '2024-11-01T00:00:00Z';

const deleteOldestPosts = async () => {
    const agent = new AtpAgent({ service: 'https://bsky.social' });

    await agent.login({
        identifier: IDENTIFIER,
        password: PASSWORD,
    });

    let allPosts = [];
    let nextPage = '';
    let continueFetching = true;

    try {
        while (continueFetching) {
            const result = await agent.getAuthorFeed({
                actor: ACTOR,
                filter: 'posts_and_author_threads',
                limit: 100,
                cursor: nextPage,
            });

            const { data } = result;

            const { feed: postsArray, cursor: nextCursor } = data;
            allPosts = allPosts.concat(postsArray);

            // If there are no more pages, break the loop
            if (!nextCursor || postsArray.length === 0) {
                continueFetching = false;
                break;
            }

            nextPage = nextCursor;
        }

        let forDeletion = [];
        const dateLimit = new Date(DATELIMIT);

        allPosts.forEach(post => {
            // add post to forDeletion array if it is older than DATELIMIT
            let createdAt = new Date(post?.post?.record?.createdAt);
            if (createdAt < dateLimit) {
                //console.log('createdAt', createdAt);
                forDeletion.push(post);
            }
        });

        forDeletion.forEach(async entry => {
            console.log(`Deleting post with URI ${entry.post.uri}`);
            await agent.deletePost(entry.post.uri);
        });

        return allPosts;
    } catch (error) {
        console.error('Error fetching posts:', error.message);
    }
};

document.querySelector('#clicker').addEventListener('click', deleteOldestPosts);