Spam Detection API: The Complete Guide

Need to keep spam out of your platform? This guide covers everything you need to know about spam detection via API, including classification methods, spam scoring, and implementation examples.

What is Spam Detection?

Spam detection is the process of identifying unsolicited, irrelevant, or malicious content. It uses machine learning models trained on spam patterns to classify content as spam or legitimate (ham).

Modern spam detection goes beyond simple keyword matching to understand context, links, and behavioral patterns.

Common Spam Indicators

Spam detection algorithms look for these common patterns:

  • Excessive Links: Multiple URLs, especially to suspicious domains
  • Urgent Language: "Act now!", "Limited time!", "Free money!"
  • ALL CAPS: Excessive capitalization and exclamation marks
  • Suspicious Patterns: Hidden text, keyword stuffing
  • Known Spam Phrases: "Nigerian prince", "Congratulations winner"
Pro Tip: Use spam scores instead of binary decisions. Queue borderline content for human review rather than auto-blocking.

Using the Spam Detection API

TinyFn provides an endpoint to detect spam:

API Request
POST https://api.tinyfn.io/v1/detect/spam
Headers: X-API-Key: your-api-key
Content-Type: application/json

{
  "text": "CONGRATULATIONS! You have been selected to receive a FREE gift card! Click here NOW to claim your prize!!!",
  "type": "comment"
}
Response
{
  "is_spam": true,
  "spam_score": 0.94,
  "classification": "spam",
  "confidence": 0.96,
  "reasons": [
    "excessive_caps",
    "urgency_language",
    "suspicious_call_to_action",
    "multiple_exclamation_marks"
  ]
}

Parameters

Parameter Type Description
text string Content to check for spam (required)
type string Content type: email, comment, message (default: generic)
threshold number Spam threshold 0-1 (default: 0.5)

Code Examples

JavaScript / Node.js

const response = await fetch('https://api.tinyfn.io/v1/detect/spam', {
  method: 'POST',
  headers: {
    'X-API-Key': 'your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    text: userSubmission,
    type: 'comment'
  })
});
const data = await response.json();

if (data.is_spam) {
  rejectSubmission("Content flagged as spam");
} else if (data.spam_score > 0.3) {
  queueForReview(userSubmission);
} else {
  publishContent(userSubmission);
}

Python

import requests

def check_spam(text, content_type='generic'):
    response = requests.post(
        'https://api.tinyfn.io/v1/detect/spam',
        json={'text': text, 'type': content_type},
        headers={'X-API-Key': 'your-api-key'}
    )
    return response.json()

# Check incoming comment
result = check_spam(user_comment, 'comment')
if result['is_spam']:
    print(f"Spam detected! Reasons: {', '.join(result['reasons'])}")
else:
    print("Content appears legitimate")

cURL

curl -X POST "https://api.tinyfn.io/v1/detect/spam" \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"text": "Check this content for spam", "type": "comment"}'

Common Use Cases

  • Comment Sections: Filter spam from blog and article comments
  • Contact Forms: Prevent spam form submissions
  • User Registration: Block spam account creation
  • Messaging: Filter spam in chat and messaging systems
  • Reviews: Identify fake or spam reviews

Best Practices

  1. Use thresholds wisely: High threshold = fewer false positives
  2. Queue borderline content: Don't auto-block everything
  3. Combine with other signals: User reputation, IP, behavior
  4. Allow appeals: Legitimate content sometimes gets flagged

Try the Spam Detection API

Get your free API key and start detecting spam in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key