Tutorial

How to Scrape X (Twitter) with JavaScript

Step-by-step guide to scraping X (Twitter) search results using JavaScript and the Scavio API. Get post snippets, author handles, timestamps as structured JSON.

X (Twitter) contains valuable data — post snippets, author handles, timestamps, engagement signals, and more. Scraping this data directly means dealing with anti-bot detection, CAPTCHAs, IP rotation, and constantly breaking selectors. The Scavio API handles all of that and returns clean, structured JSON from a single POST request.

This tutorial shows you how to scrape X (Twitter) using JavaScript and the Scavio API. By the end, you will have a working JavaScript script that fetches real-time X (Twitter) data and parses the results.

Prerequisites

  • JavaScript installed on your machine
  • A Scavio API key (free tier includes 500 credits/month — no credit card required)

Step 1: Install Dependencies

fetch is built into JavaScript, so there is nothing to install.

Bash
# No installation needed — fetch is built into Node.js 18+

Step 2: Make Your First X (Twitter) Search

Send a POST request to the Scavio X (Twitter) API endpoint with your query. The API returns structured JSON with post snippets, author handles, timestamps, and more.

JavaScript
const API_KEY = "your_scavio_api_key";

const response = await fetch("https://api.scavio.dev/api/v1/search", {
  method: "POST",
  headers: {
    "x-api-key": API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ query }),
});

const data = await response.json();
for (const result of data.organic_results?.slice(0, 5) ?? []) {
  console.log(`${result.position}. ${result.title}`);
  console.log(`   ${result.link}\n`);
}

Step 3: Example Response

The API returns structured JSON. Here is an example response for a X (Twitter) search:

JSON
{
  "search_metadata": { "status": "success" },
  "organic_results": [
    {
      "position": 1,
      "title": "@founder on X: Just shipped our AI agent framework",
      "link": "https://x.com/founder/status/1234567890",
      "snippet": "2.4K retweets · 18K likes · AI agents are eating every vertical in 2026..."
    }
  ]
}

Every field is structured and typed — no HTML parsing, no CSS selectors, no regex extraction. Your JavaScript code can access any field directly.

Step 4: Full Working Example

Here is a complete, runnable JavaScript script that searches X (Twitter) and prints the results:

JavaScript
/**
 * Scrape X (Twitter) search results using Scavio API.
 * Returns structured JSON with post snippets, author handles, timestamps, and more.
 */
const API_KEY = "your_scavio_api_key";

async function searchXTwitter(query) {
  const response = await fetch("https://api.scavio.dev/api/v1/search", {
    method: "POST",
    headers: {
      "x-api-key": API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ query }),
  });

  if (!response.ok) {
    throw new Error(`Scavio API error: ${response.status}`);
  }

  return response.json();
}

const results = await searchXTwitter("site:x.com AI agents 2026");
console.log(JSON.stringify(results, null, 2));

Why Use Scavio Instead of Scraping X (Twitter) Directly?

  • No proxy management. Direct scraping requires rotating proxies to avoid IP bans. Scavio handles all of this server-side.
  • No CAPTCHA solving. X (Twitter) aggressively blocks automated requests. Scavio returns clean data every time.
  • Structured JSON output. No HTML parsing or CSS selector maintenance. Get typed, consistent data from every request.
  • Multi-platform in one API. Search Google, Amazon, YouTube, and Walmart from the same API key with the same authentication pattern.
  • Free tier included. 500 credits/month with no credit card required. Each search costs 1 credit.

Frequently Asked Questions

Scraping publicly available data from X (Twitter) is generally legal, but you should review X (Twitter)'s Terms of Service. Using the Scavio API avoids the legal gray areas of direct scraping since Scavio handles all data collection through proper channels and returns structured results via API.

Direct scraping of X (Twitter) requires managing proxies, CAPTCHAs, rate limits, and anti-bot detection. The Scavio API handles all of this for you. Send a POST request with your query and get structured JSON back — no proxy management or browser automation needed.

The Scavio API returns structured JSON with post snippets, author handles, timestamps, engagement signals, media URLs, hashtag results. All data is returned in a clean, consistent format that is easy to parse in JavaScript.

Scavio offers a free tier with 500 credits per month. Each API request costs 1 credit regardless of which platform you search. No credit card required to start. Paid plans start at $30/month for higher volumes.

Scavio returns X (Twitter) results in 1-3 seconds on average. Results are fetched in real time from X (Twitter) — there is no caching layer or stale data. Every request returns live results.

Start Scraping X (Twitter) with JavaScript

Get your free Scavio API key and start fetching X (Twitter) data in JavaScript. 500 free credits/month — no credit card required.