How to search Reddit posts by keyword and collect their comments
Search with Reddit post search, then read the threads you keep with Reddit post comments. The worked example is 8 calls and costs $0.02; a failed call is not charged.
Updated
The job: Reddit is where people ask "which one should I buy" and get long, honest answers. You want those answers for a product or topic as data. Search for the posts, keep the threads with the most discussion, and read their comments with the reply structure intact. You end with one CSV: each row a comment with its post, community, text, score, author and, for replies, the comment it answers.
No Reddit account, app registration or OAuth token is involved. Each call is an HTTPS request with your key.
Step by step
Step 1: search posts
Call Reddit post search with query. Two parameters decide what comes back:
sort:relevance(the default),latest(newest first) orpopular(highest score first).published_within:day,week,monthoryear. Leave it out to search all time.
One call returns about 25 posts for $0.004. Each post has id (in Reddit's t3_ form, such as t3_1p4kx9q), url, type (text, image or video), title, the body in text (empty for link and image posts), published_at, author.username, the score in metrics.likes and the comment count in metrics.comments. Add view=full for ext.subreddit (the community, as r/name), ext.upvote_ratio, ext.link (the outside link of a link post), ext.flair and ext.nsfw.
curl -G "https://api.okdata.online/v1/reddit/posts/search" \
-H "Authorization: Bearer $OKDATA_API_KEY" \
--data-urlencode 'query=air fryer' \
-d sort=popular -d published_within=month -d view=full
For the next page, send page.next_cursor back as cursor, unchanged; stop when page.has_more is false.
Step 2: read the comments
Pass a post's id (with or without t3_) or its link as url to Reddit post comments. sort takes best (the default, Reddit's own order), latest or popular (highest score first).
One call returns about 10 comments for $0.002, together with the first levels of replies under them. Each comment has id (t1_ form), post_id, parent_id, text, author.username, the score in likes, the reply count in replies and published_at. parent_id is null for a top-level comment and holds the t1_ ID of the comment being answered for a reply, so you can rebuild the thread as a tree.
curl -G "https://api.okdata.online/v1/reddit/posts/comments/list" \
-H "Authorization: Bearer $OKDATA_API_KEY" \
-d id=t3_1p4kx9q -d sort=popular
Page with page.next_cursor as in step 1. Each further page brings more top-level comments with their first replies.
Step 3: put it together in Python
import csv
import os
import requests
API = "https://api.okdata.online"
HEADERS = {"Authorization": f"Bearer {os.environ['OKDATA_API_KEY']}"}
SEARCH_PAGES = 2
THREADS = 6 # first comment page of each; one call per thread
def get(path, **params):
body = requests.get(API + path, headers=HEADERS, params=params, timeout=60).json()
if body["code"] != "ok":
raise RuntimeError(f"{body['code']}: {body['message']} (charged: {body['charged']})")
return body
posts, cursor = [], None
for _ in range(SEARCH_PAGES):
body = get("/v1/reddit/posts/search", query="air fryer", sort="popular", published_within="month", view="full", **({"cursor": cursor} if cursor else {}))
posts += body["data"]["items"]
if not body["page"]["has_more"]:
break
cursor = body["page"]["next_cursor"]
busiest = sorted(posts, key=lambda p: p["metrics"]["comments"] or 0, reverse=True)[:THREADS]
with open("reddit_comments.csv", "w", newline="", encoding="utf-8") as f:
out = csv.writer(f)
out.writerow(["community", "post_id", "post_title", "comment_id", "parent_id", "author", "score", "replies", "published_at", "text"])
for p in busiest:
for c in get("/v1/reddit/posts/comments/list", id=p["id"], sort="popular")["data"]["items"]:
out.writerow([p["ext"]["subreddit"], p["id"], p["title"], c["id"], c["parent_id"], c["author"]["username"], c["likes"], c["replies"], c["published_at"], c["text"]])
Rows with an empty parent_id are top-level comments; the rest are replies. To read deeper threads, page the comments of the posts that matter most.
Good to know
- Score, not likes. Reddit shows a score (upvotes minus downvotes), and that is what
metrics.likesand a comment'slikeshold. Scores near zero can mean a disputed comment, not an ignored one. - What is null. Reddit shows no view counts, so
metrics.viewsis null. Commenters' avatars, follower counts and locations are not returned;repliescan be null when the count is not known. - Deleted content. A removed post returns
not_found, which costs nothing. Deleted comments can still appear in a thread with their text replaced by Reddit's placeholder; filter them out before analysis. - Search reach. The results are what Reddit's own search returns for the query and period, not every post that mentions the words. Try a few phrasings for a full picture.
- The official route. Reddit has its own Data API. Its Data API Terms say that commercial use, or research beyond its rate limits, needs a separate agreement with Reddit. For a personal or approved research project within those limits, it is a good option.
- Retries without paying twice. Send an
Idempotency-Keyheader with each call; the same request with the same key within 24 hours returns the stored answer and no new charge.
For what comments cost across platforms, see social media comments cost per 1,000. Turning threads like these into findings is covered in word of mouth.
What the example costs
Two pages of the top-scoring Reddit posts about air fryers from the past month, then the first page of comments under the six most commented
| Endpoint | Calls | Price | Subtotal |
|---|---|---|---|
| Reddit post search | 2 | $0.004 | $0.008 |
| Reddit post comments | 6 | $0.002 | $0.012 |
| Total | 8 | $0.02 |
Prices in force when this page was built. A failed call costs nothing.
In your AI (MCP)
Connect the MCP server once, then say what you want. The agent finds these endpoints, reads their prices and calls them on your balance.
SaySearch Reddit for air fryer posts from the past month, top score first, two pages. Take the six posts with the most comments, read the first page of comments under each by top score, and give me one table: community, post title, comment, score, whether it is a reply. Then sum up what people recommend and what they regret buying.
What the agent sends to the call tool first
{
"endpoint": "reddit.posts.search",
"params": {
"query": "saturn rings"
},
"idempotency_key": "<a new UUID>"
}