n8n + PostgreSQL + OpenAI + Slack
This AI Fraud Detection Workflow is an automated n8n pipeline that analyzes user activity in real time using a combination of rule-based fraud detection, AI interpretation and historical behavioral context. It processes events like login attempts, password changes or transactions, evaluates risk, stores results in PostgreSQL and triggers alerts for high-risk activity.
/user-activityuser_activity_logs tableThis workflow continuously monitors user activity events and evaluates them for suspicious behavior.
When a user event is received, the system:
It helps detect anomalies like:
CREATE TABLE user_activity_logs (
id BIGSERIAL PRIMARY KEY,
user_id TEXT,
event TEXT,
ip TEXT,
location TEXT,
device TEXT,
risk_score INT,
ai_flag TEXT,
created_at TIMESTAMP DEFAULT NOW()
);
Receives user activity via POST request:
Endpoint:
/user-activity
Payload:
{
"user_id": "user_002",
"event": "password_change",
"ip": "192.165.1.45",
"location": "United States",
"device": "Chrome Browser - Windows"
}
Ensures required fields exist:
Retrieves last 10 activity logs for the user to build behavioral context.
Merges:
This helps detect behavioral anomalies.
Applies deterministic fraud logic:
Outputs:
rule_scorerule_risk (LOW / MEDIUM / HIGH)risk_reasonsThe AI does not calculate risk.
It only interprets rule-based output and returns:
{
"risk_level": "LOW | MEDIUM | HIGH",
"reason": "short explanation"
}
Final risk logic:
Stores final result in PostgreSQL:
Triggers only when:
final_risk === "HIGH"
Sends fraud alert via Slack (or can be replaced with email, SMS, Teams, etc.)
This workflow can be extended to many more fraud detection and security monitoring use cases.
| Issue | Possible Cause | Solution |
|---|---|---|
| Webhook not receiving data | Incorrect endpoint or inactive workflow | Ensure workflow is active and webhook URL is correct |
| AI parsing error | Unexpected response format from OpenAI | Verify JSON structure from AI output |
| No historical data found | Empty user logs table | Ensure user_activity_logs has existing records |
| Slack alert not triggered | Risk not classified as HIGH | Check fusion logic in decision node |
| PostgreSQL error | Wrong credentials or schema mismatch | Verify DB connection and table structure |
| Incorrect risk score | Rule logic misconfiguration | Review fraud scoring conditions |
If you need help with:
You can reach out to our n8n workflow developers at WeblineIndia for professional assistance in building and optimizing automation workflows like this.