Skip to main content

Stop fraud with one line of code

Project description

Truslyo Python SDK

Stop fraud with one line of code.

Installation

pip install trusylo

Quick Start

Basic Usage

from trusylo import TruslyoClient

client = TruslyoClient(api_key="sk_live_abc123")

result = client.check(
    token="eyJ...",  # Token from Truslyo.js
    email="user@example.com",
    ip_address="192.168.1.1"
)

if result.should_block():
    raise Exception("Fraud detected!")

Flask Decorator

from flask import Flask, request
from trusylo import trusylo_protect

app = Flask(__name__)

@app.post("/signup")
@trusylo_protect(api_key="sk_live_abc123")
def signup():
    # Your code here
    return {"success": True}

FastAPI Middleware

from fastapi import FastAPI
from trusylo import TruslyoMiddleware

app = FastAPI()

app.add_middleware(
    TruslyoMiddleware,
    api_key="sk_live_abc123",
    protected_routes=["/signup", "/checkout"]
)

Django Middleware

# settings.py
MIDDLEWARE = [
    'trusylo.middleware.DjangoTruslyoMiddleware',
    # ... other middleware
]

TRUSLYO_API_KEY = "sk_live_abc123"
TRUSLYO_PROTECTED_ROUTES = ["/signup", "/checkout"]

Features

  • One-line protection - Decorator-based fraud detection
  • Framework support - Flask, FastAPI, Django, Starlette
  • Automatic enrichment - IP intelligence, email validation
  • Configurable blocking - Control when to block vs review
  • Fail-safe - Configurable fail-open behavior
  • Type hints - Full type annotation support

API Reference

TruslyoClient

client = TruslyoClient(
    api_key="sk_live_abc123",
    base_url="https://api.trusylo.com",  # Optional
    timeout=5  # Optional, seconds
)

FraudCheckResult

result = client.check(token="...")

# Decision methods
result.should_block()    # True if fraud detected
result.should_review()   # True if manual review needed
result.is_approved()     # True if approved

# Properties
result.decision          # "approve", "block", or "review"
result.risk_score        # 0.0 to 1.0
result.reasons           # List of reasons
result.fraud_patterns    # Detected fraud patterns
result.session_id        # Session identifier

Configuration

Decorator Options

@trusylo_protect(
    api_key="sk_live_abc123",
    block_on_fraud=True,      # Block when fraud detected
    block_on_review=False,    # Don't block reviews
    fail_open=True,           # Continue if API fails
    custom_handler=my_handler # Custom callback
)

Custom Handler

def my_fraud_handler(result, request):
    if result.should_block():
        # Log to your system
        logger.warning(f"Fraud blocked: {result.get_summary()}")
        # Send alert
        send_alert(result)

@app.post("/signup")
@trusylo_protect(
    api_key="sk_live_abc123",
    custom_handler=my_fraud_handler
)
def signup():
    return {"success": True}

Error Handling

from trusylo import (
    TruslyoError,
    FraudDetectedError,
    InvalidTokenError,
    APIError,
    RateLimitError
)

try:
    result = client.check(token="...")
except InvalidTokenError:
    return {"error": "Invalid or expired token"}
except RateLimitError:
    return {"error": "Rate limit exceeded"}
except APIError as e:
    return {"error": f"API error: {str(e)}"}

Requirements

  • Python 3.7+
  • requests >= 2.25.0

License

MIT License

Support


#### **`LICENSE`** (MIT License)

MIT License

Copyright (c) 2024 Truslyo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#### **`MANIFEST.in`**

include README.md include LICENSE recursive-include trusylo *.py

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

trusylo-1.0.2.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

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

trusylo-1.0.2-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for trusylo-1.0.2.tar.gz
Algorithm Hash digest
SHA256 40033d92e6ff39bd6a062aa6161892258977070d862e67117d4909c984e8d8b3
MD5 2643f26d18640e3133a60d84aa22fbf5
BLAKE2b-256 03fbc973123681e754c521713bcfa2d78ad4528beba78d16c1aec32340ad94f1

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for trusylo-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d25854dbc68da382f6a8174e4cec54d3e305986f243931b4af211aa4826fa959
MD5 995823c7924863ea9661425d51ca525d
BLAKE2b-256 f1a0619ecf087357cd16aab2a9c334c9a0785f6ec90360f5f55f043d5ade2fe4

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