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.

Note: To include a new line in the message, see the documentation for Concatenation and Encoding

Delivery receipts

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

Authentication type used Webhook used for delivery receipt
Basic Auth account-level webhook is used for delivery receipt.
JWT Auth application-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 $'{
    "to": "'${MESSAGES_TO_NUMBER}'",
    "from": "'${SMS_SENDER_ID}'",
    "channel": "sms",
    "message_type": "text",
    "text": "This is an SMS sent using the Vonage Messages API."
  }'

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.