How to Scrape Threads Posts with Kotlin

Extract post data from Threads

📱 Using Kotlin

Overview

Learn how to scrape Threads posts using Kotlin. This comprehensive guide will walk you through the entire process, from setup to implementation.

What You'll Learn

  • • Setting up your development environment
  • • Installing the required HTTP client
  • • Authenticating with the ScrapeCreators API
  • • Making requests to Threads
  • • Handling responses and errors
  • • Best practices for production use

What You'll Get

  • • Access to posts data
  • • JSON formatted responses
  • • Real-time data access
  • • Scalable solution
  • • Error handling patterns
  • • Performance optimization tips

Prerequisites

1. API Key

First, you'll need a ScrapeCreators API key to authenticate your requests.

Sign up at app.scrapecreators.com to get your free API key with 100 requests.

2. Development Environment

Make sure you have the following installed:

  • Kotlin and its dependencies
  • • A code editor (VS Code, Sublime, etc.)
  • • Basic understanding of API requests
  • • Command line interface access

Step 1: Install HTTP Client

OkHttp is an HTTP client for Kotlin/Java

gradle
implementation "com.squareup.okhttp3:okhttp:4.9.3"

Step 2: API Implementation

Now let's make a request to the Threads API using Kotlin. Replace YOUR_API_KEY with your actual API key.

Kotlin
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.net.URI
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

class Scraper {
    companion object {
        private const val API_KEY = "YOUR_API_KEY"
        private const val BASE_URL = "https://api.scrapecreators.com"
        private const val ENDPOINT_PATH = "/v1/threads/post"
        
        @JvmStatic
        fun main(args: Array<String>) {
            try {
                val result = scrape()
                println("Response: $result")
            } catch (e: Exception) {
                println("Error: ${e.message}")
            }
        }
        
        fun scrape(): String {
            val client = HttpClient.newHttpClient()
            
            // Build query parameters
            val params = mapOf(
                "url" to "https://www.threads.net/@trendspider/post/DIU8naHS6q_",
                "trim" to "false"
            )
            
            val queryString = params.entries.joinToString("&") { (key, value) ->
                "${key}=${URLEncoder.encode(value, StandardCharsets.UTF_8)}"
            }
            
            val url = "${BASE_URL}${ENDPOINT_PATH}?${queryString}"
            
            val request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("x-api-key", API_KEY)
                .header("Accept", "application/json")
                .GET()
                .build()
            
            val response = client.send(request, HttpResponse.BodyHandlers.ofString())
            
            if (response.statusCode() == 200) {
                return response.body()
            } else {
                throw RuntimeException("HTTP ${response.statusCode()}: ${response.body()}")
            }
        }
    }
}

Step 3: Testing Your Code

API Parameters

This endpoint accepts the following parameters:

urlRequired(string)

The URL of the post to get

Example: https://www.threads.net/@trendspider/post/DIU8naHS6q_

trimOptional(boolean)

Set to true for a trimmed down version of the response

Example: false

Run Your Code

Execute your script to test the API connection. You should see a JSON response with Threads posts data.

✅ Success: You should receive a structured JSON response containing the requested data.

Expected Response

Here's an example of the JSON response you'll receive:

