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.0.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.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: trusylo-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 6a152d7222cbdad7648b6048ec70e86f494b72e06e6df25b4f6db9f1a8e69a04
MD5 c94bdba92fa96548df16516d2d850948
BLAKE2b-256 048f0bd706e584692ecbc0862094a1fe140550a60647b20c06a1f763b3dd99d1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: trusylo-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ff127376627eaade90d8cc79d46fe92ea8d0ab829973aafa49437beca9a5d55
MD5 dd28f40c5f9ad6b77b3e85f4223d0fd8
BLAKE2b-256 8c338a70943c2778b5a7706362a375328a5201e4b2a7062802f16bb79b5e58eb

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