DEV Community

CBT Tools
CBT Tools

Posted on

I Built a CBT Thought Analyzer API (OpenAPI + SDKs + Postman)

I Built a CBT Thought Analyzer API (OpenAPI + SDKs + Postman)

How I turned 11 cognitive distortion patterns from CBT therapy into a REST API with OpenAPI spec, Python/JS SDKs, and a Postman collection — all running on a free Render instance.

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:

  1. All-or-Nothing Thinking — "If I'm not perfect, I'm a failure"
  2. Overgeneralization — "One rejection means I'll always be rejected"
  3. Mental Filter — dwelling on one negative detail
  4. Discounting the Positive — "Those don't count"
  5. Jumping to Conclusions (mind reading + fortune telling)
  6. Magnification/Catastrophizing — "This will be a disaster"
  7. Emotional Reasoning — "I feel guilty, so I must be guilty"
  8. Should Statements — "I should be doing more"
  9. Labeling — "I'm a loser" vs "I made a mistake"
  10. Personalization & Blame — "It's all my fault"
  11. 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
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."}'
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

The Developer Experience

I wanted this to be easy to integrate, so I built:

1. OpenAPI 3.0 Spec

Valid 3.0.3 spec with 4 paths and 9 schemas. Import directly into RapidAPI, Swagger UI, or any OpenAPI-compatible tool.

2. Python SDK

from cbt_analyzer import CBTAnalyzer

analyzer = CBTAnalyzer(base_url="https://cbt-thought-analyzer.onrender.com")
result = analyzer.analyze("I'm such a failure, I'll never get this right")

for d in result["distortions"]:
    print(f"{d['name']}: {d['confidence']:.0%}")
    print(f"  Reframe: {d['reframe']}")
Enter fullscreen mode Exit fullscreen mode

3. JavaScript SDK

import { CBTAnalyzer } from './cbt_analyzer_js_sdk.js';

const analyzer = new CBTAnalyzer();
const result = await analyzer.analyze("Everyone else is doing better than me");
console.log(result.distortions);
Enter fullscreen mode Exit fullscreen mode

4. Postman Collection

5 pre-configured requests with example responses, environment variables for switching between direct endpoint and RapidAPI proxy, and auto-switch prerequest scripts.

Why No AI Model?

Three reasons:

  1. Cost — Pattern matching is free. LLM inference costs per-call.
  2. Speed — Pattern matching returns in <100ms. LLM calls take 2-10s.
  3. 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.

What's Next: RapidAPI Marketplace

All the developer materials are ready — OpenAPI spec, SDKs, Postman collection, code examples in 6 languages. The next step is listing on RapidAPI, which gives:

  • Built-in API key management
  • Usage analytics
  • Monetization (charge per-call)
  • Discovery by 4M+ developers

If you want to try the API now, it's live at https://cbt-thought-analyzer.onrender.com. The GitHub repo with all 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)