How to Send SMS in 30 Seconds with F#
Time to read: 3 minutes
Twilio empowers developers like you to add communication technology to your applications. You can use Twilio's APIs and SDKs to send SMS and MMS, WhatsApp messages, and emails, or make phone calls and video calls.
In this tutorial, you’ll learn how to send an SMS from an F# console application.
Prerequisites
Here’s what you will need to follow along:
- The .NET 7 SDK (earlier and newer versions may work too)
- A code editor or IDE (I recommend JetBrains Rider, Visual Studio, or VS Code with Ionide for F# plugin)
- A free Twilio account (sign up with Twilio for free)
You can find the source code for this tutorial on GitHub. Use it if you run into any issues, or submit an issue if you run into problems.
Buy a Twilio Phone Number
If you haven't done so already, you'll need to buy a Twilio phone number. To do that, log in to the Twilio Console, select Phone Numbers, and then click on the “Buy a number” button. Note that if you have a free account, you will be using your trial credit for this purchase.
On the “Buy a Number” page, select your country and check SMS in the “Capabilities” field. If you’d like to request a number that is local to your region, you can enter your area code in the “Number” field.
Click the “Search” button to see what numbers are available, and then click “Buy” for the number you like from the results. After you confirm your purchase, click the “Close” button.
Take note of your Twilio Phone Number, as you'll need it later.
Create and set up your F# console project
Open your preferred shell and run the following commands to create a new F# console project:
You'll need to store sensitive secrets to authenticate with the Twilio API. You'll use the .NET Secret Manager, aka user secrets, to store your secrets.
Initialize user secrets for your project using this command:
Then, go back to the Twilio Console and find your Account SID and Auth Token in the Account Info section. Store the Account SID and Auth Token as user secrets by running the following commands:
To load these secrets into your .NET application, you'll need to add the user secrets configuration package. Add it by running the following command:
Now, open the project in your preferred IDE and update the Program.fs file with the following code:
The program loads the user secrets for the project and puts them into the configuration
variable, retrieves the two secrets from configuration, and finally prints the Account SID to the console and whether the Auth Token has been configured or not.
Try it out by running dotnet run
.
Send an SMS message
Twilio has a helper library for .NET to make sending SMS more convenient. Add the helper library using the .NET CLI, with the command below:
Update the Program.fs file to import the following Twilio namespaces:
Then add the following F# at the end of the file:
Replace [YOUR_TWILIO_PHONE_NUMBER]
with your Twilio Phone Number and [YOUR_PERSONAL_PHONE_NUMBER]
with your personal phone number. Both phone numbers need to be E.164 formatted. For example, a US phone number, E.164 formatting would look like +14155552671.
Run your project again using dotnet run
. You should now receive an SMS saying "Ahoy!" and see the following console output:
Next steps
Great job. You now know how to send SMS from F#, but you can do a lot more! You can send MMS, WhatsApp messages, or respond to incoming SMS using the SMS webhook.
Here's a tutorial for receiving SMS and phone calls using F# and Minimal APIs.
We can't wait to see what you build. Let us know!
Niels Swimberghe is a Belgian American software engineer and technical content creator at Twilio. Get in touch with Niels on Twitter @RealSwimburger and follow Niels’ personal blog on .NET, Azure, and web development at swimburger.net.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.