+ Integration Guide
Twitter/X API with Bun: Complete Guide
How to scrape Twitter/X using Bun and the FetchLayer API. Fast, lightweight examples for searching tweets, fetching profiles, and pulling follower data.
Written by Alex P.
- Bun
- twitter scraping
- X API
- Twitter API
- JavaScript
- API integration
Bun is a fast JavaScript runtime that’s great for API scripts and data pipelines. This guide shows how to use FetchLayer’s Twitter/X API with Bun — same API, faster runtime.
Setup
- Get a free API key (no credit card)
- Install Bun if you haven’t:
curl -fsSL https://bun.sh/install | bash - No npm packages needed — Bun has
fetchbuilt in.
API Client
const API_KEY = Bun.env.FETCHLAYER_API_KEY!;
const BASE = 'https://api.fetchlayer.dev/twitter';
async function twitter(endpoint: string, body: Record<string, unknown>) {
const res = await fetch(`${BASE}/${endpoint}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
if (!res.ok) throw new Error(`${res.status}: ${await res.text()}`);
return res.json();
}
Search Twitter/X
const data = await twitter('search', {
query: 'best bun packages 2026',
product: 'Latest',
count: 10,
});
for (const tweet of data.results) {
console.log(`[${tweet.likeCount} likes] @${tweet.author.handle}: ${tweet.text.slice(0, 100)}`);
}
Run it:
FETCHLAYER_API_KEY=sk-your-key bun run search.ts
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 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(`Joined: ${profile.joinedAt}`);
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}] ${tweet.text.slice(0, 120)}`);
}
Full Example: Twitter Keyword Monitor
Save as monitor.ts:
const API_KEY = Bun.env.FETCHLAYER_API_KEY!;
const BASE = 'https://api.fetchlayer.dev/twitter';
async function twitter(endpoint: string, body: Record<string, unknown>) {
const res = await fetch(`${BASE}/${endpoint}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
if (!res.ok) throw new Error(`${res.status}: ${await res.text()}`);
return res.json();
}
const keyword = Bun.argv[2] ?? 'your-product';
console.log(`\nSearching Twitter/X for "${keyword}"...\n`);
const data = await twitter('search', {
query: keyword,
product: 'Latest',
count: 25,
});
const results = data.results || [];
console.log(`Found ${results.length} tweets\n`);
const top = results
.sort((a: any, b: any) => (b.likeCount || 0) - (a.likeCount || 0))
.slice(0, 5);
for (const tweet of top) {
console.log(`[${tweet.likeCount} likes] @${tweet.author.handle}: ${tweet.text.slice(0, 120)}`);
console.log(` ${tweet.url}\n`);
}
process.exit(0);
Run it:
FETCHLAYER_API_KEY=sk-your-key bun run monitor.ts "react server components"
Bun-Specific Tips
Use Bun.env instead of process.env — it’s faster and type-aware.
Write results to a file:
const data = await twitter('search', { query: 'bun runtime', product: 'Top' });
await Bun.write('results.json', JSON.stringify(data, null, 2));
Run on a schedule with cron:
# Add to crontab: every hour
0 * * * * cd /path/to/project && FETCHLAYER_API_KEY=sk-... bun run monitor.ts >> log.txt 2>&1
What’s Next
- Twitter API with Python — Python version
- Twitter API with Node.js — JavaScript/Node version
- Twitter API with TypeScript — typed version
- Twitter API with Go — Go integration
- How to Scrape Twitter/X in 2026 — all scraping methods
- FetchLayer API Reference