Skip to main content

A Python client library for the MSG91 SMS API

Project description

MSG91 Python Client

CI PyPI Version Python Versions

A Python client library for the MSG91 API.

Installation

pip install msg91-py

Note: The package name on PyPI is msg91-py, but the module name for imports is still msg91.

Usage

Initialize the client

from msg91 import Client

# Initialize client with your auth key
client = Client("your_auth_key")

Sending SMS

# Send SMS using the standard API
response = client.sms.send(
    mobile="919XXXXXXXXX",
    message="Hello, this is a test message from MSG91!",
    sender="SENDER",
    route="4",
    country="91"
)
print(response)

# Send bulk SMS
response = client.sms.send(
    mobile=["919XXXXXXXXX", "918XXXXXXXXX"],
    message="Hello, this is a bulk message!",
    sender="SENDER",
    route="4"
)
print(response)

# Send SMS using a template (Flow API)
response = client.sms.send_template(
    template_id="your_template_id",
    mobile="919XXXXXXXXX",
    variables={"var1": "value1", "var2": "value2"},
    sender_id="SENDER"
)
print(response)

Managing Templates

# Create a new template
response = client.template.create(
    template_name="Welcome",
    template_body="Welcome to our service, {{var1}}!",
    sender_id="SENDER",
    sms_type="NORMAL"  # Options: NORMAL, UNICODE
)
print(response)

# Get template versions
template_versions = client.template.get("template_id")
print(template_versions)

# Add a new version to an existing template
response = client.template.add_version(
    template_id="template_id",
    template_body="Welcome to our service, {{var1}}! New version.",
    sender_id="SENDER"
)
print(response)

# Set a template version as default
response = client.template.set_default(
    template_id="template_id",
    version_id="version_id"
)
print(response)

OTP (One-Time Password)

# Send OTP
response = client.otp.send(
    mobile="919XXXXXXXXX"
)
print(response)
session_id = response.get("message")

# Send OTP with custom settings
response = client.otp.send(
    mobile="919XXXXXXXXX",
    message="Your login code is ##OTP##. Valid for 5 minutes.",
    sender="MYAPP",
    otp_length=6,
    otp_expiry=5
)
print(response)

# Verify OTP
response = client.otp.verify(
    mobile="919XXXXXXXXX",
    otp="123456"
)
print(response)

if response.get("type") == "success":
    print("OTP verified successfully!")

# Resend OTP via SMS
response = client.otp.resend(
    mobile="919XXXXXXXXX",
    retrytype="text"
)
print(response)

# Resend OTP via Voice
response = client.otp.resend(
    mobile="919XXXXXXXXX",
    retrytype="voice"
)
print(response)

Logs and Analytics

# Get SMS logs
logs = client.sms.get_logs(
    start_date="2023-01-01",
    end_date="2023-01-31"
)
print(logs)

# Get analytics
analytics = client.sms.get_analytics()
print(analytics)

# Get analytics for specific date range
analytics = client.sms.get_analytics(
    start_date="2023-01-01",
    end_date="2023-01-31"
)
print(analytics)

API Endpoints

The client uses the following MSG91 API endpoints:

SMS:

  • Send SMS: http://api.msg91.com/api/v2/sendsms (v2 API)
  • Send Template SMS: flow (v5 API)
  • SMS Logs: report/logs/p/sms
  • SMS Analytics: report/analytics/p/sms

Templates:

  • Create Template: sms/addTemplate
  • Add Template Version: sms/addTemplateVersion
  • Get Template Versions: sms/getTemplateVersions
  • Mark Template as Default: sms/markActive

OTP:

  • Send OTP: http://api.msg91.com/api/sendotp.php
  • Verify OTP: http://api.msg91.com/api/verifyRequestOTP.php
  • Resend OTP: http://api.msg91.com/api/retryotp.php

Requirements

  • Python 3.9+
  • httpx

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

msg91_py-0.2.0.tar.gz (50.9 kB view details)

Uploaded Source

Built Distribution

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

msg91_py-0.2.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file msg91_py-0.2.0.tar.gz.

File metadata

  • Download URL: msg91_py-0.2.0.tar.gz
  • Upload date:
  • Size: 50.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for msg91_py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1a9a736a66e5d5b5512233862de7682f9a566e80cfebf2828e1ef5fb8b3182e2
MD5 442c8a4449bcf4bf51ce4741a456ead0
BLAKE2b-256 9314ae5c554967d9195d897ed131a2e100a1e4ae17e4bcd9d2f5850983e6125d

See more details on using hashes here.

Provenance

The following attestation bundles were made for msg91_py-0.2.0.tar.gz:

Publisher: publish.yml on karambir/msg91-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file msg91_py-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: msg91_py-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for msg91_py-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6b6de630364e8e20db0fbae59e39975db23f60669911f72ebac32c197113bd8
MD5 0cefb11c7edbd71371209babebf516d7
BLAKE2b-256 b3f03355dee52f7b430b8c41b1ecd7d904478025ef7464d30422421d0e5a8c6b

See more details on using hashes here.

Provenance

The following attestation bundles were made for msg91_py-0.2.0-py3-none-any.whl:

Publisher: publish.yml on karambir/msg91-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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