Skip to main content

Official Fourbyfour SDK for Python

Project description

fourbyfour

Official Python SDK for Fourbyfour - Revenue recovery for subscription businesses.

Installation

pip install fourbyfour

Quick Start

import os
from fourbyfour import fbf

client = fbf(
    api_key=os.environ["FOURBYFOUR_API_KEY"],
    project_id=os.environ["FOURBYFOUR_PROJECT_ID"],
)

# Start a workflow when payment fails
client.start_workflow({
    "userId": "user_123",
    "intent": "PAYMENT_FAILED",
    "signals": {
        "amountCents": 9900,
        "reason": "card_expired",
    },
})

# Record conversion when payment succeeds
client.resolve_conversion({
    "userId": "user_123",
    "amount": 99.00,
})

Setup

1. Get your credentials

Get your API key and Project ID from fourbyfour.dev.

2. Configure environment variables

# .env
FOURBYFOUR_API_KEY=sk_live_...
FOURBYFOUR_PROJECT_ID=proj_...

3. Initialize the client

# lib/fourbyfour.py
import os
from fourbyfour import fbf

client = fbf(
    api_key=os.environ["FOURBYFOUR_API_KEY"],
    project_id=os.environ["FOURBYFOUR_PROJECT_ID"],
)

API

start_workflow(params)

Trigger a workflow by sending an intent event.

result = client.start_workflow({
    "userId": "user_123",
    "intent": "PAYMENT_FAILED",
    "signals": {
        "amountCents": 9900,
        "reason": "card_expired",
    },
})

print(result.event_id)            # 'evt_...'
print(result.workflows_triggered)  # ['wf_...']

resolve_conversion(params)

Record a successful conversion (payment recovered, trial converted, etc).

result = client.resolve_conversion({
    "userId": "user_123",
    "amount": 99.00,
})

print(result.conversion_id)  # 'conv_...'
print(result.attributed)     # True

Intents

Intent Description
SIGNUP_COMPLETED User signed up
ONBOARDING_STARTED User started onboarding
ONBOARDING_COMPLETED User completed onboarding
TRIAL_STARTED Trial period began
TRIAL_ENDING Trial ending soon
TRIAL_EXPIRED Trial has expired
PLAN_SELECTED User selected a plan
CHECKOUT_STARTED User started checkout
PAYMENT_FAILED Payment failed
SUBSCRIPTION_ENDING Subscription renewal coming up
SUBSCRIPTION_CANCELLED User cancelled subscription
SUBSCRIPTION_DOWNGRADED User downgraded plan

Type Safety

Use the Signals type for type hints:

from fourbyfour import fbf, Signals

client = fbf(
    api_key=os.environ["FOURBYFOUR_API_KEY"],
    project_id=os.environ["FOURBYFOUR_PROJECT_ID"],
)

# Type hints for signal fields
signals: Signals["PAYMENT_FAILED"] = {
    "amountCents": 9900,
    "reason": "card_expired",
}

client.start_workflow({
    "userId": "user_123",
    "intent": "PAYMENT_FAILED",
    "signals": signals,
})

Examples

Payment Recovery

# When payment fails
client.start_workflow({
    "userId": user.id,
    "intent": "PAYMENT_FAILED",
    "signals": {
        "amountCents": invoice.amount,
        "reason": "card_expired",
        "retryCount": 1,
    },
})

# When user updates card and pays
client.resolve_conversion({
    "userId": user.id,
    "amount": invoice.amount / 100,
})

Trial Conversion

# When trial is ending
client.start_workflow({
    "userId": user.id,
    "intent": "TRIAL_ENDING",
    "signals": {
        "daysRemaining": 3,
        "plan": {"id": "pro", "name": "Pro", "priceCents": 2900},
    },
})

# When user converts to paid
client.resolve_conversion({
    "userId": user.id,
    "amount": 29.00,
})

Churn Prevention

# When user requests cancellation
client.start_workflow({
    "userId": user.id,
    "intent": "SUBSCRIPTION_CANCELLED",
    "signals": {
        "reason": "too_expensive",
        "tenureMonths": 6,
        "plan": {"id": "pro", "name": "Pro", "priceCents": 2900},
    },
})

# If user decides to stay
client.resolve_conversion({
    "userId": user.id,
    "amount": 29.00,
})

Error Handling

from fourbyfour import fbf, FourbyfourError, AuthenticationError, RateLimitError

client = fbf(api_key="...", project_id="...")

try:
    client.start_workflow({
        "userId": "user_123",
        "intent": "PAYMENT_FAILED",
        "signals": {"amountCents": 9900, "reason": "declined"},
    })
except AuthenticationError:
    print("Invalid API key")
except RateLimitError:
    print("Rate limit exceeded, retry later")
except FourbyfourError as e:
    print(f"API error: {e}")

Context Manager

The client can be used as a context manager:

from fourbyfour import fbf

with fbf(api_key="...", project_id="...") as client:
    client.start_workflow({
        "userId": "user_123",
        "intent": "PAYMENT_FAILED",
        "signals": {"amountCents": 9900, "reason": "declined"},
    })
# Connection automatically closed

Documentation

See fourbyfour.dev/docs for full documentation.

License

MIT

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

fourbyfour-0.2.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

fourbyfour-0.2.0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file fourbyfour-0.2.0.tar.gz.

File metadata

  • Download URL: fourbyfour-0.2.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for fourbyfour-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ac5dc1a20ac34f0cd709470ac7316c7280643e283d1df31e8e787bfa5640cd6a
MD5 15fb6fdbc86fdbff598fb4faaf63574d
BLAKE2b-256 48359242906ed4ad6199028a08667072937900d281d4e979000db1d997f2bf5e

See more details on using hashes here.

File details

Details for the file fourbyfour-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: fourbyfour-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for fourbyfour-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2409f1e5c6c4ae3e6e2842806becaad40ef172bc3de34a64f4d5796acaf78b0
MD5 7a888d8b05469767d91c82aca48b7d3c
BLAKE2b-256 b5245a564970893254c645e506134d003ec226ba9638051ad81c9cd3fc8ad371

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