πŸ”’ Detect fraud in user activity with PostgreSQL, OpenAI and Slack

⚑ 59 views Β· πŸ”’ SecOps & Security Automation

Description

AI Fraud Detection Workflow

> 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.

Quick Implementation Steps

  1. Import workflow into n8n
  2. Configure webhook endpoint /user-activity
  3. Set up PostgreSQL connection and user_activity_logs table
  4. Add OpenAI API credentials
  5. Configure alerting node (Slack or alternative)
  6. Activate workflow and test with sample payload

What It Does

This 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:

Who It’s For

Requirements to Use This Workflow

Database Schema

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()
);

How It Works & Setup Guide

1. Webhook Trigger

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"
}

2. Request Validation

Ensures required fields exist:

3. Fetch User History (PostgreSQL)

Retrieves last 10 activity logs for the user to build behavioral context.

4. Context Builder

Merges:

This helps detect behavioral anomalies.

5. Rule-Based Fraud Engine

Applies deterministic fraud logic:

Outputs:

6. AI Fraud Interpreter (OpenAI)

The AI does not calculate risk.

It only interprets rule-based output and returns:

{
  "risk_level": "LOW | MEDIUM | HIGH",
  "reason": "short explanation"
}

7. AI Response Cleaner

8. Decision Fusion Layer

Final risk logic:

9. Database Logger

Stores final result in PostgreSQL:

10. High Risk Filter

Triggers only when:

final_risk === "HIGH"

11. Alert Dispatcher

Sends fraud alert via Slack (or can be replaced with email, SMS, Teams, etc.)

How to Customize Nodes

Add-ons (Enhancements)

Use Case Examples

This workflow can be extended to many more fraud detection and security monitoring use cases.

Troubleshooting Guide

IssuePossible CauseSolution
Webhook not receiving dataIncorrect endpoint or inactive workflowEnsure workflow is active and webhook URL is correct
AI parsing errorUnexpected response format from OpenAIVerify JSON structure from AI output
No historical data foundEmpty user logs tableEnsure user_activity_logs has existing records
Slack alert not triggeredRisk not classified as HIGHCheck fusion logic in decision node
PostgreSQL errorWrong credentials or schema mismatchVerify DB connection and table structure
Incorrect risk scoreRule logic misconfigurationReview fraud scoring conditions

Need Help

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.

πŸ”— Nodes Used

Function, Postgres, Slack, Webhook, OpenAI

πŸ“₯ Import

Download workflow.json and import into n8n: Workflow menu β†’ Import from File

πŸ“– Importing guide Β· πŸ”‘ Credential setup