Sift Science Python API Client
The `sift` library provides Python bindings for interacting with the Sift Science API, which offers real-time fraud prevention and risk assessment. It enables developers to send event data, retrieve scores and decisions, and manage labels. The library is actively maintained, with version 6.0.0 as the current release, and follows a regular release cadence to support new Python versions and API features.
Common errors
-
TypeError: 'str' object is not iterable
cause Attempting to pass a comma-separated string to the `abuse_types` parameter in `client.get_decisions()` after upgrading to `sift` v6.0.0, which now expects a sequence (e.g., tuple or list).fixChange the `abuse_types` parameter from a string to a tuple or list of strings: `abuse_types=('legacy', 'payment_abuse')`. -
sift.client.ApiException: API key not found or invalid.
cause The Sift API key or Account ID is missing or incorrect during client initialization, or for specific requests that require authentication.fixVerify that `SIFT_API_KEY` and `SIFT_ACCOUNT_ID` environment variables are correctly set, or that `api_key` and `account_id` are passed correctly to `sift.Client()` upon instantiation. -
Unsupported Python version: X.X. `sift` requires Python >= 3.8.
cause Running `sift` version 6.0.0 or newer with an unsupported Python version (e.g., Python 3.7 or earlier).fixUpgrade your Python environment to version 3.8 or higher. Alternatively, if unable to upgrade Python, downgrade the `sift` library version: `pip install 'sift<6.0.0'`.
Warnings
- breaking Version 6.0.0 dropped support for Python versions older than 3.8. Projects still on Python 3.7 or earlier must upgrade their Python environment before upgrading to `sift` v6.0.0 or higher.
- breaking The `abuse_types` parameter in `client.get_decisions()` changed type in v6.0.0. It no longer accepts a comma-separated string (e.g., `'legacy,payment_abuse'`) and now requires a sequence of string literals (e.g., `('legacy', 'payment_abuse')`).
- deprecated Starting from v5.6.1, several API calls (client.score(), client.get_user_score(), client.rescore_user(), client.unlabel()) moved from passing the API key as a request parameter to using Basic Authentication. While the old method might still work for some older endpoints or versions, it's deprecated for these specific calls and should be updated.
Install
-
pip install sift
Imports
- Client
from sift import Client
import sift client = sift.Client(api_key='YOUR_API_KEY', account_id='YOUR_ACCOUNT_ID')
Quickstart
import os
import sift
# Ensure API_KEY and ACCOUNT_ID are set as environment variables
api_key = os.environ.get('SIFT_API_KEY', 'YOUR_API_KEY')
account_id = os.environ.get('SIFT_ACCOUNT_ID', 'YOUR_ACCOUNT_ID')
if api_key == 'YOUR_API_KEY' or account_id == 'YOUR_ACCOUNT_ID':
print("WARNING: Please set SIFT_API_KEY and SIFT_ACCOUNT_ID environment variables or replace placeholders.")
client = sift.Client(api_key=api_key, account_id=account_id)
# Example: Send a $login event
try:
response = client.track(
event={
'$type': '$login',
'$user_id': 'billy_jones_301',
'$session_id': 'gigtleqlbrcssi8tghzz2w4w2',
'$login_status': '$success',
'$user_email': 'billy_jones@example.com',
'$ip': '128.148.1.10'
}
)
print("Login event sent successfully:", response)
except sift.client.ApiException as e:
print(f"Error sending login event: {e}")
# Example: Get decisions for a user
try:
user_decisions = client.get_user_decisions('billy_jones_301')
print("User decisions:", user_decisions)
except sift.client.ApiException as e:
print(f"Error fetching user decisions: {e}")