A Python client library for the Asanak SMS API. This package provides a simple and intuitive interface to interact with Asanak SMS services.
You can install the package via pip:
pip install asanak-apifrom asanak_api import AsanakAPI
# Initialize the client
api = AsanakAPI(username="your_username", password="your_password")
# Or use the API key format (username,password)
# api = AsanakAPI(api_key="your_username,your_password")
# Send an SMS
response = api.sms_send({
"source": "your_source_number",
"destination": ["destination_number1", "destination_number2"],
"message": "Hello from Asanak API!"
})
print(response)Asanak SMS provider uses username and password for authentication. You can provide these credentials in two ways:
-
Separate username and password parameters:
api = AsanakAPI(username="your_username", password="your_password")
-
Combined API key:
api = AsanakAPI(api_key="your_username,your_password")
-
Send SMS:
api.sms_send({ "source": "your_source_number", "destination":"recipient_number1,recipient_number2,...", "message": "Your message content" })
-
Send Template SMS:
api.sms_template({ "source": "your_source_number", "destination": "recipient_number1,recipient_number2,...", "message": "Your template name", "variables": {"var1": "value1", "var2": "value2"} })
-
Send Peer-to-Peer SMS:
api.sms_p2p( { { "source": "your_source_number", "destinations": "recipient_number1", "messages": "Message for recipient1" }, { "source": "your_source_number", "destinations": "recipient2", "messages": "Message for recipient2" } } )
-
Check SMS Status:
api.sms_status({"msgid": "your_message_id"})
-
Get Templates List:
api.sms_template_list()
-
Get Credit:
api.get_credit()
-
Get Rial Credit:
api.get_rial_credit()
All API methods return responses in the following format:
{
"status": status_code, # Status code of the response
"message": "message", # Message describing the status
"data": {...} # Response data
}The library includes predefined status codes that help you interpret API responses:
- SMS Status codes - Available in
SMS_STATUSdictionary - API Status codes - Available in
API_STATUSdictionary
The library can raise the following exceptions:
APIException: For API-related errorsHTTPException: For HTTP connection issues
from asanak_api import AsanakAPI, APIException, HTTPException
try:
api = AsanakAPI(api_key="your_api_key")
response = api.sms_send({...})
except APIException as e:
print(f"API Error: {e}")
except HTTPException as e:
print(f"HTTP Error: {e}")- Python >= 3.12
- requests >= 2.32.3