I Built a CBT Thought Analyzer API — No AI, No ML, Just Psychology
How I turned 11 cognitive distortion patterns from CBT therapy into a REST API with 3 endpoints — all running on a free Render instance. Now listed on RapidAPI.
What is CBT (Cognitive Behavioral Therapy)?
Cognitive Behavioral Therapy is the most evidence-based form of psychotherapy — decades of research show it works for anxiety, depression, OCD, and more. The core idea: your thoughts cause your feelings, not your circumstances.
A key CBT technique is identifying cognitive distortions — systematic errors in thinking that cause emotional distress. David Burns' Feeling Good identifies 11 common distortions:
- All-or-Nothing Thinking — "If I'm not perfect, I'm a failure"
- Overgeneralization — "One rejection means I'll always be rejected"
- Mental Filter — dwelling on one negative detail
- Discounting the Positive — "Those don't count"
- Jumping to Conclusions (mind reading + fortune telling)
- Magnification/Catastrophizing — "This will be a disaster"
- Emotional Reasoning — "I feel guilty, so I must be guilty"
- Should Statements — "I should be doing more"
- Labeling — "I'm a loser" vs "I made a mistake"
- Personalization & Blame — "It's all my fault"
- Comparison — "Everyone is doing better than me"
The API
I built a REST API that detects these distortions in text input. No AI model needed — it uses evidence-based keyword-pattern matching for instant analysis.
Endpoints
| Method | Path | Description |
|---|---|---|
POST |
/analyze |
Detect CBT cognitive distortions (11 types) |
POST |
/procrastination |
Detect procrastination patterns (8 types) |
POST |
/attachment |
Detect attachment style (4 types) |
GET |
/health |
Health check |
Example: Analyze a Thought
curl -X POST https://cbt-thought-analyzer.onrender.com/analyze -H "Content-Type: application/json" -d '{"text": "I always mess everything up. Nothing ever goes right for me."}'
Response:
{
"distortions": [
{
"name": "all_or_nothing_thinking",
"confidence": 0.85,
"evidence": "always mess everything",
"reframe": "Is it really true that you ALWAYS mess up? Can you think of a time when things went well?"
},
{
"name": "overgeneralization",
"confidence": 0.80,
"evidence": "always, nothing ever",
"reframe": "Always and never are rarely accurate. What specific things have gone right?"
}
],
"summary": "2 distortions detected",
"dominant_distortion": "all_or_nothing_thinking"
}
Why No AI Model?
Three reasons:
- Cost — Pattern matching is free. LLM inference costs per-call.
- Speed — Pattern matching returns in <100ms. LLM calls take 2-10s.
- Determinism — The same input always returns the same output. No hallucination risk in mental health content.
The trade-off: pattern matching is less nuanced than an LLM. But for a first-pass screening tool that helps people identify their thinking patterns, the evidence-based keyword approach is fast, free, and reliable.
The Procrastination Detector
The /procrastination endpoint detects 8 procrastination patterns based on behavioral activation research:
- Perfectionism block
- Fear of failure
- Task overwhelm
- Waiting for motivation
- Task avoidance
- All-or-nothing approach
- Guilt-procrastination cycle
- Minimization trap
Each pattern comes with a CBT intervention — not just "you're procrastinating" but "here's the specific pattern and here's what research says helps."
The Attachment Style Detector
The /attachment endpoint uses Bartholomew's 4-type model (Secure, Preoccupied/Anxious, Fearful, Dismissing/Avoidant) based on two dimensions: avoidance of intimacy and anxiety about abandonment. Grounded in Miller's Intimate Relationships 8th edition.
Hosting on Render (Free Tier)
The API runs on a free Render web service. Free tier spins down after 15 min of inactivity, so the first request after idle takes ~10-15s (cold start). Subsequent requests are instant.
For production use, a $7/month Render instance eliminates cold starts.
Live on RapidAPI
The API is now listed on RapidAPI — giving built-in API key management, usage analytics, monetization (charge per-call), and discovery by 4M+ developers.
If you want to try the API directly, it's live at https://cbt-thought-analyzer.onrender.com. The GitHub repo with all 24 CBT tools is at github.com/alexcoledev/cbt-toolkit.
This is not a replacement for therapy. It's a screening tool that helps identify thinking patterns. If you're struggling, please reach out to a licensed mental health professional.
Top comments (0)