FetchLayer fetchlayer.dev Sign in

+ Integration Guide

Twitter/X API with Node.js: Complete Guide

How to search X/Twitter, get tweet details, fetch user profiles, and pull follower lists using Node.js and the FetchLayer API. Working code examples for every endpoint.

Written by Alex P.

  • Node.js
  • twitter scraping
  • X API
  • Twitter API
  • JavaScript
  • API integration

This guide shows you how to use the FetchLayer Twitter/X API with Node.js. Every example uses the built-in fetch API (Node 18+) — no external HTTP libraries needed.

If you prefer a fully typed SDK, FetchLayer now ships @fetchlayer/twitter on npm with an open-source GitHub repo at fetchlayer-dev/twitter-scraper-js.


Setup

  1. Get a free API key (no credit card)
  2. That’s it. No npm packages to install — unless you want the official SDK:
npm install @fetchlayer/twitter

All FetchLayer endpoints use POST with a JSON body and return JSON.

const API_KEY = 'sk-your-api-key';
const BASE_URL = 'https://api.fetchlayer.dev/twitter';

async function twitter(endpoint, body) {
  const res = await fetch(`${BASE_URL}/${endpoint}`, {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(body),
  });

  if (!res.ok) {
    throw new Error(`FetchLayer ${res.status}: ${await res.text()}`);
  }

  return res.json();
}

Search Twitter/X

Find tweets matching a keyword across X/Twitter:

// Search by keyword (Latest tab)
const results = await twitter('search', {
  query: 'best project management tools',
  product: 'Top',
  count: 10,
});

for (const tweet of results.results) {
  console.log(`[${tweet.likeCount} likes] @${tweet.author.handle}: ${tweet.text.slice(0, 100)}`);
}
// Search for people/accounts
const people = await twitter('search', {
  query: 'startup founder',
  product: 'People',
  count: 20,
});

// Search media
const media = await twitter('search', {
  query: 'new feature launch',
  product: 'Media',
  count: 10,
});

// Paginate results
const page1 = await twitter('search', { query: 'React', product: 'Latest', count: 25 });
const page2 = await twitter('search', { query: 'React', product: 'Latest', count: 25, cursor: page1.cursor });

Get a Tweet by ID

const tweet = await twitter('tweet-detail', {
  tweetId: '1942939879222220800',
});

console.log(`@${tweet.author.handle}: ${tweet.text}`);
console.log(`${tweet.likeCount} likes · ${tweet.retweetCount} retweets · ${tweet.replyCount} replies`);

Get Replies to a Tweet

const replies = await twitter('tweet-replies', {
  tweetId: '1942939879222220800',
});

for (const reply of replies.replies) {
  console.log(`  @${reply.author.handle}: ${reply.text.slice(0, 100)}`);
}

Get a User Profile

const profile = await twitter('user-profile-details', {
  handle: 'openai',
});

console.log(`${profile.displayName} (@${profile.handle})`);
console.log(`Followers: ${profile.followersCount.toLocaleString()}`);
console.log(`Following: ${profile.followingCount.toLocaleString()}`);
console.log(`Tweets: ${profile.tweetsCount.toLocaleString()}`);
console.log(`Bio: ${profile.description}`);

Get Extended Profile Info

const about = await twitter('about-profile', {
  handle: 'openai',
});

console.log(`Category: ${about.category}`);
console.log(`Business: ${about.isBusiness}`);

Get a User’s Tweets

const data = await twitter('user-tweets', {
  handle: 'rauchg',
  count: 20,
});

for (const tweet of data.tweets || []) {
  console.log(`[${tweet.likeCount || 0} likes] ${tweet.text.slice(0, 120)}`);
}

Get Followers and Following

// Who a user follows
const following = await twitter('following', {
  handle: 'openai',
  count: 50,
});

for (const account of following.accounts || []) {
  console.log(`@${account.handle} — ${(account.followersCount || 0).toLocaleString()} followers`);
}

// Who follows a user
const followers = await twitter('followers', {
  handle: 'openai',
  count: 50,
});

// Verified followers only
const verified = await twitter('verified-followers', {
  handle: 'openai',
  count: 20,
});

Full Example: Twitter Monitoring Script

A practical example — monitor a keyword on Twitter and identify high-engagement tweets:

const API_KEY = process.env.FETCHLAYER_API_KEY;
const BASE_URL = 'https://api.fetchlayer.dev/twitter';

async function twitter(endpoint, body) {
  const res = await fetch(`${BASE_URL}/${endpoint}`, {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(body),
  });
  return res.json();
}

async function monitor(keyword) {
  const data = await twitter('search', {
    query: keyword,
    product: 'Latest',
    count: 25,
  });

  const results = data.results || [];
  console.log(`\nFound ${results.length} tweets for "${keyword}"`);

  // Sort by engagement
  const top = results
    .sort((a, b) => (b.likeCount || 0) - (a.likeCount || 0))
    .slice(0, 5);

  console.log('\nTop 5 tweets:');
  for (const tweet of top) {
    console.log(`  [${tweet.likeCount} likes] @${tweet.author.handle}: ${tweet.text.slice(0, 120)}`);
    console.log(`  ${tweet.url}\n`);
  }
}

monitor(process.argv[2] || 'your-product');
FETCHLAYER_API_KEY=sk-your-key node monitor.mjs "react server components"

What’s Next