FetchLayer fetchlayer.dev Sign in

+ Integration Guide

Reddit Scraping API with Go: Complete Guide

How to search Reddit and scrape posts using Go and the FetchLayer API. Clean, idiomatic Go examples with proper error handling and struct decoding.

  • Go
  • Golang
  • reddit scraping
  • reddit API
  • API integration

This guide shows how to use the FetchLayer Reddit API with Go. All examples use the standard library — no third-party HTTP clients needed.


Setup

  1. Get a free API key (no credit card)
  2. Go 1.21+ (for slog and modern stdlib features)

API Client

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
)

const baseURL = "https://fetchlayer.dev/api/reddit"

var apiKey = os.Getenv("FETCHLAYER_API_KEY")

func reddit(endpoint string, body any, result any) error {
	payload, err := json.Marshal(body)
	if err != nil {
		return fmt.Errorf("marshal: %w", err)
	}

	req, err := http.NewRequest("POST", baseURL+"/"+endpoint, bytes.NewReader(payload))
	if err != nil {
		return fmt.Errorf("request: %w", err)
	}

	req.Header.Set("Authorization", "Bearer "+apiKey)
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return fmt.Errorf("do: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("API returned %d", resp.StatusCode)
	}

	return json.NewDecoder(resp.Body).Decode(result)
}

Types

type RedditPost struct {
	Title       string `json:"title"`
	Subreddit   string `json:"subreddit"`
	Author      string `json:"author"`
	Score       int    `json:"score"`
	NumComments int    `json:"numComments"`
	URL         string `json:"url"`
	Selftext    string `json:"selftext,omitempty"`
}

type SearchResponse struct {
	Results []RedditPost `json:"results"`
}

type CommunityPostsResponse struct {
	Subreddit string       `json:"subreddit"`
	Posts     []RedditPost `json:"posts"`
}

type Comment struct {
	Author  string    `json:"author"`
	Body    string    `json:"body"`
	Score   int       `json:"score"`
	Replies []Comment `json:"replies,omitempty"`
}

type PostResponse struct {
	Title       string    `json:"title"`
	Author      string    `json:"author"`
	Score       int       `json:"score"`
	NumComments int       `json:"numComments"`
	Selftext    string    `json:"selftext"`
	URL         string    `json:"url"`
	Comments    []Comment `json:"comments"`
}

type UserProfile struct {
	Username    string `json:"username"`
	DisplayName string `json:"displayName"`
	TotalKarma  int    `json:"totalKarma"`
	AccountAge  string `json:"accountAge"`
}

Search Reddit

func main() {
	var data SearchResponse
	err := reddit("search", map[string]any{
		"query": "best Go web frameworks",
		"sort":  "top",
		"limit": 10,
	}, &data)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		os.Exit(1)
	}

	for _, post := range data.Results {
		fmt.Printf("[%d] %s — r/%s\n", post.Score, post.Title, post.Subreddit)
	}
}

Run it:

FETCHLAYER_API_KEY=sk-your-key go run main.go

Get Subreddit Posts

var data CommunityPostsResponse
err := reddit("community-posts", map[string]any{
	"subreddit": "golang",
	"sort":      "top",
	"time":      "week",
	"limit":     20,
}, &data)
if err != nil {
	log.Fatal(err)
}

for _, post := range data.Posts {
	fmt.Printf("[%d] %s\n", post.Score, post.Title)
}

Scrape a Post with Comments

var thread PostResponse
err := reddit("post", map[string]any{
	"url":   "https://www.reddit.com/r/golang/comments/abc123/some_post/",
	"pages": 2,
}, &thread)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%s%d comments\n", thread.Title, len(thread.Comments))
for _, c := range thread.Comments[:min(5, len(thread.Comments))] {
	fmt.Printf("  %s: %s\n", c.Author, truncate(c.Body, 100))
}

Get User Profile

var profile UserProfile
err := reddit("user-profile", map[string]any{
	"username": "spez",
}, &profile)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%s%d karma — %s\n", profile.Username, profile.TotalKarma, profile.AccountAge)

Full Example: Reddit Monitor

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
)

const baseURL = "https://fetchlayer.dev/api/reddit"

var apiKey = os.Getenv("FETCHLAYER_API_KEY")

type RedditPost struct {
	Title     string `json:"title"`
	Subreddit string `json:"subreddit"`
	Score     int    `json:"score"`
	URL       string `json:"url"`
}

type SearchResponse struct {
	Results []RedditPost `json:"results"`
}

func reddit(endpoint string, body any, result any) error {
	payload, _ := json.Marshal(body)
	req, _ := http.NewRequest("POST", baseURL+"/"+endpoint, bytes.NewReader(payload))
	req.Header.Set("Authorization", "Bearer "+apiKey)
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return fmt.Errorf("API returned %d", resp.StatusCode)
	}
	return json.NewDecoder(resp.Body).Decode(result)
}

func main() {
	keyword := "your-brand"
	if len(os.Args) > 1 {
		keyword = os.Args[1]
	}

	subreddits := []string{"startups", "SaaS", "webdev", "golang"}

	fmt.Printf("Searching for %q...\n\n", keyword)

	for _, sub := range subreddits {
		var data SearchResponse
		err := reddit("search", map[string]any{
			"query":     keyword,
			"subreddit": sub,
			"sort":      "new",
			"limit":     5,
		}, &data)
		if err != nil {
			fmt.Fprintf(os.Stderr, "r/%s: %v\n", sub, err)
			continue
		}

		if len(data.Results) > 0 {
			fmt.Printf("r/%s:\n", sub)
			for _, post := range data.Results {
				fmt.Printf("  [%d] %s\n", post.Score, post.Title)
				fmt.Printf("  %s\n\n", post.URL)
			}
		}
	}
}
FETCHLAYER_API_KEY=sk-your-key go run main.go "react vs svelte"

What’s Next