A client library for sending and managing SMS messages via the SMS Gateway for Android API
Project description
๐ฑ SMS Gateway for Androidโข Python API Client
A modern Python client for seamless integration with the SMSGate API. Send SMS messages programmatically through your Android devices with this powerful yet simple-to-use library.
๐ About The Project
The Python client for SMSGate provides a clean, type-safe interface to interact with the SMSGate API. It's designed specifically for Python developers who need to integrate SMS functionality into their applications with minimal setup and maximum reliability.
Key value propositions:
- ๐ Pythonic API - Designed with Python conventions and best practices in mind
- ๐ก๏ธ Robust Security - Guidance for secure credential handling and optional endโtoโend encryption
- ๐ Flexible Architecture - Supports both synchronous and asynchronous programming patterns
- ๐ป Type Safety - Full type hinting for better developer experience and fewer runtime errors
- ๐ Webhook Integration - Simplified webhook management for event-driven architectures
This client abstracts away the complexities of the underlying HTTP API while providing all the necessary functionality to send and track SMS messages through Android devices.
๐ Table of Contents
- ๐ฑ SMS Gateway for Androidโข Python API Client
โจ Features
- ๐ Dual Client: Supports both synchronous (
APIClient) and asynchronous (AsyncAPIClient) interfaces - ๐ Flexible Authentication: Supports both Basic Auth and JWT token authentication
- ๐ End-to-End Encryption: Optional message encryption using AES-256-CBC
- ๐ Multiple HTTP Backends: Native support for
requests,aiohttp, andhttpx - ๐ Webhook Management: Programmatically create, query, and delete webhooks
- โ๏ธ Customizable Base URL: Point to different API endpoints
- ๐ป Full Type Hinting: Fully typed for better development experience
- โ ๏ธ Robust Error Handling: Specific exceptions and clear error messages
- ๐ Delivery Reports: Track your message delivery status
- ๐ Token Management: Generate and revoke JWT tokens with custom scopes and TTL
โ๏ธ Requirements
- Python: 3.9 or higher
- HTTP Client (choose one):
Optional Dependencies:
- ๐ pycryptodome - For end-to-end encryption support
๐ฆ Installation
Basic Installation
pip install android-sms-gateway
Installation with Specific HTTP Client
# Choose an HTTP client:
pip install android-sms-gateway[requests] # For synchronous use
pip install android-sms-gateway[aiohttp] # For asynchronous use
pip install android-sms-gateway[httpx] # For both synchronous and asynchronous use
Installation with Encryption
# For encrypted messages:
pip install android-sms-gateway[encryption]
# Or install everything:
pip install android-sms-gateway[requests,encryption]
๐ Quickstart
Initial Setup
-
Configure your credentials:
export SMSGATE_USERNAME="your_username" export SMSGATE_PASSWORD="your_password"
-
Basic usage example:
import asyncio
import os
from android_sms_gateway import client, domain
# Configuration
login = os.getenv("SMSGATE_USERNAME")
password = os.getenv("SMSGATE_PASSWORD")
# Create message
message = domain.Message(
phone_numbers=["+1234567890"],
text_message=domain.TextMessage(
text="Hello! This is a test message.",
),
with_delivery_report=True,
)
# Synchronous Client
def sync_example():
with client.APIClient(login, password) as c:
# Send message
state = c.send(message)
print(f"Message sent with ID: {state.id}")
# Check status
status = c.get_state(state.id)
print(f"Status: {status.state}")
# Asynchronous Client
async def async_example():
async with client.AsyncAPIClient(login, password) as c:
# Send message
state = await c.send(message)
print(f"Message sent with ID: {state.id}")
# Check status
status = await c.get_state(state.id)
print(f"Status: {status.state}")
if __name__ == "__main__":
print("=== Synchronous Example ===")
sync_example()
print("\n=== Asynchronous Example ===")
asyncio.run(async_example())
Encryption Example
from android_sms_gateway import client, domain, Encryptor
# Encryption setup
encryptor = Encryptor("my-super-secure-secret-passphrase")
# Encrypted message
message = domain.Message(
phone_numbers=["+1234567890"],
text_message=domain.TextMessage(
text="This message will be encrypted!"
),
)
# Client with encryption
with client.APIClient(login, password, encryptor=encryptor) as c:
state = c.send(message)
print(f"Encrypted message sent: {state.id}")
JWT Authentication Example
import os
from android_sms_gateway import client, domain
# Option 1: Using an existing JWT token
jwt_token = os.getenv("ANDROID_SMS_GATEWAY_JWT_TOKEN")
# Create client with JWT token
with client.APIClient(login=None, password=jwt_token) as c:
message = domain.Message(
phone_numbers=["+1234567890"],
text_message=domain.TextMessage(
text="Hello from JWT authenticated client!",
),
)
# Option 2: Generate a new JWT token with Basic Auth
login = os.getenv("SMSGATE_USERNAME")
password = os.getenv("SMSGATE_PASSWORD")
with client.APIClient(login, password) as c:
# Generate a new JWT token with specific scopes and TTL
token_request = domain.TokenRequest(
scopes=["sms:send", "sms:read"],
ttl=3600 # Token expires in 1 hour
)
token_response = c.generate_token(token_request)
print(f"New JWT token: {token_response.access_token}")
print(f"Token expires at: {token_response.expires_at}")
# Use the new token for subsequent requests
with client.APIClient(login=None, password=token_response.access_token) as jwt_client:
message = domain.Message(
phone_numbers=["+1234567890"],
text_message=domain.TextMessage(
text="Hello from newly generated JWT token!",
),
)
state = jwt_client.send(message)
print(f"Message sent with new JWT token: {state.id}")
# Revoke the token when no longer needed
jwt_client.revoke_token(token_response.id)
print(f"Token {token_response.id} has been revoked")
๐ค Client Guide
Client Configuration
Both clients (APIClient and AsyncAPIClient) support these parameters:
| Parameter | Type | Description | Default |
|---|---|---|---|
login |
str |
API username | Required (for Basic Auth) |
password |
str |
API password or JWT token | Required |
base_url |
str |
API base URL | "https://api.sms-gate.app/3rdparty/v1" |
encryptor |
Encryptor |
Encryption instance | None |
http |
HttpClient/AsyncHttpClient |
Custom HTTP client | Auto-detected |
Authentication Options:
-
Basic Authentication (traditional):
client.APIClient(login="username", password="password")
-
JWT Token Authentication:
# Using an existing JWT token client.APIClient(login=None, password="your_jwt_token") # Or generate a token using Basic Auth first with client.APIClient(login="username", password="password") as c: token_request = domain.TokenRequest(scopes=["sms:send"], ttl=3600) token_response = c.generate_token(token_request) # Use the new token with client.APIClient(login=None, password=token_response.access_token) as jwt_client: # Make API calls with JWT authentication pass
Available Methods
| Method | Description | Return Type |
|---|---|---|
send(message: domain.Message) |
Send SMS message | domain.MessageState |
get_state(id: str) |
Check message status | domain.MessageState |
create_webhook(webhook: domain.Webhook) |
Create new webhook | domain.Webhook |
get_webhooks() |
List all webhooks | List[domain.Webhook] |
delete_webhook(id: str) |
Delete webhook | None |
generate_token(token_request: domain.TokenRequest) |
Generate JWT token | domain.TokenResponse |
revoke_token(jti: str) |
Revoke JWT token | None |
Data Structures
Message
class Message:
message: str # Message text
phone_numbers: List[str] # List of phone numbers
with_delivery_report: bool = True # Delivery report
is_encrypted: bool = False # Whether message is encrypted
# Optional fields
id: Optional[str] = None # Message ID
ttl: Optional[int] = None # Time-to-live in seconds
sim_number: Optional[int] = None # SIM number
MessageState
class MessageState:
id: str # Unique message ID
state: ProcessState # Current state (SENT, DELIVERED, etc.)
recipients: List[RecipientState] # Per-recipient status
is_hashed: bool # Whether message was hashed
is_encrypted: bool # Whether message was encrypted
Webhook
class Webhook:
id: Optional[str] # Webhook ID
url: str # Callback URL
event: WebhookEvent # Event type
TokenRequest
class TokenRequest:
scopes: List[str] # List of scopes for the token
ttl: Optional[int] = None # Time to live for the token in seconds
TokenResponse
class TokenResponse:
access_token: str # The JWT access token
token_type: str # The type of the token (e.g., 'Bearer')
id: str # The unique identifier of the token (jti)
expires_at: str # The expiration time of the token in ISO format
For more details, see domain.py.
๐ HTTP Clients
The library automatically detects installed HTTP clients with this priority:
| Client | Sync | Async |
|---|---|---|
| aiohttp | โ | 1๏ธโฃ |
| requests | 1๏ธโฃ | โ |
| httpx | 2๏ธโฃ | 2๏ธโฃ |
Using Specific Clients
from android_sms_gateway import client, http
# Force httpx usage
client.APIClient(..., http=http.HttpxHttpClient())
# Force requests usage
client.APIClient(..., http=http.RequestsHttpClient())
# Force aiohttp (async only)
async with client.AsyncAPIClient(..., http_client=http.AiohttpHttpClient()) as c:
# ...
Custom HTTP Client
Implement your own HTTP client following the http.HttpClient (sync) or ahttp.AsyncHttpClient (async) protocols.
๐ Security
Best Practices
โ ๏ธ IMPORTANT: Always follow these security practices:
- ๐ Credentials: Store credentials in environment variables
- ๐ซ Code: Never expose credentials in client-side code
- ๐ HTTPS: Use HTTPS for all production communications
- ๐ Encryption: Use end-to-end encryption for sensitive messages
- ๐ Rotation: Regularly rotate your credentials
JWT Security Best Practices
When using JWT authentication, follow these additional security practices:
- โฑ๏ธ Short TTL: Use short time-to-live (TTL) for tokens (recommended: 1 hour or less)
- ๐ Secure Storage: Store JWT tokens securely, preferably in memory or secure storage
- ๐ฏ Minimal Scopes: Request only the minimum necessary scopes for each token
- ๐ Token Rotation: Implement token refresh mechanisms before expiration
- ๐ Revocation: Immediately revoke compromised tokens using
revoke_token()
Secure Configuration Example
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Secure configuration
login = os.getenv("SMSGATE_USERNAME")
password = os.getenv("SMSGATE_PASSWORD")
if not login or not password:
raise ValueError("Credentials not configured!")
๐ API Reference
For complete API documentation including all available methods, request/response schemas, and error codes, visit: ๐ Official API Documentation
๐ฅ Contributing
Contributions are very welcome! ๐
How to Contribute
- ๐ด Fork the repository
- ๐ฟ Create your feature branch (
git checkout -b feature/NewFeature) - ๐พ Commit your changes (
git commit -m 'feat: add new feature') - ๐ค Push to branch (
git push origin feature/NewFeature) - ๐ Open a Pull Request
Development Environment
# Clone repository
git clone https://github.com/android-sms-gateway/client-py.git
cd client-py
# Create virtual environment
pipenv install --dev --categories encryption,requests
pipenv shell
Pull Request Checklist
- Code follows style standards (black, isort, flake8)
- Tests pass locally
- Documentation updated
- Test coverage maintained or improved
๐ License
This project is licensed under the Apache License 2.0 - see LICENSE for details.
๐ค Support
- ๐ง Email: support@sms-gate.app
- ๐ฌ Discord: SMS Gateway Community
- ๐ Documentation: docs.sms-gate.app
- ๐ Issues: GitHub Issues
Note: Android is a trademark of Google LLC. This project is not affiliated with or endorsed by Google.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file android_sms_gateway-3.1.1.tar.gz.
File metadata
- Download URL: android_sms_gateway-3.1.1.tar.gz
- Upload date:
- Size: 30.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10355b7ce47fdd9121ad5d4306580c58552a442236683876d420815021404f70
|
|
| MD5 |
f680ebd79cbb60dc694ddbe342246ae2
|
|
| BLAKE2b-256 |
aed4a3bd62d0f383b16bbc5e19da98301ce0af19cce1813145b009d6a78c858b
|
Provenance
The following attestation bundles were made for android_sms_gateway-3.1.1.tar.gz:
Publisher:
publish.yml on android-sms-gateway/client-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
android_sms_gateway-3.1.1.tar.gz -
Subject digest:
10355b7ce47fdd9121ad5d4306580c58552a442236683876d420815021404f70 - Sigstore transparency entry: 804367705
- Sigstore integration time:
-
Permalink:
android-sms-gateway/client-py@eb9bcf33779dd4148333edfd1872aac2f4b1187a -
Branch / Tag:
refs/tags/v3.1.1 - Owner: https://github.com/android-sms-gateway
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@eb9bcf33779dd4148333edfd1872aac2f4b1187a -
Trigger Event:
release
-
Statement type:
File details
Details for the file android_sms_gateway-3.1.1-py3-none-any.whl.
File metadata
- Download URL: android_sms_gateway-3.1.1-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb6dc5311404654664739eee4c2cd474a3acc26f7d37f6ca8cbe26eec4bd2055
|
|
| MD5 |
2d4413cf6dd75965c5899da891581be1
|
|
| BLAKE2b-256 |
dc36378be767834cf78b82e06538d3a9697d4f134bfce6db7db27d4429dd60b4
|
Provenance
The following attestation bundles were made for android_sms_gateway-3.1.1-py3-none-any.whl:
Publisher:
publish.yml on android-sms-gateway/client-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
android_sms_gateway-3.1.1-py3-none-any.whl -
Subject digest:
bb6dc5311404654664739eee4c2cd474a3acc26f7d37f6ca8cbe26eec4bd2055 - Sigstore transparency entry: 804367706
- Sigstore integration time:
-
Permalink:
android-sms-gateway/client-py@eb9bcf33779dd4148333edfd1872aac2f4b1187a -
Branch / Tag:
refs/tags/v3.1.1 - Owner: https://github.com/android-sms-gateway
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@eb9bcf33779dd4148333edfd1872aac2f4b1187a -
Trigger Event:
release
-
Statement type: