FetchLayer fetchlayer.dev Sign in

+ Integration Guide

Reddit Scraping API with Bun: Complete Guide

How to scrape Reddit using Bun and the FetchLayer API. Fast, lightweight examples for searching posts, scraping comments, and monitoring subreddits.

  • Bun
  • reddit scraping
  • reddit 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 Reddit API with Bun — same API, faster runtime.


Setup

  1. Get a free API key (no credit card)
  2. Install Bun if you haven’t: curl -fsSL https://bun.sh/install | bash
  3. No npm packages needed — Bun has fetch built in.

API Client

const API_KEY = Bun.env.FETCHLAYER_API_KEY!;
const BASE = 'https://fetchlayer.dev/api/reddit';

async function reddit(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 Reddit

const data = await reddit('search', {
  query: 'best bun packages 2026',
  sort: 'top',
  limit: 10,
});

for (const post of data.results) {
  console.log(`${post.title} — r/${post.subreddit} — ${post.score} pts`);
}

Run it:

FETCHLAYER_API_KEY=sk-your-key bun run search.ts

Get Subreddit Posts

const data = await reddit('community-posts', {
  subreddit: 'javascript',
  sort: 'top',
  time: 'week',
  limit: 20,
});

for (const post of data.posts) {
  console.log(`[${post.score}] ${post.title}`);
}

Scrape a Post with Comments

const thread = await reddit('post', {
  url: 'https://www.reddit.com/r/programming/comments/abc123/some_post/',
  pages: 2,
});

console.log(`${thread.title} — ${thread.comments.length} comments`);

Full Example: Reddit Keyword Monitor

Save as monitor.ts:

const API_KEY = Bun.env.FETCHLAYER_API_KEY!;
const BASE = 'https://fetchlayer.dev/api/reddit';

async function reddit(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';
const subreddits = ['startups', 'SaaS', 'webdev', 'javascript'];

console.log(`\nSearching for "${keyword}"...\n`);

for (const sub of subreddits) {
  const data = await reddit('search', {
    query: keyword,
    subreddit: sub,
    sort: 'new',
    limit: 5,
  });

  if (data.results?.length) {
    console.log(`r/${sub}:`);
    for (const post of data.results) {
      console.log(`  [${post.score}] ${post.title}`);
      console.log(`  ${post.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 reddit('search', { query: 'bun runtime', sort: '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