Creating a TypeScript Bot for Bluesky: A Beginner's Guide
As the popularity of Bluesky continues to rise, it's becoming a go-to platform for social media enthusiasts looking for a fresh alternative to X (formerly Twitter).
One standout feature fueling this growth is Bluesky's free and open API, offering developers an inviting playground to build creative and functional bots without the paywall restrictions of X's API.
In this tutorial, we'll walk you through how to create a TypeScript bot for Bluesky, leveraging its developer-friendly ecosystem to bring your ideas to life. Whether you're looking to automate tasks, enhance engagement, or just experiment, this guide will get you started.
Account Setup
- Sign up for an account at Bluesky
- If you want your bot to be able to post I had to confirm my email address. When completed you should get a blue verification check on the account page next to your email.
- Now you will need an application password for your bot. Click add app password choose a name to remember it by and then be sure to paste it somewhere safe for now. We will need it later!
Environment Setup
If you have already developed in Node / TypeScript skip this section. If not you will need to setup the following to get your development environment right!
- IDE / Text Editor (Visual Studio Code)
- Node - Choose the LTS option if there are multiple. (Node.js)
- Terminal - If you are on Windows I recommend switching your default terminal in VSCode to Git Bash. (Git Bash)
Project Creation
You will need to create a new folder to store your project. Then open up that folder in Visual Studio Code by hitting file then open folder. (1)
Then open up your terminal by hitting Terminal - New Terminal. Note if you are using something other than Git Bash your commands might be slightly different. (2)
If you want to switch your terminal to something other than the default that can be done by hitting the icon in bottom right next to the + symbol. (3)
Installing Packages
We don't need may packages installed for this project but we need a few. We will use NPM to manage our packages and dependencies.
Run the command npm init -y in your terminal to initialize a default project.
Install the required packages by running npm i @atproto/api dotenv in your terminal.
Install TypeScript and its Node.js types by running npm install typescript @types/node --save-dev in your terminal.
Run npx tsc --init to initialize a TypeScript configuration file in your project.
Edit tsconfig.json
Open up the tsconfig.json file and we need to make a few changes.
Set target to ESNext "target": "ESNext",
Set module to NodeNext "module": "NodeNext",
Set moduleResolution to nodenext "moduleResolution": "nodenext",
Create a .env file
Create a new file in the root of the project. Name it .env
Inside the file we need to variables. BLUESKY_EMAIL and BLUESKY_PASSWORD. Make sure to use the application password we generated and not your every day login password.
Here is a sample file please use your own info for it to work correctly. I had to use my email address and not my username as well.
BLUESKY_EMAIL=YourEmail@gmail.com
BLUESKY_PASSWORD=P3zd-m7jk-dzeq-i6l0
Create your program file. Ours will be index.js
Create a new file in the root of the project. Name it index.js
Program is very simple it goes through the following steps
- Import libraries. (Lines 1 and 2)
- Load our variables from our .env file. (Lines 3)
- Create our agent. We will run our BlueSky bot commands from this agent. (Lines 4 - 6)
- Login. (Lines 8 - 11)
- Hello world post! (Lines 12 - 16)
import { AtpAgent } from "@atproto/api";
import dotenv from "dotenv";
dotenv.config();
const agent = new AtpAgent({
service: "https://bsky.social",
});
await agent.login({
identifier: process.env.BLUESKY_USERNAME,
password: process.env.BLUESKY_PASSWORD,
});
await agent.post({
text: "Hello world. I will now post.",
createdAt: new Date().toISOString(),
reply: undefined,
});
Final Bot Thought!
This should get your started in creating bots related to BlueSky. Please use your new found power responsibly.