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 Fast2SMS with ASP.NET Core for Reliable SMS Delivery in India

Integrating Fast2SMS with ASP.NET Core for Reliable SMS Delivery in India

Date- Apr 28,2026 87
fast2sms aspnetcore

Overview

Fast2SMS is a popular SMS gateway service in India that enables developers to send SMS messages programmatically. It addresses the need for reliable and fast SMS delivery, which is crucial in various applications such as alerts, notifications, and marketing campaigns. With the rise of mobile communication, businesses and developers seek efficient ways to reach their audience through SMS, making services like Fast2SMS invaluable.

This integration allows developers to leverage the robust API provided by Fast2SMS, enabling seamless SMS delivery from their ASP.NET Core applications. The service supports both promotional and transactional SMS, catering to different use cases ranging from sending OTPs to promotional offers.

Prerequisites

  • ASP.NET Core SDK: Ensure you have the latest version of the ASP.NET Core SDK installed on your machine.
  • Fast2SMS Account: Create an account on Fast2SMS to obtain your API key and other credentials required for integration.
  • Development Environment: A suitable IDE such as Visual Studio or Visual Studio Code for coding.
  • Basic Knowledge of REST APIs: Understanding how to make HTTP requests and handle responses.

Setting Up the ASP.NET Core Project

To integrate Fast2SMS, we first need to set up a new ASP.NET Core project. This will serve as the foundation for our SMS sending functionality.

dotnet new webapi -n Fast2SMSIntegration

This command creates a new ASP.NET Core Web API project named Fast2SMSIntegration. Navigate into the project directory:

cd Fast2SMSIntegration

Next, we will install the required NuGet package to make HTTP requests easily:

dotnet add package Microsoft.Extensions.Http

We are using the Microsoft.Extensions.Http package to simplify working with HTTP clients. This package provides a way to configure and use HttpClient instances in a dependency injection setup.

Configuring HttpClient

In the Startup.cs file, we need to configure the HttpClient for dependency injection:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddHttpClient();
}

This code registers the HttpClient service with the dependency injection container, allowing us to inject it into our services or controllers later.

Creating the SMS Service

Next, we will create a service that encapsulates the logic for sending SMS via Fast2SMS. This service will handle the HTTP requests to the Fast2SMS API.

public class SmsService
{
    private readonly HttpClient _httpClient;

    public SmsService(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    public async Task SendSmsAsync(string mobileNumber, string message)
    {
        var apiKey = "YOUR_FAST2SMS_API_KEY";
        var requestUri = $"https://www.fast2sms.com/dev/bulkV2?authorization={apiKey}&message={message}&sender_id=FSTSMS&language=english&route=p&numbers={mobileNumber}";

        var response = await _httpClient.GetAsync(requestUri);
        return await response.Content.ReadAsStringAsync();
    }
}

The SmsService class has a constructor that accepts an HttpClient instance, which is injected using dependency injection. The SendSmsAsync method constructs the request URL using the provided mobile number and message, along with your Fast2SMS API key.

The method sends a GET request to the Fast2SMS API and returns the response content as a string. Ensure to replace YOUR_FAST2SMS_API_KEY with your actual API key.

Integrating the SMS Service with a Controller

Now that we have the SMS service set up, we need to create a controller to expose an endpoint for sending SMS messages. This allows clients to make requests to our API.

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

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

    [HttpPost]
    public async Task SendSms([FromBody] SmsRequest request)
    {
        if (string.IsNullOrEmpty(request.MobileNumber) || string.IsNullOrEmpty(request.Message))
        {
            return BadRequest("Mobile number and message are required.");
        }

        var response = await _smsService.SendSmsAsync(request.MobileNumber, request.Message);
        return Ok(response);
    }
}

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

The SmsController class is decorated with ApiController and Route attributes, indicating that it is an API controller and specifying the route for the SMS API. The constructor injects the SmsService.

The SendSms action method receives a SmsRequest object from the request body, validating that both mobile number and message are provided. It then calls the SendSmsAsync method of the SmsService to send the SMS and returns the response.

Testing the SMS Integration

To test the SMS integration, we can use tools like Postman or cURL to send a POST request to our API endpoint. Here’s a sample request using cURL:

curl -X POST https://localhost:5001/api/sms -H "Content-Type: application/json" -d "{\"MobileNumber\": \"1234567890\", \"Message\": \"Hello from Fast2SMS!\"}"

Replace 1234567890 with a valid mobile number. If successful, the API will return a response from the Fast2SMS service.

Edge Cases & Gotchas

Handling edge cases is crucial in any integration. Here are some common pitfalls:

Invalid Mobile Number Format

Sending an SMS to an invalid mobile number format can lead to errors. Always validate the mobile number before making the API call.

if (!Regex.IsMatch(request.MobileNumber, "^[0-9]{10}$"))
{
    return BadRequest("Invalid mobile number format.");
}

This validation checks that the mobile number consists of exactly 10 digits.

API Rate Limiting

Fast2SMS may impose rate limits on the number of SMS messages that can be sent within a certain time frame. Ensure you handle such errors gracefully and implement retry logic if needed.

Performance & Best Practices

When integrating with external APIs like Fast2SMS, performance considerations are essential. Here are some best practices:

  • Asynchronous Programming: Use asynchronous methods to avoid blocking threads, especially when making network calls.
  • HttpClient Singleton: Use a singleton instance of HttpClient to reduce overhead and improve performance. This can be achieved by configuring it in the Startup.cs as shown earlier.
  • Logging: Implement logging to trace API calls and responses, which aids in debugging and monitoring.

Real-World Scenario: Sending OTP via SMS

In this section, we’ll implement a simple application that sends an OTP (One-Time Password) to users for verification purposes. This is a common use case for SMS services.

public class OtpService
{
    private readonly SmsService _smsService;

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

    public async Task GenerateAndSendOtp(string mobileNumber)
    {
        var otp = new Random().Next(100000, 999999).ToString();
        var message = $"Your OTP is: {otp}";
        await _smsService.SendSmsAsync(mobileNumber, message);
        return otp;
    }
}

The OtpService generates a random 6-digit OTP and sends it to the specified mobile number. This can be integrated into your existing application where user verification is required.

Conclusion

  • Fast2SMS provides a reliable solution for SMS delivery in India.
  • Integrating Fast2SMS into an ASP.NET Core application is straightforward with proper setup and error handling.
  • Always validate input data and handle edge cases to avoid common pitfalls.
  • Implementing logging and monitoring can significantly improve the maintainability of your application.

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

Related Articles

Integrating Plivo SMS API with ASP.NET Core: A Comprehensive Guide
Apr 29, 2026
Integrating Vonage Nexmo SMS API in ASP.NET Core Applications
Apr 28, 2026
Integrating SparkPost Email API with ASP.NET Core: A Comprehensive Guide
Apr 25, 2026
Integrating Azure OpenAI Service with ASP.NET Core: A Comprehensive Guide
May 04, 2026
Previous in ASP.NET Core
Integrating MSG91 for OTP and SMS in ASP.NET Core Applications
Next in ASP.NET Core
CWE-384: Preventing Session Fixation in ASP.NET Core with Secure …
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… 367 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