Before you start
- You need a ClickSend account with credit to start sending. If you don’t have an account, create one here.
- If you need to add more credit to your account, you can top up here. You’ll be credited $2 AUD free credit when you sign up.
Installation
To start sending, all you need to do is install the ClickSend Client. Pick your preferred method below:
pip install clicksend-client
Import the package using the following function:
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.
Placeholder | Replace with |
---|---|
| Your ClickSend Username. Find it here. |
| Your ClickSend API Key. Find it here. |
| The origin identifier for your API request, which could be the name of your application or the source location for the request. |
| The content of your SMS message. |
| 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)
The code explained
Import functions
Start by preparing your environment to use the ClickSend API. Import the necessary modules, including the ClickSend client library and exception handling classes.
from __future__ import print_function
import clicksend_client
from clicksend_client import SmsMessage
from clicksend_client.rest import ApiException
Authorisation
Next, add your API credentials so you can use the ClickSend API in your project.
In the following snippet, simply replace the USERNAME
and API_KEY
placeholders with your own Username and API Key. These can be found on your Dashboard here.
configuration = clicksend_client.Configuration()
configuration.username = 'USERNAME'
configuration.password = 'API_KEY'
Create an instance of the API class
The following snippet of code will create an instance of the SMSApi
class, which is configured to work with the credentials that we set up in the previous step. This instance will be called in the following steps, allowing you to interact with ClickSend’s SMS-related functionality.
api_instance = clicksend_client.SMSApi(clicksend_client.ApiClient(configuration))
Configure your message
It’s time to configure the content of your message and specify who you’re sending to. This code includes everything that you need to send an SMS message. To see what other parameters you can include, see our full API reference.
Now, it’s a simple matter of a few tweaks:
- 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"
)
Send SMS message
Finally, try to call the sms_send_post
method using the api_instance
you created earlier. If everything works as expected then it will print the formatted response.
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)