DEV Community

Cover image for Sending SMS Messages with Python and ClickSend
Foysal
Foysal

Posted on

Sending SMS Messages with Python and ClickSend

Installation

To start sending, all you need to do is install the ClickSend Client. Pick your preferred method below:

Install via Pip

pip install clicksend-client

Below is the full snippet you can use to send an SMS. It includes placeholders that will need to be replaced when implementing the code in your project.

import clicksend_client

**Send your first message
**Below is the full snippet you can use to send an SMS. It includes placeholders that will need to be replaced when implementing the code in your project.

USERNAME > Your ClickSend Username. Find it here.

API_KEY > Your ClickSend API Key. Find it here.

SOURCE > The origin identifier for your API request, which could be the name of your application or the source location for the request.

MESSAGE > The content of your SMS message.

TO_PHONE_NUMBER > The phone number you're sending the message to, including the country code.

from __future__ import print_function
import clicksend_client
from clicksend_client import SmsMessage
from clicksend_client.rest import ApiException

# Configure HTTP basic authorization: BasicAuth
configuration = clicksend_client.Configuration()
configuration.username = 'USERNAME'
configuration.password = 'API_KEY'

# Create an instance of the API class
api_instance = clicksend_client.SMSApi(clicksend_client.ApiClient(configuration))

# Configure your message
sms_message = SmsMessage(
    source="SOURCE",  # Replace this with your desired source name
    body="MESSAGE", # Write your message here
    to="TO_PHONE_NUMBER" # Enter the number you are sending to
)

sms_messages = clicksend_client.SmsMessageCollection(messages=[sms_message])


try:
    # Send an SMS message(s)
    api_response = api_instance.sms_send_post(sms_messages)
    print(api_response)
except ApiException as e:
    print("Exception when calling SMSApi->sms_send_post: %s\n" % e)
Enter fullscreen mode Exit fullscreen mode

The code explained
Import functions
Next, set up your Username and API Key so you can use the ClickSend API in your project.

from __future__ import print_function
import clicksend_client
from clicksend_client import SmsMessage
from clicksend_client.rest import ApiException
Enter fullscreen mode Exit fullscreen mode

Authorisation
Next, add your API credentials so you can use the ClickSend API in your project.

The following line of code will create an instance of the ACCOUNTAPI class, which is configured to use the APICLIENT with the credentials that we set up in the previous step. This instance will allow you to interact with the ClickSend API's account-related functionality.

configuration = clicksend_client.Configuration()
configuration.username = 'USERNAME'
configuration.password = 'API_KEY'
Create an instance of the API class
Next, configure the content of your message and define who you’re sending to-and-from.

api_instance = clicksend_client.SMSApi(clicksend_client.ApiClient(configuration))
Enter fullscreen mode Exit fullscreen mode

Configure your message
For testing purposes, we recommend you start by sending to your own number.

For additional properties that you can use here, see the Full API Reference.

Replace SOURCE with your preferred source (eg. the name of your application). This is not seen by recipients, but will help you to identify messages sent from various applications.
Replace MESSAGE with your own message.
Replace TO_PHONE_NUMBER with your own mobile number, including country code. For example, if you have an Australian number, it will start with +61.

sms_message = SmsMessage(
    source="SOURCE",
    body="MESSAGE",
    to="TO_PHONE_NUMBER"
)
Enter fullscreen mode Exit fullscreen mode

Send SMS message
If an exception occurs during the API call, it catches the APIEXCEPTION and prints an error message with the exception details. This structure helps you handle both successful API calls and potential errors that might occur during the process.

If an exception occurs during the API call, it catches the ApiException and prints an error message with the exception details. This structure helps you handle both successful API calls and potential errors that might occur during the process.

try:
    api_response = api_instance.sms_send_post(sms_messages)
    print(api_response)
except ApiException as e:
    print("Exception when calling SMSApi->sms_send_post: %s\n" % e)
Enter fullscreen mode Exit fullscreen mode

Follow me.

Top comments (0)