FetchLayer fetchlayer.dev Sign in

+ Integration Guide

Reddit Scraping API with Node.js: Complete Guide

How to search Reddit, scrape posts, and pull subreddit data using Node.js and the FetchLayer API. Working code examples for every endpoint.

  • Node.js
  • reddit scraping
  • reddit API
  • JavaScript
  • API integration

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

If you want the official package instead of raw fetch(), FetchLayer now ships @fetchlayer/reddit on npm with an open-source GitHub repo at fetchlayer-dev/reddit-scraper-js. For a package-first walkthrough, see Reddit API npm Package for JavaScript & TypeScript.


Setup

  1. Get a free API key (no credit card)
  2. That’s it. No npm packages to install.

Optional, if you prefer the SDK:

npm install @fetchlayer/reddit

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

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

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

Find posts matching a keyword across all of Reddit or scoped to a subreddit:

// Search all of Reddit
const results = await reddit('search', {
  query: 'best project management tools',
  sort: 'top',
  limit: 10,
});

for (const post of results.results) {
  console.log(`${post.title} — r/${post.subreddit} — ${post.score} pts`);
}
// Search within a specific subreddit
const results = await reddit('search', {
  query: 'TypeScript vs JavaScript',
  subreddit: 'webdev',
  sort: 'relevance',
});

Get Subreddit Posts

Pull hot, new, top, or rising posts from any public subreddit:

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

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

Scrape a Post with Comments

Get a full post body and its entire comment tree:

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

console.log(`Title: ${thread.title}`);
console.log(`Score: ${thread.score}`);
console.log(`Comments: ${thread.comments.length}`);

for (const comment of thread.comments.slice(0, 5)) {
  console.log(`  ${comment.author}: ${comment.body.slice(0, 100)}...`);
}

Get User Profile

const profile = await reddit('user-profile', {
  username: 'spez',
});

console.log(`Username: ${profile.username}`);
console.log(`Karma: ${profile.totalKarma}`);
console.log(`Account age: ${profile.accountAge}`);

Get User Posts and Comments

// User's post history
const posts = await reddit('user-posts', {
  username: 'some-user',
  sort: 'top',
});

// User's comment history
const comments = await reddit('user-comments', {
  username: 'some-user',
  sort: 'new',
});

Search for Subreddits

const communities = await reddit('search-communities', {
  query: 'machine learning',
});

for (const sub of communities.results) {
  console.log(`r/${sub.name} — ${sub.subscribers} subscribers`);
}

// What's trending on r/popular
const popular = await reddit('popular', { limit: 10 });

// Trending communities leaderboard
const leaderboard = await reddit('leaderboard', {});

// Explore communities by topic
const explore = await reddit('explore', { topic: 'technology' });

Full Example: Reddit Monitoring Script

A practical example — monitor a keyword across subreddits and log new posts:

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

async function reddit(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, subreddits) {
  console.log(`Monitoring "${keyword}" across ${subreddits.length} subreddits...\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`);
      }
    }
  }
}

monitor('your-brand-name', ['startups', 'SaaS', 'smallbusiness', 'webdev']);

Run it:

FETCHLAYER_API_KEY=sk-your-key node monitor.js

Error Handling

async function reddit(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.status === 401) throw new Error('Invalid API key');
  if (res.status === 429) throw new Error('Rate limited — slow down');
  if (!res.ok) throw new Error(`API error ${res.status}: ${await res.text()}`);

  return res.json();
}

What’s Next