How to Send an SMS with .Net 6 Minimal API
I’ve been looking at the preview releases of .Net 6 for some time now, and one of the exciting features that a lot of people have been talking about is Minimal APIs. While it seems that opinion is very divided, I think they are a welcome addition. It removes a lot of the weight of using ASP.NET MVC and certainly lowers the bar to entry with a similar feel to ExpressJS in NodeJS.
A little over a year ago we released a blog post entitled “ How to Send an SMS With ASP.NET Core MVC “. So with the Release of .Net 6 upon us, I thought it would be a good idea to take inspiration and see what it would be like to use the new Minimal API syntax to emulate the same functionality.
Just Give Me Code
You can skip straight to the code on GitHub.
- .Net 6 RC 2 SDK or greater
- Visual Studio 2022 Preview or Visual Studio Code
Vonage API Account
To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.
Creating the Project
The easiest way I’ve found to create a new Minimal API project is using the command below
Microsoft also has a great tutorial on creating a new Minimal API project using Visual Studio.
We should now have an API with one “Hello World” endpoint. To this we will add two NuGet packages, the first is Vonage’s .Net SDK version 5.9.2 at the time of writing. As this will be an API we won’t have a UI so the second is Swashbuckle / Swagger that will enable us to try out any endpoints we create easily.
Small Is Beautiful
Along with the usual
appsettings.json, your newly created project will be just one file,
Program.cs. This is truly minimal, for an ASP.Net project at least.
Program.cs, it should look like this.
This is all you need to have a fully-fledged .NET API all in a single file. This will provide a much lighter starting point to build a small API or microservice. And to be honest, still blows my mind when I think back to how much code WebAPI would need to produce the same outcome.
Let’s make a start by adding some settings. Inside
appsettings.json we need to add our Vonage key and secret, these are used to authenticate your application with Vonage's services and can be found at the top of the Dashboard.
Currently, we do not have Dependency Injection configured so let’s add the VonageClient class to the services collection. This will allow it to be injected into any class or method that we will use further down the line.
Import the required namespaces at the top of the file.
Register the VonageClient with the services collection.
Going line at a time we can see that we get an instance of IConfiguration, this enables us to access the app settings we need. The key and secret are then retrieved from the configuration so that we can create credentials that are required by the VonageClient constructor.
Sending Out an SMS
Now that we have our VonageClient class configured and ready to be injected let’s create a new endpoint that we can send requests to. We will need to pass in a data model to the endpoint so create a class in a new file called `SmsModel`. Then inside the class, we want to add the following properties.
With our model created we can go ahead and add a new POST method with the VonageClient and our SmsModel class as parameters.
There are a couple of things going on in the block of code. First, we are using the
MapPost extension method to create an endpoint at http://localhost:5000/sms. Secondly, we are declaring the parameters for the method;
VonageClient will get injected using the dependency injection we set up previously,
SmsModel will be created using the body of the request using model binding.
The main body of the method does the actual work of sending the SMS. We create an instance of the SendSmsRequest using the data from the model we passed in, then it’s just a case of passing the request class to the SmsClient’s
SendAnSmsAsync method. In the spirit of "minimal", this is only one line!
Try It Out
The project should now build, run and receive requests. As mentioned previously though we have no UI to test this out easily so we will add a few more lines of code to implement the Swagger UI into our project.
Directly after var builder = WebApplication.CreateBuilder(args) we need to add two lines of code. These will add the necessary services to dependency inject.
With these added, we then register the Swagger middleware before the
With all our code now written, we can run the project, hit F5 if you’re using Visual Studio or run the command below inside the project folder
Now browse to https://localhost:5001/swagger and you should be able to use the “Try it out” button on the SMS endpoint. From there you will see a 200 response code and receive a text message.
Input validation is a vital part of any API, as it stands there is no validation built into Minimal APIs as you would find with ASP.NET MVC. Damian Edwards has created a small library called MinimalValidation using validation attributes similar to the MVC validation.
Service registration and endpoint changes
While MVC is a fully-featured framework incorporating built-in model binding and validation, extensible pipelines via filters, convention and declarative based behaviours and more. Some may not need specific features or have performance constraints that make using MVC undesirable. With more and more features surfacing as ASP.NET Core middleware (authorisation, authentication, routing etc), ASP.NET 6 and Minimal APIs bring these features into play with less pomp and is a perfect fit for creating lightweight microservices in a timely fashion in a maintainable way.
Originally published at https://learn.vonage.com/blog/2021/11/09/how-to-send-an-sms-with-net-6-minimal-api/