Code Monkey home page Code Monkey logo

threads-api's Introduction

Threads API

NPM MIT License Prettier Code Formatting

Unofficial, Reverse-Engineered Node.js/TypeScript client for Meta's Threads.

threads-api in Action

cover

๐Ÿš€ Usage (Read)

Read: Public

import { ThreadsAPI } from 'threads-api';

// or in Deno ๐Ÿฆ–:
// import ThreadsAPI from "npm:threads-api";

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

  const username = '_junhoyeo';

  // ๐Ÿ‘ค Details for a specific user
  const userID = await threadsAPI.getUserIDfromUsername(username);
  if (!userID) {
    return;
  }
  const user = await threadsAPI.getUserProfile(userID);
  console.log(JSON.stringify(user));
  const posts = await threadsAPI.getUserProfileThreads(userID);
  console.log(JSON.stringify(posts));
  const replies = await threadsAPI.getUserProfileReplies(userID);
  console.log(JSON.stringify(replies));

  // ๐Ÿ“– Details for a specific thread
  const postID = threadsAPI.getPostIDfromURL(
    'https://www.threads.net/t/CuX_UYABrr7/?igshid=MzRlODBiNWFlZA==',
  );
  // or use `threadsAPI.getPostIDfromThreadID('CuX_UYABrr7')`
  if (!postID) {
    return;
  }
  const post = await threadsAPI.getThreads(postID);
  console.log(JSON.stringify(post.containing_thread));
  console.log(JSON.stringify(post.reply_threads));

  const likers = await threadsAPI.getThreadLikers(postID);
  console.log(JSON.stringify(likers));
};
main();

Read: Private(Auth Required)

๐Ÿ’ก Get Timeline
const { items: threads, next_max_id: cursor } = await threadsAPI.getTimeline();
console.log(JSON.stringify(threads));
๐Ÿ’ก Get Threads/Replies from a User (with pagination)
const { threads, next_max_id: cursor } = await threadsAPI.getUserProfileThreadsLoggedIn(userID);
console.log(JSON.stringify(threads));
const { threads, next_max_id: cursor } = await threadsAPI.getUserProfileRepliesLoggedIn(userID);
console.log(JSON.stringify(threads));
๐Ÿ’ก Get Followers/Followings of a User (with Pagination)
const { users, next_max_id: cursor } = await threadsAPI.getUserFollowers(userID);
console.log(JSON.stringify(users));
const { users, next_max_id: cursor } = await threadsAPI.getUserFollowings(userID);
console.log(JSON.stringify(users));

๐Ÿš€ Usage (Write)

Note
From v1.4.0, you can also call login to update your token and userID(for current credentials). Or you can just use the methods below, and they'll take care of the authentication automatically (e.g. if it's the first time you're using those).

New API (from v1.2.0)

โœจ Text Threads
import { ThreadsAPI } from 'threads-api';

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

  await threadsAPI.publish({
    text: '๐Ÿค– Hello World',
  });
};

main();

Writing Text Threads

๐Ÿ’ก TIP: Use the url field in ThreadsAPIPublishOptions to render Link Attachments(link previews).

โœจ Reply Control (from v1.4.6)
await threadsAPI.publish({
  text: '๐Ÿค– Threads with Reply Control',
  replyControl: 'accounts_you_follow', // 'everyone' | 'accounts_you_follow' | 'mentioned_only'
});
โœจ 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 = threadsAPI.getPostIDfromURL(parentURL); // or use `getPostIDfromThreadID`

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

Writing Text Threads

โœจ Quote a Thread (from v1.4.2)
const threadURL = 'https://www.threads.net/t/CuqbBI8h19H';
const postIDToQuote = threadsAPI.getPostIDfromURL(threadURL); // or use `getPostIDfromThreadID`

await threadsAPI.publish({
  text: '๐Ÿค– Quote a Thread',
  quotedPostID: postIDToQuote,
});
โœจ Like/Unlike a Thread (from v1.3.0)
const threadURL = 'https://www.threads.net/t/CugK35fh6u2';
const postIDToLike = threadsAPI.getPostIDfromURL(threadURL); // or use `getPostIDfromThreadID`

// ๐Ÿ’ก Uses current credentials
await threadsAPI.like(postIDToLike);
await threadsAPI.unlike(postIDToLike);
โœจ Follow/Unfollow a User (from v1.3.0)
const userIDToFollow = await threadsAPI.getUserIDfromUsername('junhoyeo');

// ๐Ÿ’ก Uses current credentials
await threadsAPI.follow(userIDToFollow);
await threadsAPI.unfollow(userIDToFollow);
โœจ Repost/Unrepost a Thread (from v1.4.2)
const threadURL = 'https://www.threads.net/t/CugK35fh6u2';
const postIDToRepost = threadsAPI.getPostIDfromURL(threadURL); // or use `getPostIDfromThreadID`

