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.
- Make sure you’re using the latest version of Python.
Installation
Install Typescript
You will need to install TypeScript to compile the code. Use the following command:
npm install typescript
Compile TypeScript into JavaScript
Compile TypeScript into JavaScript by running the following commands:
npm add request http bluebird @types/node
tsc --target es5 /node_modules/clicksend/api.ts
Add the SDK to your project
Copy the api.js file along with the node_modules directory into your project to use the library. Include this in your file to use the SDK:
var api = require('./node_modules/clicksend/api.js');
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. |
var api = require('./api.js');
var smsMessage = new api.SmsMessage();
smsMessage.source = "SOURCE";
smsMessage.body = "MESSAGE";
smsMessage.to = "TO_PHONE_NUMBER";
var smsApi = new api.SMSApi("USERNAME", "API_KEY");
var smsCollection = new api.SmsMessageCollection();
smsCollection.messages = [smsMessage];
smsApi.smsSendPost(smsCollection)
.then(function(response) {
console.log(response.body);
})
.catch(function(err) {
console.error(err.body);
});
The code explained
Import modules
Prepare your environment to use the ClickSend API by importing the required SDK and required modules.
var api = require('./api.js');
Create an instance of the API class
The following snippet of code will create an instance of the SmsMessage class. This instance will be called in the following steps, allowing you to interact with ClickSend’s SMS-related functionality.
var smsMessage = new api.SmsMessage();
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.
smsMessage.source = "SOURCE";
smsMessage.body = "MESSAGE";
smsMessage.to = "TO_PHONE_NUMBER";
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. This creates an instance of the SMSApi class, which passes your API credentials as parameters.
var smsApi = new api.SMSApi("USERNAME", "API_KEY");
Create an SMS message collection
Next, we’ll create a collection and add the smsMessage
that you created earlier.
var smsMessage = new api.SmsMessage();
smsCollection.messages = [smsMessage];
Send SMS message
Finally, try to call the smsSendPost method. If everything works as expected then it will print the formatted response.
If an error occurs during the API call, the catch function will log the error message body. This structure helps you handle both successful API calls and potential errors that might occur during the process.
smsApi.smsSendPost(smsCollection)
.then(function(response) {
console.log(response.body);
})
.catch(function(err) {
console.error(err.body);
});