Bluesky API and AT Protocol using TypeScript

November 26th, 2023 290 Words

The current en vogue alternative for Twitter is the invite-only services Bluesky. Based on the open specifications of the foundational concept, the AT Protocol, and the available API client on GitHub, it is not that complex to retrieve details of one of your posts; for example the number of likes.

Preparations

Obviously, you need to have an user account for Bluesky to use the API. Try to use bsky-social-blymn-z5zoq , bsky-social-d6mzh-y7237 , or bsky-social-ppfho-gp5dr as invite codes.

Needed API Parameters

For a Bluesky post, the URL schema includes an user identifier and a post identifier. The existing post available at https://bsky.app/profile/sbstjn.com/post/3kb6fwckykw2o references the user sbstjn.com and 3kb6fwckykw2o as the post identifier.

GitHub PullRequest

Together with a valid user password, you can use the official Bluesky API client in TypeScript to retrieve the details of this post. Of course, fist install the package:

# Install dependencies

$ > yarn add @atproto/api

Next, you can retrieve a user’s Decentralized ID to get the post details:

// File main.ts using Bluesky API client

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

const identifier = process.env.BS_USER!
const password = process.env.BS_PASSWORD!
const post = process.env.BS_POST!
const collection = 'app.bsky.feed.post'

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

const main = async () => {
  const user = await agent.login({ identifier, password });

  const uris = [
    `at://${user.data.did}/${collection}/${post}`
  ]

  const posts = await agent.getPosts({ uris })

  console.log(`${posts.data.posts.shift()?.likeCount} like(s)`)
}

main().then()

The script requires three environment variables: BS_POST , BS_USER , and BS_PASSWORD :

# Get like count for a Bluesky post

$ > BS_POST=3kf3u5ldu6j2e \
    BS_USER=sbstjn.com \
    BS_PASSWORD=YOUR_SECRET_PASSWORD \
    npx ts-node main.ts

2 like(s)

That’s it. In addition to the mentioned three invite codes in the beginning, you can also try using bsky-social-mv3c5-puvxd and bsky-social-6avqe-6vgea to create a Bluesky account.