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

Send an SMS verification code with Twilio Verify

See more Twilio Examples

Live SMS. When a user requests a code, pass their phone number in E.164 format (for example, +12025550123) and select the SMS channel. Create a Verify Service in the Twilio Console first. Supply its VA service SID and an API key with access to that service. For a live test, use a phone you control. Messaging and verification charges may apply.

Twilio generates the code and sends it to the phone. A pending response means the application can prompt for the code. Save the returned VE verification SID with the user and phone number that initiated the request, then use it to check the code entered by the user.

Twilio Verify API reference

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Url
oleobject loo_Req
oleobject loo_Resp
oleobject loo_Result
long li_Parsed
string ls_Status

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Obtain these credentials from the application's private configuration.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.RequireSslCertVerify = 1
loo_Http.BasicAuth = 1
loo_Http.Login = "YOUR_API_KEY_SID"
loo_Http.Password = "YOUR_API_KEY_SECRET"

//  Both requests use the same Verify Service SID (VA...).
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")

loo_Url.Append("https://verify.twilio.com/v2/Services/")
loo_Url.Append("YOUR_VERIFY_SERVICE_SID")
loo_Url.Append("/Verifications")

loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

loo_Req.HttpVerb = "POST"
loo_Req.ContentType = "application/x-www-form-urlencoded"
//  Supply the number the user asked to verify. AddParam encodes the leading plus sign.
loo_Req.AddParam("To","PHONE_NUMBER_IN_E164_FORMAT")
loo_Req.AddParam("Channel","sms")

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpReq(loo_Url.GetAsString(),loo_Req,loo_Resp)
if loo_Resp.StatusCode = 0 then
    Write-Debug "No response from Twilio. Check connectivity and TLS settings."
    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    return
end if

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

li_Parsed = loo_Result.Load(loo_Resp.BodyStr)
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
    Write-Debug "Twilio returned HTTP " + string(loo_Resp.StatusCode)
    if (li_Parsed = 1) AND (loo_Result.JsonTypeOf("code") = 2) then
        Write-Debug "Twilio error code: " + string(loo_Result.IntOf("code"))
    end if

    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Result
    return
end if

if (li_Parsed <> 1) OR (loo_Result.JsonTypeOf("status") <> 1) then
    Write-Debug "Verification status missing from the response."
    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Result
    return
end if

ls_Status = loo_Result.StringOf("status")
if ls_Status <> "pending" then
    Write-Debug "Verification status: " + ls_Status
    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Result
    return
end if

if (loo_Result.JsonTypeOf("sid") <> 1) OR (loo_Result.StringOf("sid") = "") then
    Write-Debug "The pending response did not include a verification SID."
    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Result
    return
end if

//  Save this SID with the pending attempt before prompting the user for the code.
Write-Debug "Pending verification SID: " + loo_Result.StringOf("sid")


destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_Resp
destroy loo_Result