An r/SideProject post asked for a tool that checks Reddit demand with recency filters. This tutorial builds that tool as a REST API: it wraps Scavio's Reddit endpoint, adds configurable recency windows (1d, 7d, 30d), and scores demand intensity. Deploy it and you have a reusable validation endpoint.
Prerequisites
- Scavio API key
- Python 3.8+ with FastAPI
- uvicorn for serving
Walkthrough
Step 1: Set up FastAPI project
Minimal API with one endpoint.
from fastapi import FastAPI, Query
from pydantic import BaseModel
from datetime import datetime, timedelta
import requests, os
app = FastAPI(title='Reddit Demand API')
H = {'x-api-key': os.environ['SCAVIO_API_KEY']}
class DemandResult(BaseModel):
query: str
window: str
post_count: int
demand_score: int
top_posts: listStep 2: Build the demand endpoint
Single endpoint with recency filter parameter.
@app.get('/demand', response_model=DemandResult)
async def check_demand(
query: str,
window: str = Query('7d', regex='^(1d|7d|30d)$')
):
data = requests.post('https://api.scavio.dev/api/v1/search',
headers=H,
json={'platform': 'reddit', 'query': query, 'sort': 'new'}).json()
results = data.get('results', [])
# Filter by recency
days = {'1d': 1, '7d': 7, '30d': 30}[window]
cutoff = datetime.now() - timedelta(days=days)
recent = []
for r in results:
post_date = r.get('date', '')
if post_date:
try:
dt = datetime.fromisoformat(post_date.replace('Z', '+00:00'))
if dt.replace(tzinfo=None) >= cutoff:
recent.append(r)
except ValueError:
continue
# Score demand
score = sum(r.get('upvotes', 0) + r.get('comments', 0) * 2 for r in recent)
return DemandResult(
query=query, window=window, post_count=len(recent),
demand_score=score,
top_posts=[{'title': r.get('title', ''), 'upvotes': r.get('upvotes', 0),
'subreddit': r.get('subreddit', ''), 'url': r.get('url', '')}
for r in recent[:10]])Step 3: Add batch validation endpoint
Check multiple ideas at once.
@app.post('/demand/batch')
async def batch_demand(queries: list[str], window: str = '7d'):
results = []
for q in queries:
result = await check_demand(q, window)
results.append(result)
# Sort by demand score descending
results.sort(key=lambda x: x.demand_score, reverse=True)
return resultsStep 4: Run the server
Start the API with uvicorn.
# Install: pip install fastapi uvicorn requests
# Run: uvicorn demand_api:app --reload --port 8000
#
# Test: curl 'http://localhost:8000/demand?query=invoice+tool&window=7d'
#
# Cost: each demand check = 1 Scavio query = $0.005Python Example
# Full demand API server:
# pip install fastapi uvicorn requests
# uvicorn demand_api:app --port 8000
#
# GET /demand?query=crm+for+freelancers&window=7d
# POST /demand/batch with ["idea1", "idea2", "idea3"]
#
# Each check: 1 query = $0.005 via ScavioJavaScript Example
// Call the demand API from JS:
const res = await fetch('http://localhost:8000/demand?query=invoice+tool&window=7d');
const data = await res.json();
console.log(`Demand score: ${data.demand_score}, Posts: ${data.post_count}`);Expected Output
REST API at /demand with recency filtering (1d/7d/30d) and demand scoring. Batch endpoint for validating multiple ideas. Each query costs $0.005.