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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Variant vBody
Handle hoBody
String sSender
Variant vResp
Handle hoResp
Handle hoReply
Boolean iParsed
Integer iCount
Integer i
Integer iResultNumber
String sMessageId
String sTemp1
String sTemp2
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
// Supply the key for this One API integration from application configuration.
Send ComSetRequestHeader To hoHttp "Authorization" "ONE_API_KEY"
Send ComSetRequestHeader To hoHttp "Accept" "application/json"
Get Create (RefClass(cComChilkatJsonObject)) To hoBody
If (Not(IsComObjectCreated(hoBody))) Begin
Send CreateComObject of hoBody
End
Get ComUpdateString Of hoBody "messages[0].channel" "sms" To iSuccess
Get ComUpdateString Of hoBody "messages[0].to" "PHONE_NUMBER" To iSuccess
Get ComUpdateString Of hoBody "messages[0].content" "Test message: your order is ready." To iSuccess
// For a two-way sender, supply the number provisioned on the integration.
Move "" To sSender
If (sSender <> "") Begin
Get ComUpdateString Of hoBody "messages[0].from" sSender To iSuccess
End
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoBody to vBody
Get pvComObject of hoResp to vResp
Get ComHttpJson Of hoHttp "POST" "https://platform.clickatell.com/v1/message" vBody "application/json" vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "The send outcome is unknown. Check the message report before resending."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoReply
If (Not(IsComObjectCreated(hoReply))) Begin
Send CreateComObject of hoReply
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iParsed
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iSuccess <> True) Or ((iTemp1 <> 202) And (iTemp2 <> 207))) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Clickatell returned HTTP " iTemp1
If (iParsed = True) Begin
Get ComHasMember Of hoReply "error.description" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoReply "error.description" To sTemp1
Showln sTemp1
End
End
Procedure_Return
End
If (iParsed <> True) Begin
Showln "Expected a JSON response."
Procedure_Return
End
// A request-level error applies to the submission as a whole.
Get ComHasMember Of hoReply "error.code" To bTemp1
If (bTemp1 = True) Begin
Get ComIsNullOf Of hoReply "error.code" To bTemp1
If (bTemp1 <> True) Begin
Get ComStringOf Of hoReply "error.code" To sTemp1
Get ComStringOf Of hoReply "error.description" To sTemp2
Showln "Submission error " sTemp1 " " sTemp2
Procedure_Return
End
End
Get ComSizeOfArray Of hoReply "messages" To iCount
If (iCount <= 0) Begin
Showln "No recipient results returned. Check the message report before resending."
Procedure_Return
End
Move 0 To i
While (i < iCount)
Set ComI Of hoReply To i
Move (i + 1) To iResultNumber
// JsonTypeOf returns 5 for a JSON boolean.
Get ComJsonTypeOf Of hoReply "messages[i].accepted" To iTemp1
If (iTemp1 <> 5) Begin
Showln "Result " iResultNumber " has no acceptance flag."
End
Else Begin
Get ComBoolOf Of hoReply "messages[i].accepted" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoReply "messages[i].apiMessageId" To sMessageId
Get ComJsonTypeOf Of hoReply "messages[i].apiMessageId" To iTemp1
If ((iTemp1 <> 1) Or (sMessageId = "")) Begin
Showln "Result " iResultNumber " was accepted without a tracking ID."
End
Else Begin
Showln "Result " iResultNumber " accepted. Message ID: " sMessageId
End
End
Else Begin
Get ComStringOf Of hoReply "messages[i].error.code" To sTemp1
Get ComStringOf Of hoReply "messages[i].error.description" To sTemp2
Showln "Result " iResultNumber " rejected: " sTemp1 " " sTemp2
End
End
Move (i + 1) To i
Loop
End_Procedure