Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Integrating Twilio SMS in ASP.NET Core: SMS Sending, OTP Verification, and Voice Calls

Integrating Twilio SMS in ASP.NET Core: SMS Sending, OTP Verification, and Voice Calls

Date- Apr 27,2026 75
twilio asp.net core

Overview

Twilio is a cloud communications platform that offers a suite of APIs for building voice, video, and messaging applications. With Twilio, developers can easily integrate SMS and voice capabilities into their applications, enabling them to send notifications, alerts, and conduct two-factor authentication (2FA) through OTP verification. This integration addresses a fundamental need in software development: the ability to communicate with users effectively and securely, regardless of their location.

The significance of SMS integration lies in its widespread usage. SMS messages have a high open rate compared to emails, making them an effective channel for time-sensitive information. Real-world use cases include sending order confirmations, appointment reminders, promotional offers, and OTPs for login verification. In sectors like finance, healthcare, and e-commerce, SMS communication is crucial for user engagement and security, ensuring that users receive timely information and can verify their identities during critical transactions.

Prerequisites

  • ASP.NET Core: Familiarity with creating ASP.NET Core applications and understanding of middleware, dependency injection, and controllers.
  • Twilio Account: A registered account on Twilio to obtain API keys and phone numbers for sending SMS.
  • NuGet Package Manager: Ability to install and manage NuGet packages in ASP.NET Core projects.
  • Basic C# Knowledge: Understanding of C# syntax, asynchronous programming, and RESTful web services.

Setting Up Twilio in ASP.NET Core

To get started with Twilio, you first need to create an account on the Twilio website. Once registered, you will be provided with an Account SID and an Auth Token, which are required for authentication when making API requests. Additionally, you will need to purchase a Twilio phone number that will be used to send SMS messages.

The next step is to install the Twilio SDK for .NET. This SDK simplifies the process of interacting with Twilio's APIs. You can install the SDK using the NuGet Package Manager Console with the following command:

Install-Package Twilio

After installing the package, you can configure your application to use Twilio's services. It's a good practice to store your sensitive credentials in the appsettings.json file or use environment variables for enhanced security.

// appsettings.json
{
  "Twilio": {
    "AccountSid": "your_account_sid",
    "AuthToken": "your_auth_token",
    "FromNumber": "+1234567890"
  }
}

This configuration allows you to access your Twilio credentials securely from your application. Next, create a service class to encapsulate the SMS functionality.

using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

public class SmsService
{
    private readonly string _accountSid;
    private readonly string _authToken;
    private readonly string _fromNumber;

    public SmsService(IConfiguration configuration)
    {
        _accountSid = configuration["Twilio:AccountSid"];
        _authToken = configuration["Twilio:AuthToken"];
        _fromNumber = configuration["Twilio:FromNumber"];
    }

    public void SendSms(string to, string message)
    {
        TwilioClient.Init(_accountSid, _authToken);
        var messageOptions = new CreateMessageOptions(new PhoneNumber(to))
        {
            From = new PhoneNumber(_fromNumber),
            Body = message,
        };
        MessageResource.Create(messageOptions);
    }
}

This SmsService class initializes the Twilio client using the provided credentials and includes a method SendSms that takes in the recipient's phone number and the message body. The method constructs a CreateMessageOptions object and sends the SMS using the MessageResource.Create method. The expected output is that the recipient receives the SMS message on their phone.

Sending an SMS

To send an SMS, you can now create a controller that utilizes the SmsService.

using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class SmsController : ControllerBase
{
    private readonly SmsService _smsService;

    public SmsController(SmsService smsService)
    {
        _smsService = smsService;
    }

    [HttpPost]
    [Route("send")]
    public IActionResult SendSms([FromBody] SmsRequest request)
    {
        _smsService.SendSms(request.To, request.Message);
        return Ok(new { Status = "Message sent successfully" });
    }
}

public class SmsRequest
{
    public string To { get; set; }
    public string Message { get; set; }
}

This SmsController class defines an API endpoint to send SMS messages. The SendSms method accepts a JSON payload containing the recipient's phone number and the message. After calling the SendSms method from the SmsService, it returns a success message.

Implementing OTP Verification

OTP (One-Time Password) verification is a security mechanism that adds an additional layer of protection during user authentication. By sending an OTP via SMS, you can ensure that only the user with access to their registered phone number can complete the login process. This is essential for preventing unauthorized access to user accounts.

To implement OTP verification, you can modify the SmsService to include a method for generating and sending OTPs. OTPs are typically six-digit numeric codes that expire after a short period. For this implementation, we will generate a random OTP and send it to the user.

public string GenerateOtp()
{
    Random random = new Random();
    return random.Next(100000, 999999).ToString();
}

public string SendOtp(string to)
{
    string otp = GenerateOtp();
    SendSms(to, "Your OTP is: " + otp);
    return otp;
}

The GenerateOtp method creates a random six-digit number, while the SendOtp method sends the OTP to the specified phone number. You would typically store this OTP in a temporary data store (like a database or an in-memory cache) along with an expiration time for verification purposes.

Verifying the OTP

After sending the OTP, you will need to verify it when the user submits the code. Here is how you can implement the verification method:

private Dictionary _otpStorage = new Dictionary();

public void StoreOtp(string phoneNumber, string otp)
{
    _otpStorage[phoneNumber] = otp;
}

public bool VerifyOtp(string phoneNumber, string otp)
{
    if (_otpStorage.TryGetValue(phoneNumber, out var storedOtp))
    {
        return storedOtp == otp;
    }
    return false;
}

The StoreOtp method temporarily stores the OTP associated with the user's phone number, while the VerifyOtp method checks if the submitted OTP matches the stored one. In a production environment, consider using a more robust storage solution with expiration logic.

