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.1.0.tar.gz (10.3 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.1.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pysyriatel-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a70f00868e51f07b6c558968f6d00578c4b0c986222824eaed23a747f323ac40
MD5 885b1b44226ff8cc57d78c8a65364898
BLAKE2b-256 e43e07c54ff5d85bb26892a1df360add2c3f069887b5aac5d60bd2c7ca0128bd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pysyriatel-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 65966c4487dddc63ad189648635ef1a726059611726fe13cfc14c6d708d6ef27
MD5 8ef5580c08c8eed2f97225b8150ef319
BLAKE2b-256 24612f8845f731d5a1ef152f60e6b9f46a7729c2ad12d2be9e3a09e8a2e58ab4

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