# Send a message with Facebook Messenger (public beta)

> \[!IMPORTANT]
>
> Facebook Messenger is available as a Public Beta product, and the information contained in this document is subject to change.
>
> Some features aren't yet implemented, and others may change before the product becomes Generally Available (GA). Public Beta products aren't covered by a Service Level Agreement (SLA).

On this page, you'll learn to use [Twilio Programmable Messaging](https://www.twilio.com/en-us/messaging/channels/sms) to programmatically send messages with Facebook Messenger using your web application or [cURL](https://curl.se/).

## Review considerations

* Facebook users must initiate contact with you by messaging your Facebook Page before you can contact them. Once you receive a user's message, you can respond back to the user for 24 hours. To learn more, see [Platform Policy Overview (Facebook for Developers)](https://developers.facebook.com/docs/messenger-platform/policy/policy-overview#standard_messaging).

  If you send a message after the 24-hour window has elapsed, Twilio tags messages with `ACCOUNT_UPDATE`.
* You're responsible for managing consumer opt-ins and opt-outs to the Facebook Messenger platform. Users grant you permission to message them by initiating the conversation with your Facebook Page. When users request that you stop messaging, you must stop messaging them. Users also have the option to block your page.
* You must strictly adhere to Facebook's commerce and business policies and provide high-quality experiences in Messenger. During or after the interaction in Messenger, users can provide negative feedback to Facebook when a page's conversations are spammy, abusive, or unpleasant. Maintain high quality, value-adding conversations that align with the user's intentions when engaging with you. If a business shows a pattern of violating Facebook's policies, Facebook disables the page from using Facebook Messenger.

## Complete the prerequisites

Before you send a message with Facebook Messenger, [complete the Facebook Messenger setup](/docs/messaging/channels/facebook-messenger).

## Send a message with Facebook Messenger

You can send messages with Facebook Messenger using code that makes `HTTP POST` requests to the [Message resource](https://www.twilio.com/docs/messaging/api/message-resource) in the Twilio REST API.

To send a message with Facebook Messenger, follow the steps to [send an SMS message](/docs/messaging/tutorials/how-to-send-sms-messages#send-an-sms-message), replacing the `from` field with `messenger:<facebook_page_id>` and `to` field with `messenger:<messenger_user_id>` as shown in the following example.

To find your Facebook Page ID:

1. Open the [Channels page in the Twilio Console](https://www.twilio.com/console/channels).
2. Click **Facebook Messenger**.
3. Check the **Page ID** field.

To find your recipient's Messenger User ID, check your callback URL. When a user sends a message to your Facebook Page, your callback URL receives the message with the same parameters as a standard Twilio SMS message.

To test your Facebook Messenger sender, you can send yourself a message by replacing the `to` field with `m.me/<page_id>`.

Respond Using the Programmable Messaging API

```js
// Download the helper library from https://www.twilio.com/docs/node/install
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = twilio(accountSid, authToken);

async function createMessage() {
  const message = await client.messages.create({
    body: "Would you like to play a game?",
    from: "messenger:{facebook_page_id}",
    to: "messenger:{messenger_user_id}",
  });

  console.log(message.body);
}

createMessage();
```

```python
# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client

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

message = client.messages.create(
    to="messenger:{messenger_user_id}",
    from_="messenger:{facebook_page_id}",
    body="Would you like to play a game?",
)

print(message.body)
```

```csharp
// Install the C# / .NET helper library from twilio.com/docs/csharp/install

using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using System.Threading.Tasks;

class Program {
    public static async Task Main(string[] args) {
        // Find your Account SID and Auth Token at twilio.com/console
        // and set the environment variables. See http://twil.io/secure
        string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
        string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");

        TwilioClient.Init(accountSid, authToken);

        var message = await MessageResource.CreateAsync(
            to: new Twilio.Types.PhoneNumber("messenger:{messenger_user_id}"),
            from: new Twilio.Types.PhoneNumber("messenger:{facebook_page_id}"),
            body: "Would you like to play a game?");

        Console.WriteLine(message.Body);
    }
}
```

```java
// Install the Java helper library from twilio.com/docs/java/install

import com.twilio.type.PhoneNumber;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;

public class Example {
    // Find your Account SID and Auth Token at twilio.com/console
    // and set the environment variables. See http://twil.io/secure
    public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
    public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");

    public static void main(String[] args) {
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
        Message message = Message
                              .creator(new com.twilio.type.PhoneNumber("messenger:{messenger_user_id}"),
                                  new com.twilio.type.PhoneNumber("messenger:{facebook_page_id}"),
                                  "Would you like to play a game?")
                              .create();

        System.out.println(message.getBody());
    }
}
```

```go
// Download the helper library from https://www.twilio.com/docs/go/install
package main

import (
	"fmt"
	"github.com/twilio/twilio-go"
	api "github.com/twilio/twilio-go/rest/api/v2010"
	"os"
)

func main() {
	// Find your Account SID and Auth Token at twilio.com/console
	// and set the environment variables. See http://twil.io/secure
	// Make sure TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN exists in your environment
	client := twilio.NewRestClient()

	params := &api.CreateMessageParams{}
	params.SetTo("messenger:{messenger_user_id}")
	params.SetFrom("messenger:{facebook_page_id}")
	params.SetBody("Would you like to play a game?")

	resp, err := client.Api.CreateMessage(params)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	} else {
		if resp.Body != nil {
			fmt.Println(*resp.Body)
		} else {
			fmt.Println(resp.Body)
		}
	}
}
```

```php
<?php

// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once "/path/to/vendor/autoload.php";

use Twilio\Rest\Client;

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);

$message = $twilio->messages->create(
    "messenger:{messenger_user_id}", // To
    [
        "from" => "messenger:{facebook_page_id}",
        "body" => "Would you like to play a game?",
    ]
);

print $message->body;
```

```ruby
# Download the helper library from https://www.twilio.com/docs/ruby/install
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: 'messenger:{messenger_user_id}',
            from: 'messenger:{facebook_page_id}',
            body: 'Would you like to play a game?'
          )

puts message.body
```

```bash
# Install the twilio-cli from https://twil.io/cli

twilio api:core:messages:create \
   --to messenger:{messenger_user_id} \
   --from messenger:{facebook_page_id} \
   --body "Would you like to play a game?"
```

```bash
curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json" \
--data-urlencode "To=messenger:{messenger_user_id}" \
--data-urlencode "From=messenger:{facebook_page_id}" \
--data-urlencode "Body=Would you like to play a game?" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
```
