Skip to contentSkip to navigationSkip to topbar
On this page

Quickstart: Send and receive WhatsApp messages


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

prerequisites page anchor

Select your programming language and complete the prerequisites:

PythonNode.jsPHPC# (.NET Framework)JavacurlGoRuby
  • Get a WhatsApp account(link takes you to an external page) and install WhatsApp on your device.

  • Install Python(link takes you to an external page).

  • Install Flask(link takes you to an external page) and Twilio's Python helper library(link takes you to an external page). To install using pip(link takes you to an external page), run:

    pip install flask twilio
  • Install and set up ngrok(link takes you to an external page).


Sign up for Twilio and activate the Sandbox

sign-up-for-twilio-and-activate-the-sandbox page anchor

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.

  1. Sign up for Twilio(link takes you to an external page).
  2. Activate and connect to the Twilio Sandbox for WhatsApp:
    1. Go to the Try WhatsApp page in the Console(link takes you to an external page), acknowledge the terms, and click Confirm.
    2. 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

set-environment-variables page anchor

You need your Twilio account credentials to send requests. Follow these steps to get your account credentials and set them as environment variables.

macOS TerminalWindows command linePowerShell
  1. Go to the Twilio Console(link takes you to an external page).
  2. Copy your Account SID and set it as an environment variable using the following command. Replace ACCOUNT_SID with your Account SID.
    export TWILIO_ACCOUNT_SID=ACCOUNT_SID
  3. Copy your Auth Token and set it as an environment variable using the following command. Replace AUTH_TOKEN with your Auth Token.
    export TWILIO_AUTH_TOKEN=AUTH_TOKEN

Send a WhatsApp message from the Sandbox

send-a-whatsapp-message-from-the-sandbox page anchor

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
  1. 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 PythonLink to code sample: Send a message with WhatsApp, Twilio, and Python
    1
    # Download the helper library from https://www.twilio.com/docs/python/install
    2
    import os
    3
    from twilio.rest import Client
    4
    import json
    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)
  2. Replace the value for to with the whatsapp: prefix and your personal WhatsApp number in E.164 format. For example, whatsapp:+16285550100.

  3. Save your changes and run this command from your terminal in the directory that contains send_whatsapp.py:

    python send_whatsapp.py

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

receive-a-whatsapp-message-to-the-sandbox-and-send-an-automated-reply page anchor

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
  1. Create and open a new file called reply_whatsapp.py anywhere on your machine and paste in the following code:

    1
    from flask import Flask, request, Response
    2
    from twilio.twiml.messaging_response import MessagingResponse
    3
    4
    app = Flask(__name__)
    5
    6
    @app.route("/reply_whatsapp", methods=['POST'])
    7
    def reply_whatsapp():
    8
    # Create a new Twilio MessagingResponse
    9
    resp = MessagingResponse()
    10
    resp.message("Message received! Hello again from the Twilio Sandbox for WhatsApp.")
    11
    12
    # Return the TwiML (as XML) response
    13
    return Response(str(resp), mimetype='text/xml')
    14
    15
    if __name__ == "__main__":
    16
    app.run(port=3000)

    Save the file.

  2. In a new terminal window, run the following command to start the Python development server on port 3000:

    python reply_whatsapp.py
  3. In a new terminal window, run the following command to start ngrok(link takes you to an external page) and create a tunnel to your localhost:

    ngrok http 3000
    (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.

  4. Set up a webhook that triggers when the Sandbox receives a WhatsApp message:

    1. Open the Try WhatsApp page in the Twilio Console(link takes you to an external page).

    2. Click Sandbox settings.

    3. 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.

    4. Click Save.

  5. 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.


The WhatsApp Business Platform with Twilio uses the same REST API resources as the Twilio Programmable Messaging API. Many Twilio Messaging use cases apply to WhatsApp. For WhatsApp-specific details, see the WhatsApp overview and the API Reference.

Here are some areas to explore next.