[
  {
    "path": "README.md",
    "content": "# How Threads Works\n\nThis repository contains my notes and discoveries while reverse-engineering Threads app. Feel free to PR if you've found something new, or to build clients with this info (with credit ofc 😉).\n\n## Web (threads.net)\n\nThe web version of Threads is currently read-only, so not much can be learned about authentication or posting. It uses Meta's [Relay GraphQL Client](https://relay.dev) to talk to the backend (`threads.net/api/graphql`), which seems to be configured to disallow arbitrary queries. This leaves us limited to the existing queries found in the frontend's source:\n\n> **Note**\n> When querying the GraphQL backend, make sure to set an user-agent (seems like anything works here) and set the `x-ig-app-id` header to `238260118697367`.\n\n### Get profile data\n\n> Doc ID: `23996318473300828`\n> \n> Variables: `userID` (the user's ID)\n\n```bash\ncurl --request POST \\\n  --url https://www.threads.net/api/graphql \\\n  --header 'user-agent: threads-client' \\\n  --header 'x-ig-app-id: 238260118697367' \\\n  --header 'content-type: application/x-www-form-urlencoded' \\\n  --data 'variables={\"userID\":\"314216\"}' \\\n  --data doc_id=23996318473300828\n```\n\n### Get profile posts\n\n> Doc ID: `6232751443445612`\n> \n> Variables: `userID` (the user's ID)\n\n```bash\ncurl --request POST \\\n  --url https://www.threads.net/api/graphql \\\n  --header 'user-agent: threads-client' \\\n  --header 'x-ig-app-id: 238260118697367' \\\n  --header 'content-type: application/x-www-form-urlencoded' \\\n  --data 'variables={\"userID\":\"314216\"}' \\\n  --data doc_id=6232751443445612\n```\n\n### Get profile replies\n\n> Doc ID: `6307072669391286`\n> \n> Variables: `userID` (the user's ID)\n\n```bash\ncurl --request POST \\\n  --url https://www.threads.net/api/graphql \\\n  --header 'user-agent: threads-client' \\\n  --header 'x-ig-app-id: 238260118697367' \\\n  --header 'content-type: application/x-www-form-urlencoded' \\\n  --data 'variables={\"userID\":\"314216\"}' \\\n  --data doc_id=6307072669391286\n```\n\n### Get a post\n\n> Doc ID: `5587632691339264`\n> \n> Variables: `postID` (the post's ID)\n\n```bash\ncurl --request POST \\\n  --url https://www.threads.net/api/graphql \\\n  --header 'user-agent: threads-client' \\\n  --header 'x-ig-app-id: 238260118697367' \\\n  --header 'content-type: application/x-www-form-urlencoded' \\\n  --data 'variables={\"postID\":\"3138977881796614961\"}' \\\n  --data doc_id=5587632691339264\n```\n\n### Get a list of users who liked a post\n\n> Doc ID: `9360915773983802`\n> \n> Variables: `mediaID` (the post's ID)\n\n```bash\ncurl --request POST \\\n  --url https://www.threads.net/api/graphql \\\n  --header 'user-agent: threads-client' \\\n  --header 'x-ig-app-id: 238260118697367' \\\n  --header 'content-type: application/x-www-form-urlencoded' \\\n  --data 'variables={\"mediaID\":\"3138977881796614961\"}' \\\n  --data doc_id=9360915773983802\n```\n\n## Mobile Apps\n\n### Authentication\n\n> **Warning**\n> This endpoint currently only works for accounts without 2FA enabled.\n\nThe mobile apps use Meta's Bloks framework ([originally built for Instagram Lite](https://thenewstack.io/instagram-lite-is-no-longer-a-progressive-web-app-now-a-native-app-built-with-bloks/)) for authentication.\n\nThe bloks versioning ID for threads is `00ba6fa565c3c707243ad976fa30a071a625f2a3d158d9412091176fe35027d8`. Bloks also requires you to provide a device id (of shape `ios-RANDOM` | `android-RANDOM`, `RANDOM` being a random set of 13 chars).\n\n```bash\ncurl --request POST \\\n  --url 'https://i.instagram.com/api/v1/bloks/apps/com.bloks.www.bloks.caa.login.async.send_login_request/' \\\n  --header 'user-agent: Barcelona 289.0.0.77.109 Android' \\\n  --header 'sec-fetch-site: same-origin' \\\n  --header 'content-type: application/x-www-form-urlencoded; charset=UTF-8' \\\n  --data 'params={\"client_input_params\":{\"password\":\"$PASSWORD\",\"contact_point\":\"$USERNAME\",\"device_id\":\"$DEVICE_ID\"},\"server_params\":{\"credential_type\":\"password\",\"device_id\":\"$DEVICE_ID\"}}' \\\n  --data 'bloks_versioning_id=00ba6fa565c3c707243ad976fa30a071a625f2a3d158d9412091176fe35027d8'\n```\n\nThis request returns a big JSON payload. Your token will be immediately after the string `Bearer IGT:2:`, and should be 160 characters long.\n\n### Creating a text post\n\n```bash\ncurl --request POST \\\n  --url 'https://i.instagram.com/api/v1/media/configure_text_only_post/' \\\n  --header 'content-type: application/x-www-form-urlencoded; charset=UTF-8' \\\n  --header 'user-agent: Barcelona 289.0.0.77.109 Android' \\\n  --header 'authorization: Bearer IGT:2:$TOKEN' \\\n  --header 'sec-fetch-site: same-origin' \\\n  --data 'signed_body=SIGNATURE.{\"publish_mode\":\"text_post\",\"text_post_app_info\":\"{\\\"reply_control\\\":0}\",\"timezone_offset\":\"0\",\"source_type\":\"4\",\"_uid\":\"$USER_ID\",\"device_id\":\"$DEVICE_ID\",\"caption\":\"$POST_TEXT\",\"device\":{\"manufacturer\":\"OnePlus\",\"model\":\"ONEPLUS+A3003\",\"android_version\":26,\"android_release\":\"8.1.0\"}}'\n```\n\n## Misc\n\n### How to get a profile's id from their username?\n\nThreads uses the same ID system used by Instagram. The best approach to convert from username to id seems to be requesting the user's instagram page (`instagram.com/:username`) and manually parsing the response HTML. For other methods, see [this StackOverflow question](https://stackoverflow.com/questions/11796349/instagram-how-to-get-my-user-id-from-username).\n"
  }
]