Send an SMS

In this code snippet you will see how to send an SMS using the Messages API.

For a step-by-step guide to this topic, you can read our tutorial Sending SMS messages with the Messages API.

Delivery receipts

The webhook used for the delivery receipt will depend on the type of authentication used:

Authentication type usedWebhook used for delivery receipt
Basic Authaccount-level webhook is used for delivery receipt.
JWT Authapplication-level webhook is used for delivery receipt / status.

Reception of delivery receipts depends on availability of network support.

Example

Find the description for all variables used in each code snippet below:

KeyDescription
VONAGE_APPLICATION_ID

The Vonage Application ID.

VONAGE_APPLICATION_PRIVATE_KEY_PATH

Private key path.

VONAGE_PRIVATE_KEY_PATH

Private key path.

VONAGE_FROM_NUMBER

Refer to FROM_NUMBER above

VONAGE_BRAND_NAME

The alphanumeric string that represents the name or number of the organization sending the message.

FROM_NUMBER

The phone number you are sending the message from.

TO_NUMBER

The phone number you are sending the message to.

NOTE: Don't use a leading + or 00 when entering a phone number, start with the country code, for example 447700900000.

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

Write the code

Add the following to send-sms.sh:

curl -X POST https://api.nexmo.com/v1/messages \
  -H 'Authorization: Bearer '$JWT\
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -d $'{
          "message_type": "text",
          "text": "Nexmo Verification code: 12345. Valid for 10 minutes.",
          "to": "'$TO_NUMBER'",
          "from": "'$FROM_NUMBER'",
          "channel": "sms"
}'

View full source

Run your code

Save this file to your machine and run it:

bash send-sms.sh

Try it out

When you run the code a message is sent to the destination number.