Sample Response
{
  "success": true,
  "post": {
    "id": "3608775792320555711_63069450921",
    "pk": "3608775792320555711",
    "user": {
      "friendship_status": null,
      "pk": "63069450921",
      "profile_pic_url": "https://scontent-sjc3-1.cdninstagram.com/v/t51.2885-19/358000025_988841669212235_3183384987691331022_n.jpg?stp=dst-jpg_s150x150_tt6&_nc_ht=scontent-sjc3-1.cdninstagram.com&_nc_cat=1&_nc_oc=Q6cZ2QHENKKl6jQlD4P2ykD1kdjgJzgIifGHZXsUsCgsnHpH1p2-XeWT1cLGjrqVGxoPbnE&_nc_ohc=WkOBPuV5vCQQ7kNvwFgz1og&_nc_gid=szsnblWVDT9LOMmPYXt2zQ&edm=APs17CUBAAAA&ccb=7-5&oh=00_AfEaCFBnIBaZajCxvXpwUmetqErk-ja5mM8XavcDkiYudA&oe=67FFBF55&_nc_sid=10d13b",
      "username": "trendspider",
      "id": "63069450921",
      "transparency_label": null,
      "transparency_product": null,
      "transparency_product_enabled": false,
      "is_verified": false,
      "text_post_app_is_private": false,
      "has_onboarded_to_text_post_app": true
    },
    "text_post_app_info": {
      "show_header_follow": false,
      "is_markup": false,
      "custom_feed_preview_info": null,
      "link_preview_attachment": null,
      "linked_inline_media": null,
      "special_effects_enabled_str": "",
      "text_fragments": {
        "fragments": [
          {
            "fragment_type": "plaintext",
            "link_fragment": null,
            "mention_fragment": null,
            "plaintext": "NVDA 🟢 = 36x P/E\n\nNvidia at $110 today carries the same premium as Nvidia at $15 in 2022.",
            "tag_fragment": null,
            "linkified_web_url": null
          }
        ]
      },
      "reshare_count": 1,
      "direct_reply_count": 6,
      "repost_count": 1,
      "quote_count": 0,
      "share_info": {
        "quoted_attachment_author_attribution_allowed": true,
        "quoted_attachment_post_unavailable": false,
        "quoted_attachment_post": null,
        "quoted_post": null,
        "reposted_post": null
      },
      "reply_to_author": null,
      "reply_control": "everyone",
      "private_reply_partner": null,
      "self_thread_count": null,
      "tag_header": {
        "display_name": "NVDA",
        "id": "18402789505032695"
      },
      "root_post_author": null,
      "pinned_post_info": {
        "is_pinned_to_parent_post": false,
        "is_pinned_to_profile": false
      },
      "related_trends_info": null,
      "is_reply": false,
      "is_post_unavailable": false,
      "post_unavailable_reason": null,
      "hush_info": null,
      "can_private_reply": false,
      "can_reply": false
    },
    "is_paid_partnership": null,
    "audio": null,
    "caption": {
      "text": "NVDA 🟢 = 36x P/E\n\nNvidia at $110 today carries the same premium as Nvidia at $15 in 2022.",
      "pk": "18109768954486098",
      "has_translation": null
    },
    "caption_is_edited": false,
    "transcription_data": null,
    "carousel_media": null,
    "code": "DIU8naHS6q_",
    "image_versions2": {
      "candidates": [
        {
          "height": 790,
          "url": "https://scontent-sjc3-1.cdninstagram.com/v/t51.2885-15/489773269_18495263140062925_1435854593842334866_n.jpg?stp=dst-jpg_e15_tt6&efg=eyJ2ZW5jb2RlX3RhZyI6InRocmVhZHMuRkVFRC5pbWFnZV91cmxnZW4uMTA3OXg3OTAuc2RyLmY3NTc2MS5kZWZhdWx0X2ltYWdlIn0&_nc_ht=scontent-sjc3-1.cdninstagram.com&_nc_cat=101&_nc_oc=Q6cZ2QHENKKl6jQlD4P2ykD1kdjgJzgIifGHZXsUsCgsnHpH1p2-XeWT1cLGjrqVGxoPbnE&_nc_ohc=DWmq_LhK8ooQ7kNvwH3FuBe&_nc_gid=szsnblWVDT9LOMmPYXt2zQ&edm=APs17CUBAAAA&ccb=7-5&ig_cache_key=MzYwODc3NTc5MjMyMDU1NTcxMQ%3D%3D.3-ccb7-5&oh=00_AfGt93ejDNUbdHZMQ3yRJis7UnkYXfc4BQxHGvRH5flHEg&oe=67FFB4BA&_nc_sid=10d13b",
          "width": 1079
        }
      ]
    },
    "original_height": 790,
    "original_width": 1079,
    "accessibility_caption": "May be an image of ‎text that says '‎TrendSpider Spider NVDA, Daily, Candles WVDA,Daily,Candleschart chart nVIDIA @ NVDADAILYCHART NVDA DAILY CHART 110.56 P/ERatio(Trailing),Daily Ratio Ratio(Trailing),Da (Trailing) Daily 153xP/E P/E 153x ب 96x 96xP/E P/E 91xP/E 79xP/E 71xP/E 67xP/E 57xP/E hwn ยุรันม-- سي 36P/E 36. 6.952 952‎'‎",
    "video_versions": null,
    "has_audio": null,
    "media_type": 1,
    "caption_add_on": null,
    "giphy_media_info": null,
    "media_overlay_info": null,
    "sharing_friction_info": {
      "should_have_sharing_friction": false,
      "sharing_friction_payload": null
    },
    "like_count": 28,
    "metaPlace": null,
    "meta_place": null,
    "gen_ai_detection_method": null,
    "taken_at": 1744419624,
    "organic_tracking_token": "eyJ2ZXJzaW9uIjo1LCJwYXlsb2FkIjp7ImlzX2FuYWx5dGljc190cmFja2VkIjp0cnVlLCJ1dWlkIjoiZjg1MDQ3MWU2ZWQzNDgxMTg2M2ZiMmUyYTIyNzE0YTIzNjA4Nzc1NzkyMzIwNTU1NzExIn0sInNpZ25hdHVyZSI6IiJ9",
    "logging_info_token": null,
    "like_and_view_counts_disabled": false
  },
  "comments": [
    {
      "id": "3623864305260810977_63153846014",
      "pk": "3623864305260810977",
      "user": {
        "friendship_status": null,
        "pk": "63153846014",
        "profile_pic_url": "https://scontent-det1-1.cdninstagram.com/v/t51.2885-19/487938413_586751564386772_308932070682954511_n.jpg?stp=dst-jpg_s150x150_tt6&_nc_ht=scontent-det1-1.cdninstagram.com&_nc_cat=108&_nc_oc=Q6cZ2QFYEfpxZTcQhZTyV95qWqHgk3p4LEFPLr8l8yaZwOGP5hqqDb0Kyz_sN3iJJzb40oQ&_nc_ohc=akb7Cbt_r9UQ7kNvwGU6P3C&_nc_gid=OfmrVcoHB4ICHVuWQqHDcA&edm=APs17CUBAAAA&ccb=7-5&oh=00_AfEj8UU9jnyu2kQZeNPqt7e5bOORgAc6kgxv1zrDh8L_Ng&oe=681B44F9&_nc_sid=10d13b",
        "username": "gringo.ronin",
        "id": "63153846014",
        "transparency_label": null,
        "transparency_product": null,
        "transparency_product_enabled": false,
        "is_verified": false,
        "text_post_app_is_private": false,
        "has_onboarded_to_text_post_app": true
      },
      "text_post_app_info": {
        "show_header_follow": false,
        "is_markup": false,
        "custom_feed_preview_info": null,
        "link_preview_attachment": null,
        "linked_inline_media": null,
        "special_effects_enabled_str": "",
        "text_fragments": {
          "fragments": [
            {
              "fragment_type": "plaintext",
              "link_fragment": null,
              "mention_fragment": null,
              "plaintext": "UNLIMITED FIREPOWAHHH",
              "tag_fragment": null,
              "linkified_web_url": null,
              "styling_info": null
            }
          ]
        },
        "reshare_count": null,
        "direct_reply_count": 0,
        "repost_count": 0,
        "quote_count": 0,
        "share_info": {
          "quoted_attachment_author_attribution_allowed": true,
          "quoted_attachment_post_unavailable": false,
          "quoted_attachment_post": null,
          "quoted_post": null,
          "reposted_post": null
        },
        "reply_to_author": {
          "username": "culturecrave",
          "id": "63097619970"
        },
        "reply_control": "everyone",
        "private_reply_partner": null,
        "self_thread_count": null,
        "tag_header": null,
        "root_post_author": {
          "pk": "63097619970",
          "id": "63097619970"
        },
        "pinned_post_info": {
          "is_pinned_to_parent_post": false,
          "is_pinned_to_profile": false
        },
        "related_trends_info": null,
        "is_reply": true,
        "is_post_unavailable": false,
        "post_unavailable_reason": null,
        "hush_info": null,
        "can_private_reply": false,
        "can_reply": false
      },
      "is_paid_partnership": null,
      "audio": null,
      "caption": {
        "text": "UNLIMITED FIREPOWAHHH",
        "pk": "18094020853595608",
        "has_translation": null
      },
      "caption_is_edited": false,
      "transcription_data": null,
      "carousel_media": null,
      "code": "DJKjWK1SdLh",
      "image_versions2": {
        "candidates": []
      },
      "original_height": 612,
      "original_width": 612,
      "accessibility_caption": null,
      "video_versions": null,
      "has_audio": null,
      "media_type": 19,
      "caption_add_on": null,
      "giphy_media_info": null,
      "media_overlay_info": null,
      "sharing_friction_info": {
        "should_have_sharing_friction": false,
        "sharing_friction_payload": null
      },
      "like_count": 11,
      "metaPlace": null,
      "meta_place": null,
      "gen_ai_detection_method": null,
      "taken_at": 1746218310,
      "organic_tracking_token": "eyJ2ZXJzaW9uIjo1LCJwYXlsb2FkIjp7ImlzX2FuYWx5dGljc190cmFja2VkIjp0cnVlLCJ1dWlkIjoiM2M1ZDI4NWNmZDg4NDM1MWJhM2JjMGZkNzRhNDlhY2QzNjIzODY0MzA1MjYwODEwOTc3In0sInNpZ25hdHVyZSI6IiJ9",
      "logging_info_token": null,
      "like_and_view_counts_disabled": false
    }
  ],
  "relatedPosts": [
    {
      "id": "3608818157045893190_63438622220",
      "pk": "3608818157045893190",
      "user": {
        "friendship_status": null,
        "pk": "63438622220",
        "profile_pic_url": "https://scontent-sjc3-1.cdninstagram.com/v/t51.2885-19/90841686_1704287249714138_8421203722285088768_n.jpg?stp=dst-jpg_s150x150_tt6&_nc_ht=scontent-sjc3-1.cdninstagram.com&_nc_cat=1&_nc_oc=Q6cZ2QGKmxHCQeEziyqUxwpwsUM42533onLy6ChINs5Zae8ofWiiSfVzZ8pxcN9tOv6C_Kw&_nc_ohc=T22RbvNM4mMQ7kNvwHskFAE&_nc_gid=szsnblWVDT9LOMmPYXt2zQ&edm=APs17CUBAAAA&ccb=7-5&oh=00_AfG9PqzPXDCvZ46dcLMI8NU6iFMaUrbt23yOEBSctT0XCQ&oe=67FF9BC0&_nc_sid=10d13b",
        "username": "jim.chuong",
        "id": "63438622220",
        "transparency_label": null,
        "transparency_product": null,
        "transparency_product_enabled": false,
        "is_verified": true,
        "text_post_app_is_private": false,
        "has_onboarded_to_text_post_app": true
      },
      "text_post_app_info": {
        "show_header_follow": false,
        "is_markup": false,
        "custom_feed_preview_info": null,
        "link_preview_attachment": null,
        "linked_inline_media": null,
        "special_effects_enabled_str": "",
        "text_fragments": {
          "fragments": [
            {
              "fragment_type": "plaintext",
              "link_fragment": null,
              "mention_fragment": null,
              "plaintext": "There is no reason to fear a market collapse.\n\nIn the End nobody leaves with anything anyway.\n\nEnjoy the process.",
              "tag_fragment": null,
              "linkified_web_url": null
            }
          ]
        },
        "reshare_count": null,
        "direct_reply_count": 0,
        "repost_count": 0,
        "quote_count": 0,
        "share_info": {
          "quoted_attachment_author_attribution_allowed": true,
          "quoted_attachment_post_unavailable": false,
          "quoted_attachment_post": null,
          "quoted_post": null,
          "reposted_post": null
        },
        "reply_to_author": null,
        "reply_control": "everyone",
        "private_reply_partner": null,
        "self_thread_count": null,
        "tag_header": null,
        "root_post_author": null,
        "pinned_post_info": {
          "is_pinned_to_parent_post": false,
          "is_pinned_to_profile": false
        },
        "related_trends_info": null,
        "is_reply": false,
        "is_post_unavailable": false,
        "post_unavailable_reason": null,
        "hush_info": null
      },
      "is_paid_partnership": null,
      "audio": null,
      "caption": {
        "text": "There is no reason to fear a market collapse.\n\nIn the End nobody leaves with anything anyway.\n\nEnjoy the process.",
        "pk": "18051538439260564",
        "has_translation": null
      },
      "caption_is_edited": false,
      "transcription_data": null,
      "carousel_media": null,
      "code": "DIVGP5Vs0hG",
      "image_versions2": {
        "candidates": []
      },
      "original_height": 612,
      "original_width": 612,
      "accessibility_caption": null,
      "video_versions": null,
      "has_audio": null,
      "media_type": 19,
      "caption_add_on": null,
      "giphy_media_info": null,
      "media_overlay_info": null,
      "sharing_friction_info": {
        "should_have_sharing_friction": false,
        "sharing_friction_payload": null
      },
      "like_count": 3,
      "metaPlace": null,
      "meta_place": null,
      "gen_ai_detection_method": null,
      "taken_at": 1744424670,
      "organic_tracking_token": "eyJ2ZXJzaW9uIjo1LCJwYXlsb2FkIjp7ImlzX2FuYWx5dGljc190cmFja2VkIjp0cnVlLCJ1dWlkIjoiMTE2OTU0YmU4MGI5NGU4NGJkYjg5N2RlNzBlMDI4NjUzNjA4ODE4MTU3MDQ1ODkzMTkwIn0sInNpZ25hdHVyZSI6IiJ9",
      "logging_info_token": null,
      "like_and_view_counts_disabled": false
    }
  ]
}

