In this quickstart, you'll create an app that uses the WhatsApp Business Platform with Twilio to send and receive messages. You don't need to wait for WhatsApp sender registration because you'll use the Twilio Sandbox for WhatsApp as your sender.
You'll access the Programmable Messaging REST API and build using your preferred programming language with the Twilio Helper Libraries.
Prerequisites
Select your programming language and complete the prerequisites:
The Twilio Sandbox for WhatsApp (the Sandbox) acts as your WhatsApp sender. You can test messaging without waiting for your WhatsApp sender registration and verification.
To connect your WhatsApp account to the Sandbox, send join <your sandbox code> to the Sandbox number, or scan the QR code with your mobile device and send the prepopulated message. The Sandbox replies to confirm that you've joined.
To disconnect from the Sandbox, reply to the message with stop or switch to a different Sandbox by messaging join <other sandbox keyword>.
Set environment variables
You need your Twilio account credentials to send requests. Follow these steps to get your account credentials and set them as environment variables.
Copy your Account SID and set it as an environment variable using the following command. Replace ACCOUNT_SID with your Account SID.
$Env:TWILIO_ACCOUNT_SID="ACCOUNT_SID"
Copy your Auth Token and set it as an environment variable using the following command. Replace AUTH_TOKEN with your Auth Token.
$Env:TWILIO_AUTH_TOKEN="AUTH_TOKEN"
Send a WhatsApp message from the Sandbox
Send a message using one of the pre-approved message templates available from the Sandbox. This quickstart uses the Appointment Reminder template.
(warning)
24-hour customer service window for non-template messaging
WhatsApp Business Platform requires the use of a message template for business-initiated messages. Each time a user sends your business a message, you have a 24-hour customer service window in which to send free-form outbound messages without a template. Learn more about customer service windows.
Follow these steps to send a message from the Sandbox number to your personal WhatsApp account:
PythonNode.jsPHPC# (.NET Framework)JavacurlGoRuby
Create and open a new file called send_whatsapp.py anywhere on your machine and paste in the following code:
Send a message with WhatsApp, Twilio, and Python
1
# Download the helper library from https://www.twilio.com/docs/python/install
2
importos
3
fromtwilio.restimportClient
4
importjson
5
6
# Find your Account SID and Auth Token at twilio.com/console
7
# and set the environment variables. See http://twil.io/secure
8
account_sid=os.environ["TWILIO_ACCOUNT_SID"]
9
auth_token=os.environ["TWILIO_AUTH_TOKEN"]
10
client=Client(account_sid, auth_token)
11
12
message=client.messages.create(
13
from_="whatsapp:+14155238886",
14
to="whatsapp:+16285550100",
15
content_sid="HXb5b62575e6e4ff6129ad7c8efe1f983e",
16
content_variables=json.dumps({"1":"22 July 2026","2":"3:15pm"}),
17
)
18
19
print(message.body)
Replace the value for to with the whatsapp: prefix and your personal WhatsApp number in E.164 format. For example, whatsapp:+16285550100.
Save your changes and run this command from your terminal in the directory that contains send_whatsapp.py:
pythonsend_whatsapp.py
After a few moments, you receive a WhatsApp message from the Sandbox to your personal WhatsApp account.
Create and open a new file called send_whatsapp.js anywhere on your machine and paste in the following code:
Send a message with WhatsApp, Twilio, and 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
contentSid:"HXb5b62575e6e4ff6129ad7c8efe1f983e",
13
contentVariables:JSON.stringify({1:"22 July 2026",2:"3:15pm"}),
14
from:"whatsapp:+14155238886",
15
to:"whatsapp:+16285550100",
16
});
17
18
console.log(message.body);
19
}
20
21
createMessage();
Replace the value for to with the whatsapp: prefix and your personal WhatsApp number in E.164 format. For example, whatsapp:+16285550100.
Save your changes and run the following command from your terminal in the directory that contains send_whatsapp.js:
nodesend_whatsapp.js
After a few moments, you receive a WhatsApp message from the Sandbox to your personal WhatsApp account.
Create and open a new file called send_whatsapp.php in the project directory and paste in the following code:
Send a message with WhatsApp, Twilio, and 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
Replace the first argument to $twilio->messages->create() with your personal WhatsApp number in E.164 format, prefixed by whatsapp:. For example, whatsapp:+16285550100.
Replace line 5 of send_whatsapp.php with the following:
require__DIR__.'/vendor/autoload.php';
Save your changes and run this command from your terminal in the directory that contains send_whatsapp.php:
phpsend_whatsapp.php
After a few moments, you receive a WhatsApp message from the Sandbox to your personal WhatsApp account.
Create and set up a new project in Visual Studio:
Open Visual Studio and click Create a new project.
newDictionary<string,Object>() { {"1","22 July 2026"}, {"2","3:15pm"} },
25
Formatting.Indented));
26
27
Console.WriteLine(message.Body);
28
}
29
}
Replace the value for to: new Twilio.Types.PhoneNumber with the whatsapp: prefix and your personal WhatsApp number in E.164 format. For example, whatsapp:+16285550100.
Save your changes and run your project in Visual Studio.
After a few moments, you receive a WhatsApp message from the Sandbox to your personal WhatsApp account.
Create and open a new file called Example.java in the same directory as the fat jar file and paste in the following code:
1
importcom.twilio.type.PhoneNumber;
2
importjava.util.HashMap;
3
importcom.twilio.Twilio;
4
importcom.twilio.rest.api.v2010.account.Message;
5
importorg.json.JSONObject;
6
7
public classExample{
8
public static finalString ACCOUNT_SID=System.getenv("TWILIO_ACCOUNT_SID");
9
public static finalString AUTH_TOKEN=System.getenv("TWILIO_AUTH_TOKEN");
Replace the value for the first phone number (the recipient) with the whatsapp: prefix and your personal WhatsApp number in E.164 format. For example, whatsapp:+16285550100.
Save your changes and compile the code from your terminal in the directory that contains Example.java. Replace 10.9.0 with the version of your fat jar file.
Replace the value for to with the whatsapp: prefix and your personal WhatsApp number in E.164 format. For example, whatsapp:+16285550100.
Save your changes and run this command from your terminal in the directory that contains send_whatsapp.rb:
rubysend_whatsapp.rb
After a few moments, you receive a WhatsApp message from the Sandbox to your personal WhatsApp account.
Receive a WhatsApp message to the Sandbox and send an automated reply
When someone replies to one of your WhatsApp messages, you receive a webhook request from Twilio. To handle this request, you need to configure the webhook, create a web application that responds to an incoming message with TwiML, and expose your application to the internet.
Follow these steps to have the Sandbox reply to a WhatsApp message that you send from your personal WhatsApp account:
PythonNode.jsPHPC# (.NET Framework)JavacurlGoRuby
Create and open a new file called reply_whatsapp.py anywhere on your machine and paste in the following code:
resp.message("Message received! Hello again from the Twilio Sandbox for WhatsApp.")
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_whatsapp.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 the Sandbox receives a WhatsApp message:
In the Sandbox Configuration section, in the When a message comes in field, enter the temporary forwarding URL from your ngrok console with /reply_whatsapp 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_whatsapp.
Click Save.
With the Python development server and ngrok running, send a WhatsApp message to the Sandbox number from your personal WhatsApp account.
An HTTP request shows in your ngrok console, and you receive the response message in your personal WhatsApp account.
Create and open a new file called server.js anywhere on your machine and paste in the following code:
Respond to an incoming WhatsApp message with Node.js
1
constexpress=require('express');
2
const{MessagingResponse}=require('twilio').twiml;
3
4
constapp=express();
5
6
app.post('/whatsapp', (req,res)=>{
7
consttwiml= newMessagingResponse();
8
9
twiml.message('Message received! Hello again from the Twilio Sandbox for WhatsApp.');
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 the Sandbox receives a WhatsApp message:
In the Sandbox Configuration section, in the When a message comes in field, enter the temporary forwarding URL from your ngrok console with /whatsapp 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/whatsapp.
Click Save.
With the Node.js development server and ngrok running, send a WhatsApp message to the Sandbox number from your personal WhatsApp account.
An HTTP request shows in your ngrok console, and you receive the response message in your personal WhatsApp account.
Create and open a new file called reply_whatsapp.php in the same directory as send_whatsapp.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
"Message received! Hello again from the Twilio Sandbox for WhatsApp."
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 reply_whatsapp.php
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 the Sandbox receives a WhatsApp message:
messagingResponse.Message("Message received! Hello again from the Twilio Sandbox for WhatsApp.");
17
18
returnTwiML(messagingResponse);
19
}
20
}
21
}
Save the file.
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 the Sandbox receives a WhatsApp message:
In the Sandbox Configuration section, in the When a message comes in field, enter the temporary forwarding URL from your ngrok console with /whatsapp 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/whatsapp.
Click Save.
With the application and ngrok running, send a WhatsApp message to the Sandbox number from your personal WhatsApp account.
An HTTP request shows in your ngrok console, and you receive the response message in your personal WhatsApp account.
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.
Add the following dependencies to your Maven or Gradle build file:
To create a new Java class, click File > New > Java Class. Name the class WhatsappApp.
In the new WhatsappApp.java file that IntelliJ creates, paste in the following code:
Respond to an incoming WhatsApp 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 classWhatsAppApp{
8
public static voidmain(String[]args) {
9
get("/", (req, res)->"Hello Web");
10
11
post("/whatsapp", (req, res)->{
12
res.type("application/xml");
13
Body body= newBody
14
.Builder("Message received! Hello again from the Twilio Sandbox for WhatsApp.")
15
.build();
16
Message whatsapp= newMessage
17
.Builder()
18
.body(body)
19
.build();
20
MessagingResponse twiml= newMessagingResponse
21
.Builder()
22
.message(whatsapp)
23
.build();
24
returntwiml.toXml();
25
});
26
}
27
}
Right-click on the WhatsappApp class in the project outline and choose Run 'WhatsappApp.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 the Sandbox receives a WhatsApp message:
In the Sandbox Configuration section, in the When a message comes in field, enter the temporary forwarding URL from your ngrok console with /whatsapp 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/whatsapp.
Click Save.
With the Java development server and ngrok running, send a WhatsApp message to the Sandbox number from your personal WhatsApp account.
An HTTP request shows in your ngrok console, and you receive the response message in your personal WhatsApp account.
Although we're sure it's possible to run a server from your command line, we suggest exploring how to set up your environment, send messages, and respond to messages with TwiML in the programming language of your choice.
To respond to messages, you'll set up a webhook that triggers when the Sandbox receives a WhatsApp message. You can configure webhooks by connecting the Sandbox to an app you've already built for handling incoming messages, or build a new one for WhatsApp messages.
Create and open a new file called server.go in your Go project directory and paste in the following code:
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 the Sandbox receives a WhatsApp message:
In the Sandbox Configuration section, in the When a message comes in field, enter the temporary forwarding URL from your ngrok console with /whatsapp 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/whatsapp.
Click Save.
With the Go development server and ngrok running, send a WhatsApp message to the Sandbox number from your personal WhatsApp account.
An HTTP request shows in your ngrok console, and you receive the response message in your personal WhatsApp account.
Create and open a new file called reply_whatsapp.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'/whatsapp'do
10
twiml=Twilio::TwiML::MessagingResponse.new do|r|
11
r.message(body:'Message received! Hello again from the Twilio Sandbox for WhatsApp.')
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_whatsapp.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 the Sandbox receives a WhatsApp message:
In the Sandbox Configuration section, in the When a message comes in field, enter the temporary forwarding URL from your ngrok console with /whatsapp 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/whatsapp.
Click Save.
With the Ruby development server and ngrok running, send a WhatsApp message to the Sandbox number from your personal WhatsApp account.
An HTTP request shows in your ngrok console, and you receive the response message in your personal WhatsApp account.