How to Send SMS in 30 Seconds with F#

January 19, 2023
Written by
Reviewed by

How to Send SMS  in 30 Seconds with F#

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.

Want to receive SMS and phone calls? Check out this tutorial for receiving SMS and phone calls using F# and Minimal APIs.


Here’s what you will need to follow along:

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.

Buy a Twilio phone number

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:

dotnet new console -lang F# -o SendSms
cd SendSms

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:

dotnet user-secrets init

Twilio Account Info showing the Account SID and Auth Token.

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:

dotnet user-secrets set "Twilio:AccountSid" "[YOUR_ACCOUNT_SID]"
dotnet user-secrets set "Twilio:AuthToken" "[YOUR_AUTH_TOKEN]"

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:

dotnet add package Microsoft.Extensions.Configuration.UserSecrets

Now, open the project in your preferred IDE and update the Program.fs file with the following code:

open System
open System.Reflection
open Microsoft.Extensions.Configuration

let configuration = ConfigurationBuilder()

let twilioAccountSid = configuration["Twilio:AccountSid"]
let twilioAuthToken = configuration["Twilio:AuthToken"]

printfn "Twilio Account SID: %s"
    (if String.IsNullOrEmpty(twilioAccountSid) then "[NOT CONFIGURED]" else twilioAccountSid)
printfn "Twilio Auth Token: %s"
    (if String.IsNullOrEmpty(twilioAuthToken) then "[NOT CONFIGURED]" else "[CONFIGURED]")

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:

dotnet add package Twilio

Update the Program.fs file to import the following Twilio namespaces:

open System
open System.Reflection
open Microsoft.Extensions.Configuration
open Twilio
open Twilio.Rest.Api.V2010.Account
open Twilio.Types

Then add the following F# at the end of the file:

TwilioClient.Init(twilioAccountSid, twilioAuthToken)

let message = MessageResource.Create(
    from = PhoneNumber("[YOUR_TWILIO_PHONE_NUMBER]"),
    ``to`` = PhoneNumber("[YOUR_PERSONAL_PHONE_NUMBER]"),
    body = "Ahoy!"

printfn "Message sent"
printfn $"Message ID: {message.Sid}"

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.

Note how the to parameter is surrounded by two backticks ``. This is because to is a reserved keyword in F# and it needs to be escaped.

Run your project again using dotnet run. You should now receive an SMS saying "Ahoy!" and see the following console output:

Twilio Account SID: AC********************************
Twilio Auth Token: [CONFIGURED]
Message sent
Message ID: SM********************************

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