Verify Response Structure

Check that your response includes the expected fields:

  • success(boolean)
  • post(object)
  • comments(object)
  • relatedPosts(object)

Best Practices

1

Error Handling

Implement comprehensive error handling and retry logic for failed requests. Log errors properly for debugging.

2

Caching

Cache responses when possible to reduce API calls and improve performance. Consider data freshness requirements.

3

Security

Never expose your API key in client-side code. Use environment variables and secure key management practices.

Performance Tips

Batch Requests

When scraping multiple posts, consider batching requests to maximize throughput while staying within rate limits.

Async Processing

Use asynchronous processing in Kotlin to handle multiple requests concurrently and improve overall performance.

Common Use Cases

Market Research

Analyze Threads posts to understand market trends, competitor analysis, and audience insights.

Content Analytics

Track performance metrics, engagement rates, and content trends across Threads posts.

Lead Generation

Identify potential customers and business opportunities throughThreads data analysis.

Troubleshooting

Common Errors

401 Unauthorized

Check your API key is correct and properly formatted in the x-api-key header.

402 Payment Required

You ran out of credits and need to buy more.

404 Not Found

The resource might not exist or be private.

500 Server Error

Temporary server issue. Implement retry logic with exponential backoff.

Frequently Asked Questions

How much does it cost to scrape Threads posts?

ScrapeCreators offers 100 free API calls to get started. After that, pricing starts at $10 for 5k requests with volume discounts available.

Is it legal to scrape Threads data?

Scraping publicly available data is fair game, and we only collect public data. So anything that you can see in an incognito browser is what we collect.

How fast can I scrape Threads posts?

There is no rate limit! So you can scrape as fast as you want!

What data format does the API return?

All API responses are returned in JSON format, making it easy to integrate with any programming language or application.

Can I use this with other Kotlin frameworks?

Yes! This tutorial focuses on core Kotlin HTTP concepts that work with any framework. The API calls remain the same regardless of your specific Kotlin setup.

How do I handle large datasets?

For large datasets, implement pagination, use streaming responses where available, and consider storing data in a database for efficient querying.

Related Tutorials

Ready to Start Scraping?

Get started with 100 free API calls. No credit card required.