Skip to main content

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.

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.1.tar.gz (51.1 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.1-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for msg91_py-0.2.1.tar.gz
Algorithm Hash digest
SHA256 3500f03450e18bd74d55174d8ad83f7e7cef5dc993239b328e8f04b6ea7a277d
MD5 e45b1f7e20a3f5a7f3d9b501ae8be25c
BLAKE2b-256 61448fcf896da17dbf7470f579386fe8c491f7669de07956e3b8d18925043494

See more details on using hashes here.

Provenance

The following attestation bundles were made for msg91_py-0.2.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: msg91_py-0.2.1-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.13.7

File hashes

Hashes for msg91_py-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d0198f20a136195011e923ac1cb1b053e7572f9b60b635e11638bdaa1548ec17
MD5 27bfb698d758fbc5cb648923269aa4e7
BLAKE2b-256 48e2abc86cc0bcf18ae32689fb5e42e9a88d42a7de82a5b603fef764117c05a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for msg91_py-0.2.1-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.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 files

0.2.0

2 files

0.1.7

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page