Python

Send SMS in Python with requests

Call https://api.taysend.com with the requests library. No official Python SDK — use Bearer sms_test_ or sms_live_ keys and an Idempotency-Key on every POST /v1/messages.

Send

Send and poll DLR

send.py
import os
import uuid
import requests

API_URL = os.environ.get("API_URL", "https://api.taysend.com")
KEY = os.environ["TAYSEND_KEY"]

r = requests.post(
    f"{API_URL}/v1/messages",
    headers={
        "Authorization": f"Bearer {KEY}",
        "Idempotency-Key": str(uuid.uuid4()),
        "Content-Type": "application/json",
    },
    json={
        "to": "+233200000001",
        "sender_id": "TAYSEND",
        "message": "Hello from Taysend",
        "type": "transactional",
    },
    timeout=30,
)
r.raise_for_status()
msg = r.json()
print(msg["id"])

# Poll delivery report
dlr = requests.get(
    f"{API_URL}/v1/messages/{msg['id']}",
    headers={"Authorization": f"Bearer {KEY}"},
    timeout=30,
)
print(dlr.json())

OTP

OTP verification

otp.py
r = requests.post(
    f"{API_URL}/v1/verify/send",
    headers={
        "Authorization": f"Bearer {KEY}",
        "Content-Type": "application/json",
    },
    json={"to": "+233200000001", "locale": "en"},
    timeout=30,
)
print(r.status_code, r.json())

Check codes with POST /v1/verify/check. See OTP reference.

Env

Environment variables

  • TAYSEND_KEY — sms_test_ or sms_live_ API key
  • API_URL — optional override (default https://api.taysend.com)

Explore

Related

Each page answers a different question. They are not copies of this one.

Next

Send your first Python SMS with a TEST key.