Clickatell One API: Send an SMS and get its message ID
See more Clickatell Examples
Send one text using an SMS-enabled One API integration. Supply an international phone number starting with the country code, using digits only. For a US two-way integration, set from to its provisioned two-way number. With a test integration, use a registered test phone.
HTTP 202 means the submission was processed. Inspect accepted for each message. Save the returned apiMessageId with the application record and use it to look up delivery status. When a multi-message submission returns HTTP 207, check each result before retrying any rejected message.
Already using the SMS-only REST integration? Its documented request is POST /messages with the integration key in Authorization and a body such as {"content":"Test message","to":["PHONE_NUMBER"]}. One API has a different request body and a separate integration key. SMS API reference
Chilkat VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
' Supply the key for this One API integration from application configuration.
http.SetRequestHeader "Authorization","ONE_API_KEY"
http.SetRequestHeader "Accept","application/json"
set body = CreateObject("Chilkat.JsonObject")
success = body.UpdateString("messages[0].channel","sms")
success = body.UpdateString("messages[0].to","PHONE_NUMBER")
success = body.UpdateString("messages[0].content","Test message: your order is ready.")
' For a two-way sender, supply the number provisioned on the integration.
sender = ""
If (sender <> "") Then
success = body.UpdateString("messages[0].from",sender)
End If
set resp = CreateObject("Chilkat.HttpResponse")
success = http.HttpJson("POST","https://platform.clickatell.com/v1/message",body,"application/json",resp)
If (resp.StatusCode = 0) Then
outFile.WriteLine("The send outcome is unknown. Check the message report before resending.")
WScript.Quit
End If
set reply = CreateObject("Chilkat.JsonObject")
parsed = reply.Load(resp.BodyStr)
If ((success <> 1) Or ((resp.StatusCode <> 202) And (resp.StatusCode <> 207))) Then
outFile.WriteLine("Clickatell returned HTTP " & resp.StatusCode)
If (parsed = 1) Then
If (reply.HasMember("error.description") = 1) Then
outFile.WriteLine(reply.StringOf("error.description"))
End If
End If
WScript.Quit
End If
If (parsed <> 1) Then
outFile.WriteLine("Expected a JSON response.")
WScript.Quit
End If
' A request-level error applies to the submission as a whole.
If (reply.HasMember("error.code") = 1) Then
If (reply.IsNullOf("error.code") <> 1) Then
outFile.WriteLine("Submission error " & reply.StringOf("error.code") & " " & reply.StringOf("error.description"))
WScript.Quit
End If
End If
count = reply.SizeOfArray("messages")
If (count <= 0) Then
outFile.WriteLine("No recipient results returned. Check the message report before resending.")
WScript.Quit
End If
i = 0
Do While (i < count)
reply.I = i
resultNumber = i + 1
' JsonTypeOf returns 5 for a JSON boolean.
If (reply.JsonTypeOf("messages[i].accepted") <> 5) Then
outFile.WriteLine("Result " & resultNumber & " has no acceptance flag.")
Else
If (reply.BoolOf("messages[i].accepted") = 1) Then
messageId = reply.StringOf("messages[i].apiMessageId")
If ((reply.JsonTypeOf("messages[i].apiMessageId") <> 1) Or (messageId = "")) Then
outFile.WriteLine("Result " & resultNumber & " was accepted without a tracking ID.")
Else
outFile.WriteLine("Result " & resultNumber & " accepted. Message ID: " & messageId)
End If
Else
outFile.WriteLine("Result " & resultNumber & " rejected: " & reply.StringOf("messages[i].error.code") & " " & reply.StringOf("messages[i].error.description"))
End If
End If
i = i + 1
Loop
outFile.Close