Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.0.0+

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

Clickatell API reference

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loBody
LOCAL lcSender
LOCAL loResp
LOCAL loReply
LOCAL lnParsed
LOCAL lnCount
LOCAL i
LOCAL lnResultNumber
LOCAL lcMessageId

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
*  Supply the key for this One API integration from application configuration.
loHttp.SetRequestHeader("Authorization","ONE_API_KEY")
loHttp.SetRequestHeader("Accept","application/json")

loBody = CreateObject('Chilkat.JsonObject')
loBody.UpdateString("messages[0].channel","sms")
loBody.UpdateString("messages[0].to","PHONE_NUMBER")
loBody.UpdateString("messages[0].content","Test message: your order is ready.")
*  For a two-way sender, supply the number provisioned on the integration.
lcSender = ""
IF (lcSender <> "") THEN
    loBody.UpdateString("messages[0].from",lcSender)
ENDIF

loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpJson("POST","https://platform.clickatell.com/v1/message",loBody,"application/json",loResp)
IF (loResp.StatusCode = 0) THEN
    ? "The send outcome is unknown. Check the message report before resending."
    RELEASE loHttp
    RELEASE loBody
    RELEASE loResp
    CANCEL
ENDIF

loReply = CreateObject('Chilkat.JsonObject')
lnParsed = loReply.Load(loResp.BodyStr)
IF ((lnSuccess <> 1) OR ((loResp.StatusCode <> 202) AND (loResp.StatusCode <> 207))) THEN
    ? "Clickatell returned HTTP " + STR(loResp.StatusCode)
    IF (lnParsed = 1) THEN
        IF (loReply.HasMember("error.description") = 1) THEN
            ? loReply.StringOf("error.description")
        ENDIF

    ENDIF

    RELEASE loHttp
    RELEASE loBody
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF (lnParsed <> 1) THEN
    ? "Expected a JSON response."
    RELEASE loHttp
    RELEASE loBody
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

*  A request-level error applies to the submission as a whole.
IF (loReply.HasMember("error.code") = 1) THEN
    IF (loReply.IsNullOf("error.code") <> 1) THEN
        ? "Submission error " + loReply.StringOf("error.code") + "  " + loReply.StringOf("error.description")
        RELEASE loHttp
        RELEASE loBody
        RELEASE loResp
        RELEASE loReply
        CANCEL
    ENDIF

ENDIF

lnCount = loReply.SizeOfArray("messages")
IF (lnCount <= 0) THEN
    ? "No recipient results returned. Check the message report before resending."
    RELEASE loHttp
    RELEASE loBody
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

i = 0
DO WHILE (i < lnCount)
    loReply.I = i
    lnResultNumber = i + 1
    *  JsonTypeOf returns 5 for a JSON boolean.
    IF (loReply.JsonTypeOf("messages[i].accepted") <> 5) THEN
        ? "Result " + STR(lnResultNumber) + " has no acceptance flag."
    ELSE
        IF (loReply.BoolOf("messages[i].accepted") = 1) THEN
            lcMessageId = loReply.StringOf("messages[i].apiMessageId")
            IF ((loReply.JsonTypeOf("messages[i].apiMessageId") <> 1) OR (lcMessageId = "")) THEN
                ? "Result " + STR(lnResultNumber) + " was accepted without a tracking ID."
            ELSE
                ? "Result " + STR(lnResultNumber) + " accepted. Message ID: " + lcMessageId
            ENDIF

        ELSE
            ? "Result " + STR(lnResultNumber) + " rejected: " + loReply.StringOf("messages[i].error.code") + "  " + loReply.StringOf("messages[i].error.description")
        ENDIF

    ENDIF

    i = i + 1
ENDDO

RELEASE loHttp
RELEASE loBody
RELEASE loResp
RELEASE loReply