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

Clickatell One API: Read the account balance and currency

See more Clickatell Examples

Request the messaging balance associated with a One API integration key. The response supplies both the decimal amount and its currency. Read the amount as text for display to preserve its precision. A zero balance is a valid result. For example, a billing dashboard can display 1267.92865 ZAR.

Clickatell API reference

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Handle hoReply
    Boolean iParsed
    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(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoResp to vResp
    Get ComHttpNoBody Of hoHttp "GET" "https://platform.clickatell.com/v1/balance" vResp To iSuccess
    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 = 0) Begin
        Showln "No HTTP response. Check the connection and retry the lookup."
        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
    If ((iSuccess <> True) Or ((iTemp1 <> 200))) 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

    //  JsonTypeOf returns 2 for a number and 1 for a string. Zero is a valid amount.
    Get ComJsonTypeOf Of hoReply "balance" To iTemp1
    Get ComJsonTypeOf Of hoReply "currency" To iTemp2
    If ((iTemp1 <> 2) Or (iTemp2 <> 1)) Begin
        Showln "The balance response is missing the amount or currency."
        Procedure_Return
    End

    Get ComStringOf Of hoReply "balance" To sTemp1
    Get ComStringOf Of hoReply "currency" To sTemp2
    Showln "Account balance: " sTemp1 " " sTemp2


End_Procedure