Skip to main content

tensorrail

TensorRail Payments SDK for Python (3.9+). A typed, server-side client for the TensorRail API.

  • Base URL: https://api.tensorrail.com
  • Auth: the api-key request header (your merchant API key)
  • Amounts are in minor units (the lowest denomination of the currency), e.g. 6540 for $65.40 USD.
  • Built-in idempotency keys, retries with backoff, and typed errors.

Installation

pip install tensorrail

Quick Start

from tensorrail import TensorRail

client = TensorRail("your-api-key")  # base_url defaults to https://api.tensorrail.com

# Create a payment — amount is in MINOR units (cents): 4999 = $49.99
payment = client.payments.create(
    amount=4999,
    currency="USD",
    payment_method="card",
    payment_method_data={
        "card_number": "4242424242424242",
        "exp_month": 12,
        "exp_year": 2027,
        "cvc": "123",
    },
    idempotency_key="order-abc-123",
)
print(payment.payment_id, payment.status)

# Retrieve a payment
fetched = client.payments.retrieve(payment.payment_id)

# Confirm / capture / cancel
confirmed = client.payments.confirm(payment.payment_id)
captured = client.payments.capture(payment.payment_id, amount_to_capture=4999)
client.payments.cancel(payment.payment_id)

Refunds

# Refund a payment — amount in MINOR units (omit to refund the full amount).
refund = client.refunds.create(
    payment.payment_id,
    amount=1000,  # $10.00
    reason="requested_by_customer",
    idempotency_key="refund-abc-123",
)
print(refund.refund_id, refund.status)

fetched_refund = client.refunds.retrieve(refund.refund_id)

Customers

customer = client.customers.create(name="Ada Lovelace", email="ada@example.com")
fetched_customer = client.customers.retrieve(customer.customer_id)

Context Manager

with TensorRail("your-api-key") as client:
    payment = client.payments.create(amount=999, currency="EUR")  # €9.99

Webhook Signature Verification

TensorRail signs webhook deliveries with the TensorRail-Signature header (HMAC-SHA512). Always verify the signature against the raw request body before trusting an event — do not re-serialize the parsed JSON first.

from flask import Flask, request, abort
from tensorrail import TensorRail, WebhookVerificationError

client = TensorRail("your-api-key")
app = Flask(__name__)

@app.post("/webhooks/tensorrail")
def handle_webhook():
    signature = request.headers.get("TensorRail-Signature", "")
    try:
        event = client.webhooks.construct_event(
            request.get_data(),  # raw bytes
            signature,
            secret="your-webhook-secret",
        )
    except WebhookVerificationError:
        abort(400)
    # event is the verified, parsed payload
    print("verified event:", event)
    return "", 200

Error Handling

from tensorrail import (
    TensorRail,
    TensorRailError,
    AuthenticationError,
    ValidationError,
    RateLimitError,
)

try:
    client.payments.create(amount=-1, currency="USD")
except ValidationError as e:
    print(f"Invalid request: {e} (code={e.code})")
except AuthenticationError as e:
    print(f"Auth failed: {e} (HTTP {e.status_code})")
except RateLimitError as e:
    print(f"Rate limited, retry after {e.retry_after}s")
except TensorRailError as e:
    print(f"API error: {e} (HTTP {e.status_code}, request_id={e.request_id})")

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tensorrail-0.2.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

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

tensorrail-0.2.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tensorrail-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2ab1678d78a16765fed88d3cb64e61781456dcc575d1a673e93539ae589f0a49
MD5 a6d233470b4c153270bd6a061815ab05
BLAKE2b-256 5abda99cfcf3867db68b31ef1506ab1dc07a689de5a61e404b00b4da68163ee2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for tensorrail-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a1b3160f43e59398e5ee762bd60fc3eae39556dab308b02323128f036c600f7
MD5 f40c85c7011fc83e2369a2bb514e10e5
BLAKE2b-256 30d87ea42609fd0e729a1a82ec7a4ba8d356fb157c1ee6b94c2d8ee5d65b81a8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page