Skip to main content

A professional Python library for interacting with the Syriatel Cash API

Project description

Syriatel Cash API Python Library

A professional Python library for interacting with the Syriatel Cash API. Supports both async and sync operations with proper error handling.

Installation

pip install -e .

Or install from source:

git clone <repository-url>
cd syr_cash_api
pip install -e .

Quick Start

Async Usage

import asyncio
from syriatel import SyriatelCashClient

async def main():
    # Using async context manager (recommended)
    async with SyriatelCashClient(api_token="your_api_token") as client:
        # Get balance (returns integer directly)
        balance = await client.get_balance("0991234567")
        print(f"Balance: {balance} SYP")
        
        # Get incoming history (returns list of transactions directly)
        transactions = await client.get_incoming_history(number="0991234567", page=1)
        print(f"Total transactions: {len(transactions)}")
        for tx in transactions:
            print(f"{tx.date}: {tx.amount} SYP from {tx.from_gsm}")
        
        # Filter transactions by date
        filtered = client.filter_transactions_by_date(
            transactions,
            start_date="2024-01-01",
            end_date="2024-01-31"
        )
        
        # Find transaction by number
        tx = await client.find_incoming_transaction("TXN123456", number="0991234567")
        if tx:
            print(f"Found transaction: {tx.amount} SYP")
        
        # Get outgoing history
        outgoing = await client.get_outgoing_history(code="1234", page=1)
        
        # List all active numbers
        numbers = await client.get_incoming_history()
        for num in numbers:
            print(f"Number: {num.number}, Code: {num.code}")

asyncio.run(main())

Sync Usage

from syriatel import SyriatelCashClientSync

# Using context manager (recommended)
with SyriatelCashClientSync(api_token="your_api_token") as client:
    # Get balance (returns integer directly)
    balance = client.get_balance("0991234567")
    print(f"Balance: {balance} SYP")
    
    # Get incoming history (returns list of transactions directly)
    transactions = client.get_incoming_history(number="0991234567", page=1)
    print(f"Total transactions: {len(transactions)}")
    
    # Get outgoing history
    outgoing = client.get_outgoing_history(code="1234", page=1)
    
    # List all active numbers
    numbers = client.get_incoming_history()
    for num in numbers:
        print(f"Number: {num.number}, Code: {num.code}")

API Reference

SyriatelCashClient (Async)

get_balance(number: str) -> BalanceResponse

Get Syriatel Cash balance for a phone number.

Parameters:

  • number (str): Phone number in format 0XXXXXXXXX (will be normalized)

Returns:

  • BalanceResponse: Object with balance (int) attribute

Raises:

  • InvalidTokenError: API token is invalid
  • SubscriptionExpiredError: Subscription is expired
  • FetchFailedError: Failed to fetch from Syriatel
  • NotAuthorizedError: Account not authorized
  • ServerMaintenanceError: Servers under maintenance
  • NetworkError: Network request failed

Example:

balance = await client.get_balance("0991234567")
print(balance.balance)  # 50000

get_incoming_history(number=None, code=None, page=1) -> HistoryResponse | List[NumberWithCode]

Get incoming transaction history.

Parameters:

  • number (str, optional): Phone number
  • code (str, optional): Secret code (alternative to number)
  • page (int, optional): Page number (default: 1)

Returns:

  • HistoryResponse if number/code provided: Object with total (int) and transactions (List[Transaction])
  • List[NumberWithCode] if neither provided: List of active subscriptions

Example:

# Get history for a number
history = await client.get_incoming_history(number="0991234567", page=1)
print(history.total)  # 25
for tx in history.transactions:
    print(tx.amount, tx.from_gsm, tx.to_gsm)

# List all active numbers
numbers = await client.get_incoming_history()
for num in numbers:
    print(num.number, num.code)

get_outgoing_history(number=None, code=None, page=1) -> HistoryResponse | List[NumberWithCode]

Get outgoing transaction history. Same parameters and return types as get_incoming_history.

SyriatelCashClientSync (Sync)

All methods are the same as async client but without await:

client = SyriatelCashClientSync(api_token="your_token")
balance = client.get_balance("0991234567")
history = client.get_incoming_history(number="0991234567")

Data Models

BalanceResponse

@dataclass
class BalanceResponse:
    balance: int  # Balance in SYP

HistoryResponse

@dataclass
class HistoryResponse:
    total: int
    transactions: List[Transaction]

Transaction

@dataclass
class Transaction:
    transaction_no: str
    date: str
    from_gsm: str
    to_gsm: str
    amount: int
    fee: int
    net: int
    channel: str
    status: str

NumberWithCode

@dataclass
class NumberWithCode:
    number: str
    code: str

Error Handling

The library provides specific exception classes for different error types:

from syr_cash_api import (
    SyriatelCashError,           # Base exception
    InvalidTokenError,           # Invalid API token
    SubscriptionExpiredError,    # Subscription expired
    FetchFailedError,            # Fetch from Syriatel failed
    NotAuthorizedError,          # Account not authorized
    ServerMaintenanceError,      # Servers under maintenance
    NetworkError,                # Network issues
)

try:
    balance = await client.get_balance("0991234567")
except SubscriptionExpiredError:
    print("Subscription expired, please renew")
except InvalidTokenError:
    print("Invalid API token")
except NetworkError as e:
    print(f"Network error: {e}")
except SyriatelCashError as e:
    print(f"API error: {e.code} - {e.message}")

Advanced Usage

Custom Base URL

client = SyriatelCashClient(
    api_token="your_token",
    base_url="https://custom-api-url.com/v1"
)

Custom Timeout

client = SyriatelCashClient(
    api_token="your_token",
    timeout=60  # 60 seconds
)

Reusing Session (Async)

import aiohttp

async with aiohttp.ClientSession() as session:
    client = SyriatelCashClient(
        api_token="your_token",
        session=session
    )
    # Use client...
    # Session will not be closed when client closes

Phone Number Normalization

Phone numbers are automatically normalized:

  • +9639912345670991234567
  • 9639912345670991234567
  • 0991 234 5670991234567

Requirements

  • Python 3.7+
  • aiohttp >= 3.8.0
  • requests >= 2.28.0

License

MIT License

Support

For API support, visit: https://api.melchersman.com/syr-cash/

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

pysyriatel-2.0.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

pysyriatel-2.0.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file pysyriatel-2.0.0.tar.gz.

File metadata

  • Download URL: pysyriatel-2.0.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for pysyriatel-2.0.0.tar.gz
Algorithm Hash digest
SHA256 ff463c82ecd4bd8f396d9453df610b17ec76a70871dc83b0fcf83d487f74478e
MD5 baff9b184ea5a740750efa74b5129058
BLAKE2b-256 8b44b98957aed283ec5c944a92b56fbf4459e1c9fa9d9908ae5d77061f3c824f

See more details on using hashes here.

File details

Details for the file pysyriatel-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pysyriatel-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for pysyriatel-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 673f96c7593ee748247200d1230dea8e5ac46372508c7bcbac4c6c8f93b9b672
MD5 56f8d392a83f0fc1bd3d1eecab42ebf0
BLAKE2b-256 7041b619491b9173b1831e7c46be6bca6d3467a4ea929ba14c4ba3aac4ffe706

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