# Programmable Messaging for WhatsApp Ruby Quickstart

> \[!WARNING]
>
> Twilio is launching a new Console. Some screenshots on this page may show the Legacy Console and therefore may no longer be accurate. We are working to update all screenshots to reflect the new Console experience. [Learn more about the new Console](https://www.twilio.com/blog/new-and-improved-console-now-in-general-availability).

With just a few lines of code, your application can send and receive messages with WhatsApp using the [Twilio API for WhatsApp](/docs/whatsapp/api).

This WhatsApp Quickstart will teach you how to do this using the Twilio Sandbox for WhatsApp, Ruby, [the Twilio Ruby Twilio helper library](/docs/libraries), and the [Sinatra web framework](https://sinatrarb.com/). In this Quickstart, you will learn how to:

1. Sign up for Twilio and activate the Sandbox.
2. Set up your development environment to send and receive messages
3. Opt-in to the Sandbox
4. Send your first WhatsApp message
5. Receive inbound WhatsApp messages
6. Reply to incoming WhatsApp messages

Prefer to watch a video? The video below shows you how to use the Sandbox to send and receive WhatsApp messages.

https://www.youtube.com/watch?v=UVez2UyjpFk

## Sign up for Twilio and activate the Sandbox

Before you can send a WhatsApp message from your web language, you'll need to [sign up for a Twilio account](https://www.twilio.com/try-twilio) or sign into your existing account and activate the [Twilio Sandbox for WhatsApp](https://www.twilio.com/console/sms/whatsapp/sandbox). It allows you to prototype with WhatsApp immediately using a shared phone number, without waiting for a dedicated number to be approved by WhatsApp.

To get started, select a number from the available sandbox numbers to activate your sandbox.

![WA\_Sandbox.](https://docs-resources.prod.twilio.com/12bea172c44c2e43ed9695169dbbf3c1c0879a5cd2cfff3150de1041648e6621.png)

Be sure to take note of the phone number you choose in the Sandbox. You will need this later when we're ready to send some messages.

## Gather your Twilio account information

Before you can send any messages, you'll need to gather your Twilio account credentials. You can find these in the [Twilio Console](https://www.twilio.com/console).

* **Account SID** - Used to authenticate REST API requests
* **Auth Token** - Used to authenticate REST API requests

![Account Credentials.](https://docs-resources.prod.twilio.com/bc6afaa57c1fe443cae20a658cf7cd424c18fa660b55c3b6722f77622844fc4c.png)

For all of our code snippets and curl examples, you will need to authenticate with the **Account SID** and **Auth Token**.

## Set up your Ruby development environment

The next steps will involve writing some code. We've written up development environment setup in Ruby, where you'll set up a dev environment and prepare to build a web app with Sinatra.

* [Ruby development environment setup](/docs/usage/tutorials/how-to-set-up-your-ruby-and-sinatra-development-environment)

If you haven't yet set up your development environment to write some Ruby, head there first to get ready. This quickstart will be waiting.

## Send a message with WhatsApp in Ruby

> \[!WARNING]
>
> The Twilio Sandbox for WhatsApp uses a single WhatsApp Sender shared by all Twilio users. To begin using the Sandbox, you **must** send a join message
> from the number you wish to send outbound messages. Each number may only receive messages from one Sandbox at a time.
>
> When registering your own WhatsApp Sender, these limits do not apply. [Learn how to register your own WhatsApp Senders](/docs/whatsapp#whatsapp-sender-registration).

### Sandbox Opt-in Message

Send "`join <your sandbox keyword>`" to your Sandbox number in WhatsApp to join your Sandbox, and we'll reply with a confirmation that you've joined. Your sandbox keyword can be found in the console.

Once you join, you will only receive messages from your specific Sandbox. To disconnect from the sandbox, you can reply to the message from WhatsApp with `sandbox stop`, or switch to a different sandbox by messaging `join <other sandbox keyword>`.

![Set up the Twilio Sandbox for WhatsApp by sending "join your-sandbox-keyword" to a given number.](https://docs-resources.prod.twilio.com/d28cc8a0e30de10a3950b3f8d38e9afff01e81359dd56abecc9ece80119ef5c0.png)

### Inviting other users to your sandbox (OPTIONAL)

To invite someone else to your sandbox, create a link with the following format containing the opt-in message and send it to them: `whatsapp://send?phone=<Your Sandbox Number>&text=<your URL-encoded sandbox keyword>`

You can also create a [QR code](https://www.qr-code-generator.com/) with the link format above that users can scan on their phone to opt-in to your sandbox.

![whatsapp-sandbox-opt-in-qr.](https://docs-resources.prod.twilio.com/080dfe8dd47d677047fd30f711b0f1eb901bfbbfaccaa9a2ea84e6ee24ee04d5.png)

To send a message, use the following code and replace the **to** parameter with the phone number for your personal WhatsApp account in the [E.164 format](/docs/glossary/what-e164). (If you haven't already, install WhatsApp on your device and register for an account.) For the **from** parameter, be sure to include the `whatsapp:` channel identifier before the Sandbox number in [E.164 format](/docs/glossary/what-e164).

For the `From` parameter, you need your `Sender ID` which should be shown on the installed channel in the Twilio Console, as shown previously.

Send a message with WhatsApp and Ruby

```ruby
# Download the helper library from https://www.twilio.com/docs/ruby/install
require 'rubygems'
require 'twilio-ruby'

# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_AUTH_TOKEN']
@client = Twilio::REST::Client.new(account_sid, auth_token)

message = @client
          .api
          .v2010
          .messages
          .create(
            to: 'whatsapp:+13233633791',
            from: 'whatsapp:+14155238886',
            body: 'Hello there!'
          )

puts message.body
```

```json
{
  "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "api_version": "2010-04-01",
  "body": "Hello there!",
  "date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
  "date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
  "date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
  "direction": "outbound-api",
  "error_code": null,
  "error_message": null,
  "from": "whatsapp:+14155238886",
  "num_media": "0",
  "num_segments": "1",
  "price": null,
  "price_unit": null,
  "messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "status": "queued",
  "subresource_uris": {
    "media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"
  },
  "tags": {
    "campaign_name": "Spring Sale 2022",
    "message_type": "cart_abandoned"
  },
  "to": "whatsapp:+13233633791",
  "uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"
}
```

## Receive and reply to messages from WhatsApp

When someone replies to one of your messages, you will receive a [webhook](/docs/glossary/what-is-a-webhook) request from Twilio.

You can configure webhooks by connecting your Sandbox to an app you've already built for handling incoming messages, or build a new one for WhatsApp messages.

![Configure WhatsApp Sandbox Webhook.](https://docs-resources.prod.twilio.com/daa22c39a40e5213551105c4ec5b363c73f5a638edac1105b79181d09ee5e502.png)

This webhook request also supports [TwiML](/docs/messaging/twiml) (*the Twilio Markup Language*) just like a regular Twilio SMS request.

To handle this request, you need to set up a web application and expose it to the internet. The [SMS developer quickstart](/docs/messaging/quickstart) shows you how to respond to a message and generate TwiML in Ruby with Sinatra.

And - that's all there is to it; receiving and responding is exactly the same as you would do in any SMS app with our Messaging API. Cool, right?

> \[!NOTE]
>
> Although these Quickstarts show you how to receive an SMS message, the webhook that Twilio will send will include the [same parameters](/docs/messaging/twiml#twilios-request-to-your-application) as an incoming SMS message, with the exception that **To** and **From** addresses will be set to the WhatsApp number receiving the message (`whatsapp:<E.164 formatted Twilio phone number associated with your business>`) and the WhatsApp number sending the message (`whatsapp:<User's E.164 phone number>`), respectively.

## What's next for WhatsApp and Ruby?

Because the Twilio WhatsApp API is essentially the same as the [Twilio Programmable Messaging API](/docs/messaging), all of the documentation for that API applies to your apps sending and receiving messages with WhatsApp. To dive deeper with a WhatsApp integration, see the [WhatsApp documentation overview](/docs/whatsapp) as well as the [API Reference](/docs/whatsapp/api).

Here are some areas you might like to explore next.

* [Send appointment reminders](/docs/messaging/tutorials/appointment-reminders)
* [Create SMS conversations](/docs/messaging/tutorials/how-to-create-sms-conversations)
* [The Programmable Messaging API overview](/docs/messaging/api)

Let's build some amazing WhatsApp integrations.
