Send a message with failover

In this example we will send a Facebook Messenger message that failsover to SMS. In the array you can order the messages in any order you want. Each message object must contain a failover object except for the last message as there is nothing to failover to.

Key Description
NEXMO_APPLICATION_ID The ID of the application that you created.
FROM_NUMBER The phone number you are sending the message from in E.164  format. For example 447700900000.
SENDER_ID Your sender ID. This value should be the to.id value you received in the inbound messenger event.
RECIPIENT_ID The recipient ID is the Facebook user you are messaging. This value should be the from.id value you received in the inbound messenger event. It is sometimes called the PSID.

Prerequisites

Generate a JWT

$ JWT="$(nexmo jwt:generate /path/to/private.key \application_id=NEXMO_APPLICATION_ID)"
$ echo $JWT

Example

curl -X POST https://api.nexmo.com/beta/workflows \
  -H 'Authorization: Bearer $JWT'\
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -d $'{
    "template":"failover",
    "workflow": [
      {
        "from": { "type": "messenger", "id": "SENDER_ID" },
        "to": { "type": "messenger", "id": "RECIPIENT_ID" },
        "message": {
          "content": {
            "type": "text",
            "text": "This is a Facebook Messenger Message sent from the Workflows API"
          }
        },
        "failover":{
          "expiry_time": 600,
          "condition_status": "read"
        }
      },
      {
        "from": {"type": "sms", "number": "FROM_NUMBER"},
        "to": { "type": "sms", "number": "TO_NUMBER"},
        "message": {
          "content": {
            "type": "text",
            "text": "This is an SMS sent from the Workflows API"
          }
        }
      }
    ]
  }'
http POST 'https://api.nexmo.com/beta/workflows' \
  'Authorization':'Bearer '$JWT\
  template="failover" \
  workflow:='[
    {
      "from": { "type": "messenger", "id": "SENDER_ID" },
      "to": { "type": "messenger", "id": "RECIPIENT_ID" },
      "message": {
        "content": {
          "type": "text",
          "text": "This is a Facebook Messenger message sent from the Workflows API"
        }
      },
      "failover": {
        "condition_status": "read",
        "expiry_time": 600
      },
    },
    {
      "to": { "type": "sms", "number": "TO_NUMBER" },
      "from": { "type": "sms", "number": "FROM_NUMBER" }
      "message": {
        "content": {
          "type": "text",
          "text": "This is an SMS sent from the Workflows API"
        }
      },
    }
  ]'
const Nexmo = require('nexmo')

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET,
  applicationId: NEXMO_APPLICATION_ID,
  privateKey: NEXMO_APPLICATION_PRIVATE_KEY_PATH
})

nexmo.workflow.create("failover", [
  {
    "from": { "type": "messenger", "id": "SENDER_ID" },
    "to": { "type": "messenger", "id": "RECIPIENT_ID" },
    "message": {
      "content": {
        "type": "text",
        "text": "This is a Facebook Messenger message sent from the Workflows API"
      }
    },
    "failover":{
      "expiry_time": 600,
      "condition_status": "read"
    }
  },
  {
    "from": {"type": "sms", "number": "FROM_NUMBER"},
    "to": { "type": "sms", "number": "TO_NUMBER"},
    "message": {
      "content": {
        "type": "text",
        "text": "This is an SMS sent from the Workflows API"
      }
    }
  },
  (err, data) => { console.log(data.workflow_uuid); }
])