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 truslyo

Quick Start

Basic Usage

from truslyo 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 truslyo import truslyo_protect

app = Flask(__name__)

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

FastAPI Middleware

from fastapi import FastAPI
from truslyo import TruslyoMiddleware

app = FastAPI()

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

Django Middleware

# settings.py
MIDDLEWARE = [
    'truslyo.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.truslyo.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

@truslyo_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")
@truslyo_protect(
    api_key="sk_live_abc123",
    custom_handler=my_fraud_handler
)
def signup():
    return {"success": True}

Error Handling

from truslyo 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 truslyo *.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

truslyo-1.0.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

truslyo-1.0.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for truslyo-1.0.0.tar.gz
Algorithm Hash digest
SHA256 88837ddb433cc2b62c5f8b44ac086cbb735e82b8c3b5741cb193249044825ef3
MD5 cae0cff52ba2aa93b77bf70a69319467
BLAKE2b-256 4b906944c4b9bf6c9c36f1610d23c6ca02e859454d041c7a5011b2540d266af5

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for truslyo-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 62ee19fe4861497a4f2582059d9ea8eb9038d3e52fc1a3bd6a346cc80034a434
MD5 54ee867be129a9626a8fb2f2b4ba4bc3
BLAKE2b-256 df8e65f2db56e762ca21c7733173cf193e1f30ea20e957a928e82a4370204569

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