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 Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set http [new_CkHttp]
CkHttp_put_RequireSslCertVerify $http 1
# Supply the key for this One API integration from application configuration.
CkHttp_SetRequestHeader $http "Authorization" "ONE_API_KEY"
CkHttp_SetRequestHeader $http "Accept" "application/json"
set body [new_CkJsonObject]
CkJsonObject_UpdateString $body "messages[0].channel" "sms"
CkJsonObject_UpdateString $body "messages[0].to" "PHONE_NUMBER"
CkJsonObject_UpdateString $body "messages[0].content" "Test message: your order is ready."
# For a two-way sender, supply the number provisioned on the integration.
set sender ""
if {$sender != ""} then {
CkJsonObject_UpdateString $body "messages[0].from" $sender
}
set resp [new_CkHttpResponse]
set success [CkHttp_HttpJson $http "POST" "https://platform.clickatell.com/v1/message" $body "application/json" $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
puts "The send outcome is unknown. Check the message report before resending."
delete_CkHttp $http
delete_CkJsonObject $body
delete_CkHttpResponse $resp
exit
}
set reply [new_CkJsonObject]
set parsed [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
if {expr [$success != 1] || [expr [[CkHttpResponse_get_StatusCode $resp] != 202] && [[CkHttpResponse_get_StatusCode $resp] != 207]]} then {
puts "Clickatell returned HTTP [CkHttpResponse_get_StatusCode $resp]"
if {$parsed == 1} then {
if {[CkJsonObject_HasMember $reply "error.description"] == 1} then {
puts [CkJsonObject_stringOf $reply error.description]
}
}
delete_CkHttp $http
delete_CkJsonObject $body
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
if {$parsed != 1} then {
puts "Expected a JSON response."
delete_CkHttp $http
delete_CkJsonObject $body
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
# A request-level error applies to the submission as a whole.
if {[CkJsonObject_HasMember $reply "error.code"] == 1} then {
if {[CkJsonObject_IsNullOf $reply "error.code"] != 1} then {
puts "Submission error [CkJsonObject_stringOf $reply error.code] [CkJsonObject_stringOf $reply error.description]"
delete_CkHttp $http
delete_CkJsonObject $body
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
}
set count [CkJsonObject_SizeOfArray $reply "messages"]
if {$count <= 0} then {
puts "No recipient results returned. Check the message report before resending."
delete_CkHttp $http
delete_CkJsonObject $body
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
set i 0
while {$i < $count} {
CkJsonObject_put_I $reply $i
set resultNumber [expr $i + 1]
# JsonTypeOf returns 5 for a JSON boolean.
if {[CkJsonObject_JsonTypeOf $reply "messages[i].accepted"] != 5} then {
puts "Result $resultNumber has no acceptance flag."
} else {
if {[CkJsonObject_BoolOf $reply "messages[i].accepted"] == 1} then {
set messageId [CkJsonObject_stringOf $reply "messages[i].apiMessageId"]
if {expr [[CkJsonObject_JsonTypeOf $reply "messages[i].apiMessageId"] != 1] || [$messageId == ""]} then {
puts "Result $resultNumber was accepted without a tracking ID."
} else {
puts "Result $resultNumber accepted. Message ID: $messageId"
}
} else {
puts "Result $resultNumber rejected: [CkJsonObject_stringOf $reply {messages[i].error.code}] [CkJsonObject_stringOf $reply {messages[i].error.description}]"
}
}
set i [expr $i + 1]
}
delete_CkHttp $http
delete_CkJsonObject $body
delete_CkHttpResponse $resp
delete_CkJsonObject $reply