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 syr_cash_api import SyriatelCashClient

async def main():
    # Using async context manager (recommended)
    async with SyriatelCashClient(api_token="your_api_token") as client:
        # Get balance
        balance = await client.get_balance("0991234567")
        print(f"Balance: {balance.balance} SYP")
        
        # Get incoming history
        history = await client.get_incoming_history(number="0991234567", page=1)
        print(f"Total transactions: {history.total}")
        for tx in history.transactions:
            print(f"{tx.date}: {tx.amount} SYP from {tx.from_gsm}")
        
        # 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 syr_cash_api import SyriatelCashClientSync

# Using context manager (recommended)
with SyriatelCashClientSync(api_token="your_api_token") as client:
    # Get balance
    balance = client.get_balance("0991234567")
    print(f"Balance: {balance.balance} SYP")
    
    # Get incoming history
    history = client.get_incoming_history(number="0991234567", page=1)
    print(f"Total transactions: {history.total}")
    
    # 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-1.0.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

pysyriatel-1.0.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pysyriatel-1.0.0.tar.gz
Algorithm Hash digest
SHA256 47fd9e8aec8acab53ff791ff878f3d4f032fee5a11031f7a5800fec800172922
MD5 576fcb926f4401a28d0f7729a649c6b5
BLAKE2b-256 635ac3089b738468e1fd2d358813e595dce4c1411a0d8f13778ad1df30cc8d16

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pysyriatel-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fdf8883f5128f1c02c8c93e75aa6793899f24d4b99f94ecc8fee8146a3ea973c
MD5 3bcfdd5f443ef9a633b67b8cf0dcdd47
BLAKE2b-256 a6171b8d23edd4f57490915df35a881f368e6365aa8025193388ed9a36765427

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