Making Voice Calls with Twilio

Twilio also allows you to make voice calls, which can be useful for applications that require immediate communication, such as customer support or verification calls. The process of making a call is similar to sending an SMS, but it requires additional parameters for call configuration.

To make a voice call, you can extend the SmsService with a new method. The method will require a TwiML URL, which defines how Twilio should handle the call.

public void MakeCall(string to, string twimlUrl)
{
    TwilioClient.Init(_accountSid, _authToken);
    var callOptions = new CreateCallOptions(new PhoneNumber(to))
    {
        From = new PhoneNumber(_fromNumber),
        Url = new Uri(twimlUrl),
    };
    CallResource.Create(callOptions);
}

The MakeCall method initializes the Twilio client and creates a call using the CallResource.Create method. The twimlUrl parameter points to a TwiML document that defines the call behavior, such as playing a message or gathering user input.

Creating a TwiML Document

A TwiML document can be hosted on your server or a cloud service. Here is an example of a simple TwiML document that plays a message:

// TwiML document

    Your verification code is: 123456

When the call is made, Twilio will fetch this TwiML document and execute the instructions, playing the specified message to the recipient.

Edge Cases & Gotchas

While integrating Twilio, developers may encounter several edge cases and pitfalls. Understanding these can save time and prevent errors in production.

Common Pitfalls

  • Incorrect Phone Number Format: Twilio requires phone numbers to be in E.164 format. Always validate and format phone numbers before sending requests.
  • Rate Limiting: Twilio enforces limits on the number of messages you can send in a given period. Ensure your application handles HTTP 429 responses gracefully.
  • Handling Delivery Failures: SMS delivery is not guaranteed. Implement callback URLs to receive status updates for sent messages and handle failures accordingly.

Example of Handling Incorrect Phone Numbers

public IActionResult SendSms([FromBody] SmsRequest request)
{
    if (!IsValidPhoneNumber(request.To))
    {
        return BadRequest(new { Error = "Invalid phone number format" });
    }
    _smsService.SendSms(request.To, request.Message);
    return Ok(new { Status = "Message sent successfully" });
}

private bool IsValidPhoneNumber(string number)
{
    // Validate phone number format (E.164)
    return number.StartsWith("+") && number.Length >= 10;
}

The IsValidPhoneNumber method checks if the phone number adheres to the expected format before sending the SMS, preventing unnecessary API calls.

Performance & Best Practices

To ensure optimal performance while using Twilio APIs, consider the following best practices:

Batch Sending

Instead of sending individual SMS requests, batch your messages when possible. This can reduce the number of HTTP requests and lower costs.

Asynchronous Processing

Use asynchronous programming to avoid blocking the main thread during SMS sending or call processing. This improves application responsiveness.

public async Task SendSmsAsync(string to, string message)
{
    await Task.Run(() => SendSms(to, message));
}

The SendSmsAsync method wraps the SMS sending logic in a Task, allowing for non-blocking execution.

Monitoring and Logging

Implement logging for all API interactions to track successes and failures. Use tools like Application Insights to gain insights into your application's performance and user interactions.

Real-World Scenario: User Registration with OTP

As a practical example, let’s build a user registration scenario that incorporates SMS sending and OTP verification. This will simulate a common use case in modern applications.

public class UserRegistrationService
{
    private readonly SmsService _smsService;

    public UserRegistrationService(SmsService smsService)
    {
        _smsService = smsService;
    }

    public async Task RegisterUserAsync(string phoneNumber)
    {
        string otp = _smsService.SendOtp(phoneNumber);
        // Store the OTP and return a success message
        return otp;
    }

    public bool ConfirmOtp(string phoneNumber, string otp)
    {
        return _smsService.VerifyOtp(phoneNumber, otp);
    }
}

The UserRegistrationService class handles user registration by sending an OTP to the provided phone number. After the user receives the OTP, they can confirm it to complete the registration process.

Conclusion

  • Integrating Twilio SMS and voice services in ASP.NET Core enhances user communication and security.
  • Understand the importance of validating phone numbers and handling edge cases to ensure a smooth user experience.
  • Implement best practices such as asynchronous processing and logging for optimal performance.
  • Real-world scenarios, like OTP verification, exemplify the practical application of these integrations.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Integrating Twilio SMS and Voice Calls in ASP.NET Core: A Comprehensive Guide
Apr 27, 2026
Integrating Plivo SMS API with ASP.NET Core: A Comprehensive Guide
Apr 29, 2026
Integrating Fast2SMS with ASP.NET Core for Reliable SMS Delivery in India
Apr 28, 2026
Integrating Vonage Nexmo SMS API in ASP.NET Core Applications
Apr 28, 2026
Previous in ASP.NET Core
Integrating Elastic Email with ASP.NET Core: A Comprehensive Guid…
Next in ASP.NET Core
Integrating Twilio SMS and Voice Calls in ASP.NET Core: A Compreh…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    Complete Guide to C++ Classes: Explained with Examples 4,212 views
  • 2
    Implementing an End-to-End CI/CD Pipeline for ASP.NET Core… 366 views
  • 3
    Create Database and CRUD operation 3,388 views
  • 4
    Mastering TypeScript Utility Types: Partial, Required, Rea… 675 views
  • 5
    Responsive Slick Slider 23,373 views
  • 6
    Integrating Azure Cognitive Search into ASP.NET Core Appli… 156 views
  • 7
    Integrating Anthropic Claude API in ASP.NET Core for AI Ch… 141 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26191 views
  • Exception Handling Asp.Net Core 20938 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 20391 views
  • How to implement Paypal in Asp.Net Core 19753 views
  • Task Scheduler in Asp.Net core 17705 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor