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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oBody
LOCAL cSender
LOCAL oResp
LOCAL oReply
LOCAL nParsed
LOCAL nCount
LOCAL i
LOCAL nResultNumber
LOCAL cMessageId
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
// Supply the key for this One API integration from application configuration.
oHttp:SetRequestHeader("Authorization", "ONE_API_KEY")
oHttp:SetRequestHeader("Accept", "application/json")
oBody := CreateObject("Chilkat.JsonObject")
oBody:UpdateString("messages[0].channel", "sms")
oBody:UpdateString("messages[0].to", "PHONE_NUMBER")
oBody:UpdateString("messages[0].content", "Test message: your order is ready.")
// For a two-way sender, supply the number provisioned on the integration.
cSender := ""
IF (cSender != "")
oBody:UpdateString("messages[0].from", cSender)
ENDIF
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://platform.clickatell.com/v1/message", oBody, "application/json", oResp)
IF (oResp:StatusCode == 0)
? "The send outcome is unknown. Check the message report before resending."
oHttp:destroy()
oBody:destroy()
oResp:destroy()
RETURN
ENDIF
oReply := CreateObject("Chilkat.JsonObject")
nParsed := oReply:Load(oResp:BodyStr)
IF ((nSuccess != 1) .OR. ((oResp:StatusCode != 202) .AND. (oResp:StatusCode != 207)))
? "Clickatell returned HTTP " + Str(oResp:StatusCode)
IF (nParsed == 1)
IF (oReply:HasMember("error.description") == 1)
? oReply:StringOf("error.description")
ENDIF
ENDIF
oHttp:destroy()
oBody:destroy()
oResp:destroy()
oReply:destroy()
RETURN
ENDIF
IF (nParsed != 1)
? "Expected a JSON response."
oHttp:destroy()
oBody:destroy()
oResp:destroy()
oReply:destroy()
RETURN
ENDIF
// A request-level error applies to the submission as a whole.
IF (oReply:HasMember("error.code") == 1)
IF (oReply:IsNullOf("error.code") != 1)
? "Submission error " + oReply:StringOf("error.code") + " " + oReply:StringOf("error.description")
oHttp:destroy()
oBody:destroy()
oResp:destroy()
oReply:destroy()
RETURN
ENDIF
ENDIF
nCount := oReply:SizeOfArray("messages")
IF (nCount <= 0)
? "No recipient results returned. Check the message report before resending."
oHttp:destroy()
oBody:destroy()
oResp:destroy()
oReply:destroy()
RETURN
ENDIF
i := 0
DO WHILE (i < nCount)
oReply:I := i
nResultNumber := i + 1
// JsonTypeOf returns 5 for a JSON boolean.
IF (oReply:JsonTypeOf("messages[i].accepted") != 5)
? "Result " + Str(nResultNumber) + " has no acceptance flag."
ELSE
IF (oReply:BoolOf("messages[i].accepted") == 1)
cMessageId := oReply:StringOf("messages[i].apiMessageId")
IF ((oReply:JsonTypeOf("messages[i].apiMessageId") != 1) .OR. (cMessageId == ""))
? "Result " + Str(nResultNumber) + " was accepted without a tracking ID."
ELSE
? "Result " + Str(nResultNumber) + " accepted. Message ID: " + cMessageId
ENDIF
ELSE
? "Result " + Str(nResultNumber) + " rejected: " + oReply:StringOf("messages[i].error.code") + " " + oReply:StringOf("messages[i].error.description")
ENDIF
ENDIF
i := i + 1
ENDDO
oHttp:destroy()
oBody:destroy()
oResp:destroy()
oReply:destroy()