In this quickstart, you'll build your first application to programmatically send and receive text messages with Twilio Programmable Messaging. This quickstart uses the Programmable Messaging REST API, the Twilio helper libraries, and the Twilio Virtual Phone.
Create and open a new file called send_sms.py anywhere on your machine and paste in the following code:
Send an SMS Using Twilio with Python
1
# Download the helper library from https://www.twilio.com/docs/python/install
2
importos
3
fromtwilio.restimportClient
4
5
# Find your Account SID and Auth Token at twilio.com/console
6
# and set the environment variables. See http://twil.io/secure
7
account_sid=os.environ["TWILIO_ACCOUNT_SID"]
8
auth_token=os.environ["TWILIO_AUTH_TOKEN"]
9
client=Client(account_sid, auth_token)
10
11
message=client.messages.create(
12
body="Join Earth's mightiest heroes. Like Kevin Bacon.",
13
from_="+15017122661",
14
to="+15558675310",
15
)
16
17
print(message.body)
In the send_sms.py file, replace the values for account_sid and auth_token with your Account SID and Auth Token surrounded by quotation marks.
(error)
Danger
This quickstart hardcodes your credentials to get you started quickly. Use environment variables and API keys to keep credentials secret and control access when you deploy to production.
Replace the value for from with the phone number that Twilio gave you in E.164 format.
Replace the value for to with the Twilio Virtual Phone number (+18777804236).
Save your changes and run this command from your terminal in the directory that contains send_sms.py:
pythonsend_sms.py
After a few moments, you receive an SMS from your Twilio number on the Twilio Virtual Phone.
Create and open a new file called send_sms.js anywhere on your machine and paste in the following code:
Send an SMS Using Twilio with Node.js
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
consttwilio=require("twilio");// Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
constaccountSid=process.env.TWILIO_ACCOUNT_SID;
7
constauthToken=process.env.TWILIO_AUTH_TOKEN;
8
constclient=twilio(accountSid, authToken);
9
10
async functioncreateMessage() {
11
constmessage= awaitclient.messages.create({
12
body:"This is the ship that made the Kessel Run in fourteen parsecs?",
13
from:"+15017122661",
14
to:"+15558675310",
15
});
16
17
console.log(message.body);
18
}
19
20
createMessage();
In the send_sms.js file, replace the values for accountSid and authToken with your Account SID and Auth Token surrounded by quotation marks.
(error)
Danger
This quickstart hardcodes your credentials to get you started quickly. Use environment variables and API keys to keep credentials secret and control access when you deploy to production.
Replace the value for from with the phone number that Twilio gave you in E.164 format.
Replace the value for to with the Twilio Virtual Phone number (+18777804236).
Save your changes and run this command from your terminal in the directory that contains send_sms.js:
nodesend_sms.js
After a few moments, you receive an SMS from your Twilio number on the Twilio Virtual Phone.
Create and open a new file called send_sms.php in the project directory and paste in the following code:
Send an SMS Using Twilio with PHP
1
<?php
2
3
// Update the path below to your autoload.php,
4
// see https://getcomposer.org/doc/01-basic-usage.md
5
require_once"/path/to/vendor/autoload.php";
6
7
useTwilio\Rest\Client;
8
9
// Find your Account SID and Auth Token at twilio.com/console
10
// and set the environment variables. See http://twil.io/secure
11
$sid=getenv("TWILIO_ACCOUNT_SID");
12
$token=getenv("TWILIO_AUTH_TOKEN");
13
$twilio= newClient($sid, $token);
14
15
$message=$twilio->messages->create(
16
"+15558675310",// To
17
[
18
"body"=>
19
"This is the ship that made the Kessel Run in fourteen parsecs?",
20
"from"=>"+15017122661",
21
]
22
);
23
24
print$message->body;
In the send_sms.php file, replace the values for $sid and $token with your Account SID and Auth Token surrounded by quotation marks.
(error)
Danger
This quickstart hardcodes your credentials to get you started quickly. Use environment variables and API keys to keep credentials secret and control access when you deploy to production.
Replace the value for from with the phone number that Twilio gave you in E.164 format.
Replace the value for To with the Twilio Virtual Phone number (+18777804236).
Update line 5 of send_sms.php to require __DIR__ . '/vendor/autoload.php';
Save your changes and run this command from your terminal in the directory that contains send_sms.php:
phpsend_sms.php
After a few moments, you receive an SMS from your Twilio number on the Twilio Virtual Phone.
Create and set up a new project in Visual Studio:
Open Visual Studio and click Create a new project.
body:"Join Earth's mightiest heroes. Like Kevin Bacon.",
19
from:newTwilio.Types.PhoneNumber("+15017122661"),
20
to:newTwilio.Types.PhoneNumber("+15558675310"));
21
22
Console.WriteLine(message.Body);
23
}
24
}
In the Program.cs file, replace the values for accountSid and authToken with your Account SID and Auth Token surrounded by quotation marks.
(error)
Danger
This quickstart hardcodes your credentials to get you started quickly. Use environment variables and API keys to keep credentials secret and control access when you deploy to production.
Replace the value for from: new Twilio.Types.PhoneNumber with the phone number that Twilio gave you in E.164 format.
Replace the value for to: new Twilio.Types.PhoneNumber with the Twilio Virtual Phone number (+18777804236).
Save your changes and run your project in Visual Studio.
After a few moments, you receive an SMS from your Twilio number on the Twilio Virtual Phone.
Run the following commands to create a new .NET project and install the Twilio NuGet package:
1
mkdirTwilioSend
2
cdTwilioSend
3
dotnetnew console
4
dotnetadd package Twilio
Open the file in your new project called Program.cs and paste in the following code, replacing the existing template code:
Send an SMS Using Twilio with C# (.NET Core)
1
// Install the C# / .NET helper library from twilio.com/docs/csharp/install
2
3
usingSystem;
4
usingTwilio;
5
usingTwilio.Rest.Api.V2010.Account;
6
usingSystem.Threading.Tasks;
7
8
classProgram{
9
public static asyncTaskMain(string[]args) {
10
// Find your Account SID and Auth Token at twilio.com/console
11
// and set the environment variables. See http://twil.io/secure
body:"Join Earth's mightiest heroes. Like Kevin Bacon.",
19
from:newTwilio.Types.PhoneNumber("+15017122661"),
20
to:newTwilio.Types.PhoneNumber("+15558675310"));
21
22
Console.WriteLine(message.Body);
23
}
24
}
In the Program.cs file, replace the values for accountSid and authToken with your Account SID and Auth Token surrounded by quotation marks.
(error)
Danger
This quickstart hardcodes your credentials to get you started quickly. Use environment variables and API keys to keep credentials secret and control access when you deploy to production.
Replace the value for from: new Twilio.Types.PhoneNumber with the phone number that Twilio gave you in E.164 format.
Replace the value for to: new Twilio.Types.PhoneNumber with the Twilio Virtual Phone number (+18777804236).
Save your changes and run this command in the directory that contains Program.cs:
dotnetrun
After a few moments, you receive an SMS from your Twilio number on the Twilio Virtual Phone.
Create and open a new file called Example.java in the same directory as the fat jar file and paste in the following code:
Send an SMS Using Twilio with Java
1
// Install the Java helper library from twilio.com/docs/java/install
2
3
importcom.twilio.type.PhoneNumber;
4
importcom.twilio.Twilio;
5
importcom.twilio.rest.api.v2010.account.Message;
6
7
public classExample{
8
// Find your Account SID and Auth Token at twilio.com/console
9
// and set the environment variables. See http://twil.io/secure
10
public static finalString ACCOUNT_SID=System.getenv("TWILIO_ACCOUNT_SID");
11
public static finalString AUTH_TOKEN=System.getenv("TWILIO_AUTH_TOKEN");
"This is the ship that made the Kessel Run in fourteen parsecs?")
19
.create();
20
21
System.out.println(message.getBody());
22
}
23
}
In the Example.java file, replace the values for ACCOUNT_SID and AUTH_TOKEN with your Account SID and Auth Token surrounded by quotation marks.
(error)
Danger
This quickstart hardcodes your credentials to get you started quickly. Use environment variables and API keys to keep credentials secret and control access when you deploy to production.
Replace the value for the first phone number with the Twilio Virtual Phone number (+18777804236).
Replace the value for the second phone number with the phone number that Twilio gave you in E.164 format.
Save your changes and compile the Java from your terminal in the directory that contains Example.java. Replace 10.9.0 with the version of your fat jar file.
Create and open a new file called send_sms.go in your Go project directory and paste in the following code:
Send an SMS Using Twilio with Go
1
// Download the helper library from https://www.twilio.com/docs/go/install
2
packagemain
3
4
import(
5
"fmt"
6
"github.com/twilio/twilio-go"
7
api"github.com/twilio/twilio-go/rest/api/v2010"
8
"os"
9
)
10
11
funcmain() {
12
// Find your Account SID and Auth Token at twilio.com/console
13
// and set the environment variables. See http://twil.io/secure
14
// Make sure TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN exists in your environment
15
client:=twilio.NewRestClient()
16
17
params:= &api.CreateMessageParams{}
18
params.SetBody("Join Earth's mightiest heroes. Like Kevin Bacon.")
19
params.SetFrom("+15017122661")
20
params.SetTo("+15558675310")
21
22
resp, err:=client.Api.CreateMessage(params)
23
iferr!=nil{
24
fmt.Println(err.Error())
25
os.Exit(1)
26
}else{
27
ifresp.Body!=nil{
28
fmt.Println(*resp.Body)
29
}else{
30
fmt.Println(resp.Body)
31
}
32
}
33
}
Run the following command in the terminal to set your environment variables. Replace <your-account-sid> and <your-auth-token> with your Account SID and Auth Token:
body:'Join Earth\'s mightiest heroes. Like Kevin Bacon.',
17
from:'+15017122661',
18
to:'+15558675310'
19
)
20
21
putsmessage.body
In the send_sms.rb file, replace the values for account_sid and auth_token with your Account SID and Auth Token surrounded by single quotation marks.
(error)
Danger
This quickstart hardcodes your credentials to get you started quickly. Use environment variables and API keys to keep credentials secret and control access when you deploy to production.
Replace the value for from with the phone number that Twilio gave you in E.164 format.
Replace the value for to with the Twilio Virtual Phone number (+18777804236).
Save your changes and run this command from your terminal in the directory that contains send_sms.rb:
rubysend_sms.rb
After a few moments, you receive an SMS from your Twilio number on the Twilio Virtual Phone.
Receive and reply to an inbound SMS message
Follow these steps to reply to an SMS message sent to your Twilio phone number.
resp.message("The Robots are coming! Head for the hills!")
11
12
# Return the TwiML (as XML) response
13
returnResponse(str(resp),mimetype='text/xml')
14
15
if__name__=="__main__":
16
app.run(port=3000)
Save the file.
In a new terminal window, run the following command to start the Python development server on port 3000:
pythonreply_sms.py
In a new terminal window, run the following command to start ngrok and create a tunnel to your localhost:
ngrokhttp3000
(warning)
Warning
Use ngrok only for testing because it creates a temporary URL that exposes your local development machine to the internet. Host your application with a cloud provider or your public server when you deploy to production.
Set up a webhook that triggers when your Twilio phone number receives an SMS message:
In the Messaging Configuration section, in the URL field for A message comes in, enter the temporary forwarding URL from your ngrok console with /reply_sms appended to the end.
For example, if your ngrok console shows Forwarding https://1aaa-123-45-678-910.ngrok-free.app, enter https://1aaa-123-45-678-910.ngrok-free.app/reply_sms.
Click Save configuration.
With the Python development server and ngrok running, send an SMS to your Twilio phone number:
Enter a message in the Click here to reply field at the bottom of the Twilio Virtual Phone.
Click the send icon.
An HTTP request shows in your ngrok console, and you get the response back as an SMS on the Twilio Virtual Phone.
Create and open a new file called server.js anywhere on your machine and paste in the following code:
Respond to an Incoming Text Message with Node.js
1
constexpress=require('express');
2
const{MessagingResponse}=require('twilio').twiml;
3
4
constapp=express();
5
6
app.post('/sms', (req,res)=>{
7
consttwiml= newMessagingResponse();
8
9
twiml.message('The Robots are coming! Head for the hills!');
10
11
res.type('text/xml').send(twiml.toString());
12
});
13
14
app.listen(3000, ()=>{
15
console.log('Express server listening on port 3000');
16
});
In a new terminal window, start the Node.js development server on port 3000 by running this command in the directory that contains server.js:
nodeserver.js
In a new terminal window, run the following command to start ngrok and create a tunnel to your localhost:
ngrokhttp3000
(warning)
Warning
Use ngrok only for testing because it creates a temporary URL that exposes your local development machine to the internet. Host your application with a cloud provider or your public server when you deploy to production.
Set up a webhook that triggers when your Twilio phone number receives an SMS message:
In the Messaging Configuration section, in the URL field for A message comes in, enter the temporary forwarding URL from your ngrok console with /sms appended to the end.
For example, if your ngrok console shows Forwarding https://1aaa-123-45-678-910.ngrok-free.app, enter https://1aaa-123-45-678-910.ngrok-free.app/sms.
Click Save configuration.
With the Node.js development server and ngrok running, send an SMS to your Twilio phone number:
Enter a message in the Click here to reply field at the bottom of the Twilio Virtual Phone.
Click the send icon.
An HTTP request shows in your ngrok console, and you get the response back as an SMS on the Twilio Virtual Phone.
Create and open a new file called reply_sms.php in the same directory as send_sms.php and paste in the following code:
1
<?php
2
require_once"vendor/autoload.php";
3
useTwilio\TwiML\MessagingResponse;
4
5
// Set the content-type to XML to send back TwiML from the PHP Helper Library
6
header("content-type: text/xml");
7
8
$response= newMessagingResponse();
9
$response->message(
10
"The Robots are coming! Head for the hills!"
11
);
12
13
echo$response;
Save the file.
In a new terminal window, start the PHP development server on port 3000 by running this command:
php-Slocalhost:3000
In a new terminal window, run the following command to start ngrok and create a tunnel to your localhost:
ngrokhttp3000
(warning)
Warning
Use ngrok only for testing because it creates a temporary URL that exposes your local development machine to the internet. Host your application with a cloud provider or your public server when you deploy to production.
Set up a webhook that triggers when your Twilio phone number receives an SMS message:
In the Messaging Configuration section, in the URL field for A message comes in, enter the temporary forwarding URL from your ngrok console with /reply_sms.php appended to the end.
For example, if your ngrok console shows Forwarding https://1aaa-123-45-678-910.ngrok-free.app, enter https://1aaa-123-45-678-910.ngrok-free.app/reply_sms.php.
Click Save configuration.
With the PHP development server and ngrok running, send an SMS to your Twilio phone number:
Enter a message in the Click here to reply field at the bottom of the Twilio Virtual Phone.
Click the send icon.
An HTTP request shows in your ngrok console, and you get the response back as an SMS on the Twilio Virtual Phone.
Create a new ASP.NET MVC Project in Visual Studio:
Open Visual Studio and click Create a new project.
In Visual Studio, run the application by clicking the play arrow. Your web browser opens on a localhost URL. Note the port number; for example, if the URL opens on https://localhost:44360, your port number is 44360.
In a new terminal window, run the following command to start ngrok and create a tunnel to your localhost. Replace <port> with the port number from your application.
ngrokhttp<port>
(warning)
Warning
Use ngrok only for testing because it creates a temporary URL that exposes your local development machine to the internet. Host your application with a cloud provider or your public server when you deploy to production.
Set up a webhook that triggers when your Twilio phone number receives an SMS message:
In the Messaging Configuration section, in the URL field for A message comes in, enter the temporary forwarding URL from your ngrok console with /sms appended to the end.
For example, if your ngrok console shows Forwarding https://1aaa-123-45-678-910.ngrok-free.app, enter https://1aaa-123-45-678-910.ngrok-free.app/sms.
Click Save configuration.
With the application and ngrok running, send an SMS to your Twilio phone number:
Enter a message in the Click here to reply field at the bottom of the Twilio Virtual Phone.
Click the send icon.
An HTTP request shows in your ngrok console, and you get the response back as an SMS on the Twilio Virtual Phone.
Run the following commands to create a new ASP.NET Core project and install the Twilio NuGet package:
1
mkdirTwilioReceive
2
cdTwilioReceive
3
dotnetnew mvc
4
dotnetadd package Twilio.AspNet.Core
In the Controllers directory, create a file named SmsController.cs and paste in the following code:
From the root of the project directory, run following command to start the application:
dotnetrun
Check the command output for the localhost URL. Note the port number; for example, if the URL opens on https://localhost:5242, your port number is 5242.
In a new terminal window, run the following command to start ngrok and create a tunnel to your localhost. Replace <port> with the port number from your command output.
ngrokhttp<port>
(warning)
Warning
Use ngrok only for testing because it creates a temporary URL that exposes your local development machine to the internet. Host your application with a cloud provider or your public server when you deploy to production.
Set up a webhook that triggers when your Twilio phone number receives an SMS message:
In the Messaging Configuration section, in the URL field for A message comes in, enter the temporary forwarding URL from your ngrok console with /sms appended to the end.
For example, if your ngrok console shows Forwarding https://1aaa-123-45-678-910.ngrok-free.app, enter https://1aaa-123-45-678-910.ngrok-free.app/sms.
Click Save configuration.
With the application and ngrok running, send an SMS to your Twilio phone number:
Enter a message in the Click here to reply field at the bottom of the Twilio Virtual Phone.
Click the send icon.
An HTTP request shows in your ngrok console, and you get the response back as an SMS on the Twilio Virtual Phone.
Create and set up the IntelliJ project.
Open IntelliJ IDEA Community Edition.
Create a new project with either Maven or Gradle as the build system.
Click File > New> Java Class to create a new Java class. Name the class SmsApp.
In the new SmsApp.java file that IntelliJ creates, paste in the following code:
Respond to an Incoming Text Message with Java
1
importcom.twilio.twiml.MessagingResponse;
2
importcom.twilio.twiml.messaging.Body;
3
importcom.twilio.twiml.messaging.Message;
4
5
import staticspark.Spark.*;
6
7
public classSmsApp{
8
public static voidmain(String[]args) {
9
get("/", (req, res)->"Hello Web");
10
11
post("/sms", (req, res)->{
12
res.type("application/xml");
13
Body body= newBody
14
.Builder("The Robots are coming! Head for the hills!")
15
.build();
16
Message sms= newMessage
17
.Builder()
18
.body(body)
19
.build();
20
MessagingResponse twiml= newMessagingResponse
21
.Builder()
22
.message(sms)
23
.build();
24
returntwiml.toXml();
25
});
26
}
27
}
Right-click on the SmsApp class in the project outline and choose Run 'SmsApp.main()'.
The Java spark web application server starts listening on port 4567.
In a new terminal window, run the following command to start ngrok and create a tunnel to your localhost:
ngrokhttp4567
(warning)
Warning
Use ngrok only for testing because it creates a temporary URL that exposes your local development machine to the internet. Host your application with a cloud provider or your public server when you deploy to production.
Set up a webhook that triggers when your Twilio phone number receives an SMS message:
In the Messaging Configuration section, in the URL field for A message comes in, enter the temporary forwarding URL from your ngrok console with /sms appended to the end.
For example, if your ngrok console shows Forwarding https://1aaa-123-45-678-910.ngrok-free.app, enter https://1aaa-123-45-678-910.ngrok-free.app/sms.
Click Save configuration.
With the Java development server and ngrok running, send an SMS to your Twilio phone number:
Enter a message in the Click here to reply field at the bottom of the Twilio Virtual Phone.
Click the send icon.
An HTTP request shows in your ngrok console, and you get the response back as an SMS on the Twilio Virtual Phone.
Create and open a new file called server.go in your Go project directory and paste in the following code:
Respond to an Incoming Text Message with Go
1
packagemain
2
3
import(
4
"net/http"
5
6
"github.com/gin-gonic/gin"
7
"github.com/twilio/twilio-go/twiml"
8
)
9
10
funcmain() {
11
router:=gin.Default()
12
13
router.POST("/sms",func(context*gin.Context) {
14
message:= &twiml.MessagingMessage{
15
Body:"The Robots are coming! Head for the hills!",
In a new terminal window, start the Go development server on port 3000 by running this command in the directory that contains server.go:
gorun server.go
In a new terminal window, run the following command to start ngrok and create a tunnel to your localhost:
ngrokhttp3000
(warning)
Warning
Use ngrok only for testing because it creates a temporary URL that exposes your local development machine to the internet. Host your application with a cloud provider or your public server when you deploy to production.
Set up a webhook that triggers when your Twilio phone number receives an SMS message:
In the Messaging Configuration section, in the URL field for A message comes in, enter the temporary forwarding URL from your ngrok console with /sms appended to the end.
For example, if your ngrok console shows Forwarding https://1aaa-123-45-678-910.ngrok-free.app, enter https://1aaa-123-45-678-910.ngrok-free.app/sms.
Click Save configuration.
With the Go development server and ngrok running, send an SMS to your Twilio phone number:
Enter a message in the Click here to reply field at the bottom of the Twilio Virtual Phone.
Click the send icon.
An HTTP request shows in your ngrok console, and you get the response back as an SMS on the Twilio Virtual Phone.
To install Sinatra and the Twilio Ruby helper library, create and open a new file called Gemfile anywhere on your machine and paste in the following code.
In the same directory as Gemfile, run the following command from the terminal:
bundleinstall
Create and open a new file called reply_sms.rb in the same directory as Gemfile and paste in the following code:
1
require'twilio-ruby'
2
require'sinatra'
3
4
# disable HostAuthorization for development only
5
configure:developmentdo
6
set:host_authorization, {permitted_hosts:[] }
7
end
8
9
post'/sms-quickstart'do
10
twiml=Twilio::TwiML::MessagingResponse.new do|r|
11
r.message(body:'Ahoy! Thanks so much for your message.')
12
end
13
14
twiml.to_s
15
end
Save the file.
In a new terminal window, start the Ruby development server on port 4567 by running this command:
rubyreply_sms.rb
In a new terminal window, run the following command to start ngrok and create a tunnel to your localhost:
ngrokhttp4567
(warning)
Warning
Use ngrok only for testing because it creates a temporary URL that exposes your local development machine to the internet. Host your application with a cloud provider or your public server when you deploy to production.
Set up a webhook that triggers when your Twilio phone number receives an SMS message:
In the Messaging Configuration section, in the URL field for A message comes in, enter the temporary forwarding URL from your ngrok console with /sms-quickstart appended to the end.
For example, if your ngrok console shows Forwarding https://1aaa-123-45-678-910.ngrok-free.app, enter https://1aaa-123-45-678-910.ngrok-free.app/sms-quickstart.
Click Save configuration.
With the Ruby development server and ngrok running, send an SMS to your Twilio phone number:
Enter a message in the Click here to reply field at the bottom of the Twilio Virtual Phone.
Click the send icon.
An HTTP request shows in your ngrok console, and you get the response back as an SMS on the Twilio Virtual Phone.