// ๐Ÿ’ก Uses current credentials
await threadsAPI.repost(postIDToRepost);
await threadsAPI.unrepost(postIDToRepost);
โœจ Delete a Post (from v1.3.1)
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);

Old API (Deprecated, Still works for backwards compatibility)

import { ThreadsAPI } from 'threads-api';

const main = async () => {
  const threadsAPI = new ThreadsAPI({
    username: 'jamel.hammoud', // Your username
    password: 'PASSWORD', // Your password
  });

  await threadsAPI.publish('๐Ÿค– Hello World');
};

main();

You can also provide custom deviceID (Default is android-${(Math.random() * 1e24).toString(36)}).

const deviceID = `android-${(Math.random() * 1e24).toString(36)}`;

const threadsAPI = new ThreadsAPI({
  username: 'jamel.hammoud',
  password: 'PASSWORD',
  deviceID,
});

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({});

Roadmap

  • โœ… Read public data
    • โœ… Fetch UserID(314216) via username(zuck)
    • โœ… Read timeline feed
    • โœ… Read User Profile Info
    • โœ… Read list of User Threads
      • โœ… With Pagination (If auth provided)
    • โœ… Read list of User Replies
      • โœ… With Pagination (If auth provided)
    • โœ… Fetch PostID(3140957200974444958) via ThreadID(CuW6-7KyXme) or PostURL(https://www.threads.net/t/CuW6-7KyXme)
    • โœ… Read Threads via PostID
    • โœ… Read Likers in Thread via PostID
    • โœ… Read User Followers
    • โœ… Read User Followings
  • โœ… Write data (i.e. write automated Threads)
    • โœ… Create new Thread with text
      • โœ… Make link previews to get shown
    • โœ… Create new Thread with a single image
    • ๐Ÿšง Create new Thread with multiple images
    • โœ… Reply to existing Thread
    • โœ… Quote Thread
    • โœ… Delete Thread
  • โœ… Friendships
    • โœ… Follow User
    • โœ… Unfollow User
  • โœ… Interactions
    • โœ… Like Thread
    • โœ… Unlike Thread
  • ๐Ÿดโ€โ˜ ๏ธ Restructure the project as a monorepo
    • ๐Ÿดโ€โ˜  Add Demo App with Next.js
    • ๐Ÿดโ€โ˜ ๏ธ Cool CLI App to run Threads in the Terminal

Projects made with threads-api

Add yours by just opening an pull request!

NPM MIT License Prettier Code Formatting

Embed Static Threads in your React/Next.js application. UI components for Meta's Threads. Powered by junhoyeo/threads-api.

cover

Demo

Warning
Vercel Deployment is currently sometimes unstable. ๐Ÿดโ€โ˜ ๏ธ

cover

๐Ÿดโ€โ˜ ๏ธ threads-api CLI (WIP)

To use the threads-api command line interface, run the following command:

$ npx threads-api --help
Usage: threads-api [command] [options]

Options:
  -v, --version                                                                   output the current version
  -h, --help                                                                      display help for command

Commands:
  help                                                                            display help for command
  getUserIDfromUsername|userid|uid|id <username>                                  det user ID from username
  getUserProfile|userprofile|uprof|up <username> <userId> [stringify]             get user profile
  getUserProfileThreads|uthreads|ut <username> <userId> [stringify]               get user profile threads
  getUserProfileReplies|userreplies|ureplies|ur <username> <userId> [stringify]   get user profile replies
  getPostIDfromURL|postid|pid|p <postURL>                                         get post ID from URL
  getThreads|threads|t <postId> [stringify]                                       get threads
  getThreadLikers|threadlikers|likers|l <postId> [stringify]                      get thread likers

Screenshot (84)

parameter demo
Default (_junhoyeo's account) _junhoyeo Badge
Custom Text and Colors Alternative Count Badge
Scale Badge Size https://www.threads.net/@zuck

License

MIT ยฉ Junho Yeo

If you find this project intriguing, please consider starring it(โญ) or following me on GitHub (I wouldn't say Threads). I code 24/7 and ship mind-breaking things on a regular basis, so your support definitely won't be in vain.

threads-api's People

Contributors

junhoyeo avatar billy-the-ape avatar mineru98 avatar sethusenthil avatar digital39999 avatar aleclarson avatar yssf-io avatar jamelhammoud avatar imgbot[bot] avatar cedriking avatar corard avatar aayushgithub avatar farizrifqi avatar sudarshanmg avatar 0xero7 avatar sooluh avatar alllz avatar nainish-rai avatar aminedjohar avatar

Stargazers

Omar Nagy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.