Skip to main content

AntiScam AI – AI-powered request inspection middleware for Python

Project description

antiscamai

PyPI version Python versions Downloads License: MIT

AI-powered request inspection middleware for Python.
Add one line to your FastAPI, Django, or Flask app and every incoming request is automatically screened by a trained AI model for scams, phishing, social engineering, and fraudulent content.


Install

# FastAPI / Starlette
pip install antiscamai[fastapi]

# Django
pip install antiscamai[django]

# Flask
pip install antiscamai[flask]

# All frameworks
pip install antiscamai[all]

Quick Start

FastAPI (Middleware)

from fastapi import FastAPI
from antiscamai.middleware.fastapi import AntiScamFastAPIMiddleware
import os

app = FastAPI()
app.add_middleware(
    AntiScamFastAPIMiddleware,
    api_key=os.environ["ANTISCAM_API_KEY"],
    mode="block",   # "block" | "flag" | "monitor"
)

# All your existing routes are now protected — no other changes needed
@app.post("/api/contact")
async def contact(body: ContactForm):
    ...

FastAPI (Per-Route Dependency)

from fastapi import Depends
from antiscamai.middleware.fastapi import antiscam_fastapi

checker = antiscam_fastapi(api_key=os.environ["ANTISCAM_API_KEY"])

@app.post("/api/payment")
async def payment(body: PaymentForm, _=Depends(checker)):
    ...

Django

# settings.py
ANTISCAMAI = {
    "API_KEY": os.environ.get("ANTISCAM_API_KEY"),
    "MODE": "block",                              # block | flag | monitor
    "ENDPOINT": "http://localhost:5000",
    "EXCLUDE_PATHS": ["/health/", "/static/"],
    "INSPECT_METHODS": ["POST", "PUT", "PATCH"],
}

MIDDLEWARE = [
    # ... your existing middleware ...
    "antiscamai.middleware.django.AntiScamDjangoMiddleware",
]

Flask

from flask import Flask
from antiscamai.middleware.flask import antiscam_flask
import os

app = Flask(__name__)
antiscam_flask(app, api_key=os.environ["ANTISCAM_API_KEY"])

What Gets Blocked

Threat Example
Phishing messages "Your account is suspended — verify now"
Investment fraud "Guaranteed 500% ROI — risk-free"
Lottery / prize scams "You've won $50,000 — claim your prize"
Social engineering "I'm from Apple Support, share your OTP"
Phishing URLs in body http://secur3-paypal.xyz/verify
Prompt injection (LLM APIs) "Ignore previous instructions…"
Credential phishing "Enter your password to verify identity"

Response on Blocked Request

HTTP 403 Forbidden
{
  "error": "Request blocked by AntiScam AI",
  "requestId": "a4f2c1d3...",
  "riskLevel": "HIGH",
  "reason": "High urgency language + suspicious patterns detected"
}

Read the Inspection Result

# FastAPI — result stored in request.state
@app.post("/api/contact")
async def contact(request: Request, body: ContactForm):
    result = request.state.antiscam
    # result.threat_score  → 0–100
    # result.risk_level    → MINIMAL | LOW | MEDIUM | HIGH | CRITICAL
    # result.decision      → allow | flag | block
    # result.threats       → list of detected threats with explanations

# Django — result stored in request.antiscam
def my_view(request):
    result = getattr(request, "antiscam", None)

# Flask — result stored in g.antiscam
from flask import g
result = g.antiscam

Threat Callback

from antiscamai.middleware.fastapi import AntiScamFastAPIMiddleware

async def on_threat(result):
    print(f"Threat! score={result.threat_score} level={result.risk_level}")
    # Send to Slack, PagerDuty, your SIEM...

app.add_middleware(
    AntiScamFastAPIMiddleware,
    api_key="...",
    on_threat=on_threat,
)

Modes

Mode Behaviour
block Requests scoring ≥65 return HTTP 403 (default)
flag Allowed but sets X-AntiScam-Flag: true header
monitor All requests pass; threats are only logged

Configuration

Parameter Default Description
api_key required Your API key
endpoint http://localhost:5000 AntiScam AI gateway URL
mode "block" block, flag, or monitor
timeout_ms 3000 AI call timeout in ms
on_error "allow" Fail-open (allow) or fail-closed (block)
exclude_paths ["/health", "/metrics"] URL prefixes to skip
inspect_methods ["POST","PUT","PATCH"] HTTP methods to inspect
on_threat None Async/sync callback on threat detection

Self-Hosting

git clone https://github.com/antiscamai/backend
cd backend/deploy/docker
docker-compose up -d
AntiScamFastAPIMiddleware(app, api_key="YOUR_KEY", endpoint="http://localhost:5000")

Requirements

  • Python 3.10+
  • httpx >= 0.27.0

License

MIT © AntiScam AI

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

antiscamai-1.0.2.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

antiscamai-1.0.2-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file antiscamai-1.0.2.tar.gz.

File metadata

  • Download URL: antiscamai-1.0.2.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for antiscamai-1.0.2.tar.gz
Algorithm Hash digest
SHA256 87e86dc50dc7a8ce83f3c627e341caf1af29b0ede517d63cf2b54faa1f6600bc
MD5 7ebbdf8943daaef317ec2dfc36ca5557
BLAKE2b-256 c8fdc557bea19282210d84a26ba53753003946131b9ed971138ab213e9d8dcbd

See more details on using hashes here.

File details

Details for the file antiscamai-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: antiscamai-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for antiscamai-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b04fe62ea421313c511c86ef9e5671280c30dd5b09d62e6c1a3bfbad1ccadb70
MD5 3e6988658452b8e85faf1225fc54641a
BLAKE2b-256 1a81534c3df25a24876708aef15749fa0a79d1c7a67e90c8c4cd6aafb0cbf2b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page