Sample code for 30+ languages & platforms
C# 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 C# Downloads

C#
bool success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Obtain these credentials from the application's private configuration.
Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.BasicAuth = true;
http.Login = "YOUR_API_KEY_SID";
http.Password = "YOUR_API_KEY_SECRET";

//  Both requests use the same Verify Service SID (VA...).
Chilkat.StringBuilder url = new Chilkat.StringBuilder();
url.Append("https://verify.twilio.com/v2/Services/");
url.Append("YOUR_VERIFY_SERVICE_SID");
url.Append("/Verifications");

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

Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
success = http.HttpReq(url.GetAsString(),req,resp);
if (resp.StatusCode == 0) {
    Debug.WriteLine("No response from Twilio. Check connectivity and TLS settings.");
    return;
}

Chilkat.JsonObject result = new Chilkat.JsonObject();
bool parsed = result.Load(resp.BodyStr);
if ((success != true) || (resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
    Debug.WriteLine("Twilio returned HTTP " + Convert.ToString(resp.StatusCode));
    if ((parsed == true) && (result.JsonTypeOf("code") == 2)) {
        Debug.WriteLine("Twilio error code: " + Convert.ToString(result.IntOf("code")));
    }

    return;
}

if ((parsed != true) || (result.JsonTypeOf("status") != 1)) {
    Debug.WriteLine("Verification status missing from the response.");
    return;
}

string status = result.StringOf("status");
if (status != "pending") {
    Debug.WriteLine("Verification status: " + status);
    return;
}

if ((result.JsonTypeOf("sid") != 1) || (result.StringOf("sid") == "")) {
    Debug.WriteLine("The pending response did not include a verification SID.");
    return;
}

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