Site icon Coding is Love

Threads API for developers for programmatic access

Threads, An Instagram app is all the rage now. Threads is an alternative to Twitter and everyone’s hoping on the new platform now. If you’re a developer and looking for Threads API then continue reading.

Threads API

There is no official developer API for Threads yet but a developer reverse-engineered the Threads API requests and created an unofficial API client.

The API client is an unofficial, Reverse-Engineered Node.js/TypeScript client for Meta’s Threads.

📦 Installation

yarn add threads-api
# or with npm
npm install threads-api
# or with pnpm
pnpm install threads-api

// or in Deno 🦖
import ThreadsAPI from 'npm:threads-api';

const threadsAPI = new ThreadsAPI.ThreadsAPI({});

Threads API Usage

It is as simple as importing the API client and using the client’s methods. If you want to access public data then you can directly use the API but if you want to access private methods such as publishing a thread or following someone then you should initialize the API with username and password

Checkout the examples below:

How to fetch threads user profile from API

import APIClient from "threads-api";

const main = async () => {
  const threadsAPI = new APIClient.ThreadsAPI();

  const username = "codingislove";

  // 👤 Details for a specific user
  const userID = await threadsAPI.getUserIDfromUsername(username);
  if (!userID) {
    return;
  }
  const user = await threadsAPI.getUserProfile(username, userID);
  console.log(JSON.stringify(user));
};

main();

Response:

{
  "is_private": false,
  "profile_pic_url": "https://scontent.cdninstagram.com/v/t51.2885-19/358193075_291108399947602_86677529474390528_n.jpg?stp=dst-jpg_s150x150&_nc_ht=scontent.cdninstagram.com&_nc_cat=110&_nc_ohc=qa3mujfMuOMAX_hdows&edm=APs17CUBAAAA&ccb=7-5&oh=00_AfBjrANoN_vgzct9EOFSe3NZACDpJijVQbwXnUsgUIkDuQ&oe=64B34485&_nc_sid=10d13b",
  "username": "codingislove",
  "hd_profile_pic_versions": null,
  "is_verified": false,
  "biography": "If you're a developer, you should follow this account 🤖",
  "biography_with_entities": null,
  "follower_count": 4,
  "profile_context_facepile_users": null,
  "bio_links": [{ "url": "https://codingislove.com/" }],
  "pk": "5635301639",
  "full_name": "Coding is Love",
  "id": null
}

✨ How to publish a thread using API

import APIClient from "threads-api";

const publishThread = async () => {
  const threadsAPI = new APIClient.ThreadsAPI({
    username: '_junhoyeo', // Your username
    password: 'PASSWORD', // Your password
  });

  await threadsAPI.publish({
    text: '🤖 Hello World',
  });
};

publishThread();

✨ Threads with Image

await threadsAPI.publish({
  text: '🤖 Threads with Image',
  image: 'https://github.com/junhoyeo/threads-api/raw/main/.github/cover.jpg',
});

✨ Threads with Link Attachment

await threadsAPI.publish({
  text: '🤖 Threads with Link Attachment',
  url: 'https://github.com/junhoyeo/threads-api',
});

✨ Reply to Other Threads

const parentURL = 'https://www.threads.net/t/CugF-EjhQ3r';
const parentPostID = await threadsAPI.getPostIDfromURL(parentURL); // or use `getPostIDfromThreadID`

await threadsAPI.publish({
  text: '🤖 Beep',
  link: 'https://github.com/junhoyeo/threads-api',
  parentPostID: parentPostID,
});

✨ Like/Unlike a Thread

const threadURL = 'https://www.threads.net/t/CugK35fh6u2';
const postIDToLike = await threadsAPI.getPostIDfromURL(threadURL); // or use `getPostIDfromThreadID`

// 💡 Uses current credentials
await threadsAPI.like(postIDToLike);
await threadsAPI.unlike(postIDToLike);

✨ Follow/Unfollow a User

const userIDToFollow = await threadsAPI.getUserIDfromUsername('junhoyeo');

// 💡 Uses current credentials
await threadsAPI.follow(userIDToFollow);
await threadsAPI.unfollow(userIDToFollow);

❤️‍🔥 Delete a Post

const postID = await threadsAPI.publish({
  text: '🤖 This message will self-destruct in 5 seconds.',
});

await new Promise((resolve) => setTimeout(resolve, 5_000));
await threadsAPI.delete(postID);

Supported features

Conclusion

Checkout the official repo for more options and updates

Also, read How to Send SMS from API or Backend 📲

Exit mobile version