Skip to main content

Python SDK for AfroMessage API to send SMS and OTP

Project description

AfroMessage Python SDK

A complete Python SDK for the AfroMessage SMS and OTP API.
This package makes it easy to send single SMS, bulk SMS, and handle OTP challenges with minimal setup.


🚀 Installation

Install the package via pip:

pip install afromessage

Or from source:

git clone https://github.com/yonas8989/afromessage-python.git
cd afromessage-python
pip install -e .

🔑 Quick Start

Create a .env file in your project root:

AFROMESSAGE_TOKEN=your_api_token_here

Then in Python:

import os
from dotenv import load_dotenv
from afromessage import AfroMessage
from afromessage.models.sms_models import SendSMSRequest, BulkSMSRequest
from afromessage.models.otp_models import SendOTPRequest, VerifyOTPRequest

# Load environment variables
load_dotenv()
token = os.getenv("AFROMESSAGE_TOKEN")

if not token:
    raise ValueError("⚠️ Set AFROMESSAGE_TOKEN in your .env file!")

# Initialize the SDK
sdk = AfroMessage(token=token)

# --- Single SMS ---
sms_request = SendSMSRequest(
    to="+2519xxxxxxx",
    message="Hello from AfroMessage SDK!",
    from_="MySender",
    sender="MyBrand"
)
response = sdk.sms.send(sms_request)
print("✅ Single SMS:", response)

# --- Bulk SMS ---
bulk_request = BulkSMSRequest(
    to=["+2519xxxxxxx", "+2519yyyyyyy"],
    message="Hello, bulk users!",
    from_="MySender",
    sender="MyBrand",
    campaign="MyCampaign"
)
bulk_response = sdk.sms.bulk_send(bulk_request)
print("✅ Bulk SMS:", bulk_response)

# --- OTP Challenge ---
otp_request = SendOTPRequest(
    to="+2519xxxxxxx",
    pr="Your code",
    len_=6
)
otp_response = sdk.otp.send(otp_request)
print("✅ OTP Challenge:", otp_response)

# --- Verify OTP ---
verify_request = VerifyOTPRequest(
    to="+2519xxxxxxx",
    code="123456"
)
verify_response = sdk.otp.verify(verify_request)
print("✅ OTP Verify:", verify_response)

📦 Features

✅ Send single SMS

✅ Send bulk SMS campaigns

✅ Generate OTP challenges

✅ Verify OTP codes

✅ Built-in error handling

✅ Request/response logging for debugging

⚡ API Reference

Single SMS

from afromessage.models.sms_models import SendSMSRequest

sms_request = SendSMSRequest(
    to="+2519xxxxxxx",
    message="Hello from AfroMessage!",
    from_="MySender",
    sender="MyBrand",
    callback="https://myapp.com/callback"  # optional
)

response = sdk.sms.send(sms_request)

Single SMS (GET request)

response = sdk.sms.send_get(sms_request)

Bulk SMS

from afromessage.models.sms_models import BulkSMSRequest

bulk_request = BulkSMSRequest(
    to=["+2519xxxxxxx", "+2519yyyyyyy"],
    message="Hello, everyone!",
    from_="MySender",
    sender="MyBrand",
    campaign="MyCampaign"
)

response = sdk.sms.bulk_send(bulk_request)

Bulk SMS (Personalized messages)

from afromessage.models.sms_models import BulkRecipient, BulkSMSRequest

personalized_request = BulkSMSRequest(
    to=[
        BulkRecipient(to="+2519xxxxxxx", message="Hi Yonas!"),
        BulkRecipient(to="+2519yyyyyyy", message="Hi Eshetu!")
    ],
    from_="MySender",
    sender="MyBrand",
    campaign="PersonalizedCampaign"
)

response = sdk.sms.bulk_send(personalized_request)

OTP Challenge

from afromessage.models.otp_models import SendOTPRequest

otp_request = SendOTPRequest(
    to="+2519xxxxxxx",
    pr="Your code",
    len_=6,
    ttl=300  # time-to-live in seconds (optional)
)

response = sdk.otp.send(otp_request)

Verify OTP

from afromessage.models.otp_models import VerifyOTPRequest

verify_request = VerifyOTPRequest(
    to="+2519xxxxxxx",
    code="123456"
)

response = sdk.otp.verify(verify_request)

🛠 Error Handling

All errors are wrapped and logged automatically.

Example:

try:
    response = sdk.sms.send(sms_request)
    print("✅ Success:", response)
except Exception as e:
    print("❌ Error:", str(e))

Errors include:

API errors (invalid request, authentication failure, etc.)

Network errors (timeouts, connection issues)

📑 Logging

Requests and responses are logged automatically. Example:

📤 [POST] Request to: send
   Payload: {
     "to": "+2519xxxxxxx",
     "message": "Hello!"
   }

📥 Response from: send
   Data: {
     "acknowledge": "success",
     "response": { "code": "202", "to": "+2519xxxxxxx" }
   }

🧪 Advanced Example (Real Test)

We provide a full interactive test script in usag/test_real.py

.

It shows how to send SMS, bulk SMS, and run OTP challenge + verification with .env loading.

Run it with:

python usage/test_real.py

⚠️ Note: Running this will send real SMS/OTP messages and may incur costs.

🤝 Contributing

Contributions are welcome! To contribute:

1. Fork the repo

2. Create your feature branch (git checkout -b feature/my-feature)

3. Commit your changes (git commit -m 'Add my feature')

4. Push to the branch (git push origin feature/my-feature)

5. Open a Pull Request

Run tests before submitting:

pytest

📜 License

This project is licensed under the MIT License.

See LICENSE

for details.

🙋 Support

For issues or feature requests, open a GitHub Issue

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

afromessage-0.1.2.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

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

afromessage-0.1.2-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file afromessage-0.1.2.tar.gz.

File metadata

  • Download URL: afromessage-0.1.2.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for afromessage-0.1.2.tar.gz
Algorithm Hash digest
SHA256 df19db6a2afff0e861f38016daefc328239f5873a763d2dcf6b2cfbc8f14011a
MD5 2df4d280509eddd0a9acc636bfcf3177
BLAKE2b-256 2d21ea87be01b9318258d25c997f0b377a0ec35335dfc180495ccc0a066858e4

See more details on using hashes here.

File details

Details for the file afromessage-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: afromessage-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for afromessage-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9513bcc92d75b21c553995df30701dd10462fd2dd02691724ceb4fec1ad949c5
MD5 f95c642b5e079e471b85cc348cdc99b2
BLAKE2b-256 c635089d817e62bf1cdd4077f4854b5e15392338dd15f015bcaa62a1aa99059c

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