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

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

Trial Conversion

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

Churn Prevention

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

Recording Conversions

Call resolve_conversion in your payment webhook. We handle attribution internally.

# In your Stripe/payment webhook
client.resolve_conversion({
    "userId": event["customer"]["metadata"]["user_id"],
    "amount": event["amount"] / 100,
})

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: fourbyfour-0.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 80e66fd3213543613df7629ecefad3dd43b35f7e2cf0cb3054cd86294d77854a
MD5 5fbea98ee4828566b2a4b4ce3fa05d16
BLAKE2b-256 4977ba813411e37d2a893454d198c4fc04c22b77e857c09b482a9efb72501cc3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fourbyfour-0.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f5bae5d74275f2b05fb81bff571b4e07615b774ab770d52e5628dee412c7b548
MD5 366cf3f38b93e2cf001f00158fa15721
BLAKE2b-256 4bc8c81f8639ae91b3a52c0304742b0bd8685c903df1ff320a58a5b27b887b88

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