DEV Community

Manipulate Threads with TypeScript

Are you playing Threads? I immediately registered on the first day (7/6).

I was surprised to see that a library that hacked this Threads and made it possible to use it with TypeScript appeared immediately.
https://github.com/junhoyeo/threads-api

So I played it quickly. First install the package. Use your favorite package manager, npm or yarn.

MacBook:~ $ mkdir threads_test
MacBook:~ $ cd threads_test/
MacBook:threads_test $ npm install threads-api

added 131 packages, and audited 132 packages in 25s

35 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Enter fullscreen mode Exit fullscreen mode

ts-node is necessary to run TypeScript directly, so install that as well.

MacBook:threads_test $ npm install --save-dev typescript ts-node

added 18 packages, and audited 150 packages in 9s

35 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Enter fullscreen mode Exit fullscreen mode

After doing that, copy the README source of threads-api and make some modifications.

import { ThreadsAPI } from 'threads-api';

// or in Deno 🦖:
// import { ThreadsAPI } from "npm:threads-api";

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

  const username = 'hardreggaecafe';

  // 👤 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));
  const posts = await threadsAPI.getUserProfileThreads(username, userID);
  console.log(JSON.stringify(posts));
  const replies = await threadsAPI.getUserProfileReplies(username, userID);
  console.log(JSON.stringify(replies));

  // 📖 Details for a specific thread
  const postID = await threadsAPI.getPostIDfromURL(
    'https://www.threads.net/t/CuWKab3S6mI/?igshid=NTc4MTIwNjQ2YQ==',
  );
  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();

Enter fullscreen mode Exit fullscreen mode

The username is my Threads account name, so it's self-explanatory, but how can I find out the URL of a specific thread in "Details for a specific thread"? Click on
the thread's paper plane icon to get it out.

You can copy the link.
If you copy from iPhone to Mac, you can paste it as it is, but you may write it down on something.

The result of running this

MacBookAirForNori:threads_test takamizawanoriaki$ ./node_modules/.bin/ts-node test.ts
{"is_private":false,"profile_pic_url":"https://scontent.cdninstagram.com/v/t51.2885-19/358184609_303246119031060_6603713084272654825_n.jpg?stp=dst-jpg_s150x150&_nc_ht=scontent.cdninstagram.com&_nc_cat=100&_nc_ohc=0Mhro1IvNEkAX8AvBXa&edm=APs17CUBAAAA&ccb=7-5&oh=00_AfAAc0zIYT0lU4esaOTUA20JBYVaSGL0y2CvXNbU3ewEzg&oe=64AD942E&_nc_sid=10d13b","username":"hardreggaecafe","hd_profile_pic_versions":
[{"height":320,"url":"https://scontent.cdninstagram.com/v/t51.2885-19/358184609_303246119031060_6603713084272654825_n.jpg?stp=dst-jpg_s320x320&_nc_ht=scontent.cdninstagram.com&_nc_cat=100&_nc_ohc=0Mhro1IvNEkAX8AvBXa&edm=APs17CUBAAAA&ccb=7-5&oh=00_AfByUOVOw6zzhZ5h043Gv8QIMncMZeh5vjf-kfuG4vuELg&oe=64AD942E&_nc_sid=10d13b","width":320},
{"height":640,"url":"https://scontent.cdninstagram.com/v/t51.2885-19/358184609_303246119031060_6603713084272654825_n.jpg?stp=dst-jpg_s640x640&_nc_ht=scontent.cdninstagram.com&_nc_cat=100&_nc_ohc=0Mhro1IvNEkAX8AvBXa&edm=APs17CUBAAAA&ccb=7-5&oh=00_AfA_foLUbR2XRrjSwZ0u_ACkuV4OXpndOgbKX50ReYNm6A&oe=64AD942E&_nc_sid=10d13b","width":640}],"is_verified":false,"biography":"ONODERA INTERNET SERVICE新規事業担当\na.k.a Vegewel / Good Good Mart CTO","biography_with_entities":null,"follower_count":95,"profile_context_facepile_users":null,"bio_links":[],"pk":"888911","full_name":"高見沢 徳明","id":null}
・・・

Enter fullscreen mode Exit fullscreen mode

Information about your profile and posted threads will appear.
Give it a try.

share

Top comments (0)