Sending SMS messages to users is a critical feature for most platforms — from authenticating and validating mobile numbers to confirming payments, orders, or even shipment statuses. One of the most popular platforms for sending SMS via an API is Twilio.
In this post, I’ll show you a simple way to send SMS messages using D Language and Twilio’s API.
Send SMS
Here’s a basic function to send an SMS with Twilio:
import std.stdio;
import std.string;
import std.base64;
import std.net.curl;
import std.json;
import std.conv;
bool sendSMS( string smsTo, string smsBody, ref string twilioResponse )
{
try
{
string twilioAccountSID = "[Twilio Account SID]";
string twilioAuthToken = "[Twilio Auth Token]";
string twilioFromNumber = "[Twilio From Number]";
auto url = "https://api.twilio.com/2010-04-01/Accounts/"~ twilioAccountSID ~"/Messages.json";
auto token = twilioAccountSID ~ ":" ~ twilioAuthToken;
auto tokenBytes = representation(token);
auto authorization = "Basic " ~ Base64.encode(tokenBytes);
auto http = HTTP();
http.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.addRequestHeader("Authorization", authorization);
auto payload = "From="~twilioFromNumber~"&To="~smsTo~"&Body="~smsBody;
auto result = post(url, payload, http);
bool responded = false;
JSONValue resultJSON = parseJSON(result.to!string);
if ("sid" in resultJSON)
{
twilioResponse = result.to!string;
responded = true;
}
return responded;
}
catch(Throwable)
{
// Handle the Error if needed
return false;
}
}
void main() {
string to = "+1234567890";
string body = "The content of the SMS";
string response = "";
auto result = sendSMS( to, body, response );
writeln("SMS Resut = ", result);
writeln("SMS twilioResponse = ", response);
}
Using the function above, you can easily send an SMS to any user. However, one of the most common use cases for sending SMS is mobile number verification, and implementing that yourself comes with several challenges.
Let’s walk through the typical mobile number verification flow:

As you can see in the chart, you would need to handle the code generation, sending, and verification logic on your own server. But the real challenge isn’t just the backend implementation — it’s ensuring that your SMS messages are actually delivered worldwide.
Twilio restricts sending certain types of SMS based on a few factors, including:
- Country-specific regulations (You must comply with local laws.)
- Spam and messaging laws (There are limits on unsolicited or bulk messages.)
- Sender ID policies (Some countries require local or pre-approved sender IDs.)
- Twilio’s own policies (Certain types of content are blocked.)
- Technical routing issues (Some routes may fail without notice.)
- Cost and registration requirements (Some regions require upfront registration and fees.)
Handling all these challenges manually is complicated and time-consuming.
The easier and more practical solution? Let Twilio manage the verification flow for you.
SMS Verification (Twilio API)
Here’s the new verification flow when you use Twilio’s built-in services:

Now, you don’t need to implement your own SMS verification logic or worry about regional regulations. Twilio takes care of everything, including sending the verification code and validating it.
Instead of just one function to send SMS, you’ll now need two functions:
- sendVerificationSMS — Request Twilio to send a verification code to the user.
- checkVerificationCode — Verify the code entered by the user with Twilio.
Here’s the first function to send a verification SMS:
import std.stdio;
import std.string;
import std.base64;
import std.net.curl;
import std.json;
import std.conv;
bool sendVerificationSMS( string smsTo, ref string twilioResponse )
{
try
{
string twilioAccountSID = "[Twilio Account SID]";
string twilioAuthToken = "[Twilio Auth Token]";
string twilioServiceSID = "[Twilio Service ID]";
auto url = "https://verify.twilio.com/v2/Services/"~ twilioServiceSID ~"/Verifications";
auto token = twilioAccountSID ~ ":" ~ twilioAuthToken;
auto tokenBytes = representation(token);
auto authorization = "Basic " ~ Base64.encode(tokenBytes);
auto http = HTTP();
http.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.addRequestHeader("Authorization", authorization);
auto payload = ("To="~ smsTo ~"&Channel=sms").replace("+", "%2b");
auto result = post(url, payload, http);
bool responded = false;
JSONValue resultJSON = parseJSON(result.to!string);
if ("sid" in resultJSON)
{
twilioResponse = result.to!string;
responded = true;
}
return responded;
}
catch(Throwable)
{
// Handle the Error if needed
return false;
}
}
void main() {
string to = "+1234567890";
string response = "";
auto result = sendVerificationSMS( to, response );
writeln("SMS Resut = ", result);
writeln("SMS twilioResponse = ", response);
}
When you call this function, Twilio will send a message like:
Your verification code is: 123456
Once the user enters this code in your app, you can verify it using the following function:
import std.stdio;
import std.string;
import std.base64;
import std.net.curl;
import std.json;
import std.conv;
bool checkVerificationCode( string phone, string code, ref string twilioResponse )
{
try
{
string twilioAccountSID = "[Twilio Account SID]";
string twilioAuthToken = "[Twilio Auth Token]";
string twilioServiceSID = "[Twilio Service ID]";
auto url = "https://verify.twilio.com/v2/Services/"~ twilioServiceSID ~"/VerificationCheck";
auto token = twilioAccountSID ~ ":" ~ twilioAuthToken;
auto tokenBytes = representation(token);
auto authorization = "Basic " ~ Base64.encode(tokenBytes);
auto http = HTTP();
http.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.addRequestHeader("Authorization", authorization);
auto payload = ("To="~phone~"&Code="~code).replace("+", "%2b");
auto result = post(url, payload, http);
bool responded = false;
JSONValue resultJSON = parseJSON(result.to!string);
if ("sid" in resultJSON && resultJSON["valid"].boolean == true)
{
twilioResponse = result.to!string;
responded = true;
}
return responded;
}
catch(Throwable)
{
// Handle the Error if needed
return false;
}
}
void main() {
string phone = "+1234567890";
string code = "[user entered this]";
string response = "";
auto result = checkVerificationCode( phone, code, response );
writeln("SMS Resut = ", result);
writeln("SMS twilioResponse = ", response);
}
And that’s it!
Quick Recap:
- To send general SMS messages, you can use the
sendSMS
function. - To verify mobile numbers, it’s better to rely on Twilio’s verification service using the
sendVerificationSMS
andcheckVerificationCode
functions.
If you found this post helpful, please share it!
Your support helps us continue creating more content like this for you and the developer community.