SMS API

Vonage's SMS API enables you to send and receive text messages to and from users worldwide, using our REST APIs.

  • Programmatically send and receive high volumes of SMS globally.
  • Send SMS with low latency and high delivery rates.
  • Receive SMS using local numbers.
  • Scale your applications with familiar web technologies.
  • Pay only for what you use, nothing more.
  • Auto-redact feature to protect privacy.

Contents

This topic contains the following information:

Getting Started

Send an SMS

This example shows you how to send an SMS to your chosen number.

First, sign up for a Vonage account if you don't already have one, and make a note of your API key and secret on the dashboard getting started page.

Replace the following placeholder values in the sample code:

Key Description
VONAGE_API_KEY Your Vonage API key.
VONAGE_API_SECRET Your Vonage API secret.

Write the code

Add the following to send-sms.sh:

curl -X "POST" "https://rest.nexmo.com/sms/json" \
  -d "from=$VONAGE_BRAND_NAME" \
  -d "text=A text message sent using the Vonage SMS API" \
  -d "to=$TO_NUMBER" \
  -d "api_key=$VONAGE_API_KEY" \
  -d "api_secret=$VONAGE_API_SECRET"

View full source

Run your code

Save this file to your machine and run it:

sh send-sms.sh

Prerequisites

npm install @vonage/server-sdk

Create a file named send.js and add the following code:

const Vonage = require('@vonage/server-sdk')

const vonage = new Vonage({
  apiKey: VONAGE_API_KEY,
  apiSecret: VONAGE_API_SECRET
})

View full source

Write the code

Add the following to send.js:

const from = VONAGE_BRAND_NAME
const to = TO_NUMBER
const text = 'A text message sent using the Vonage SMS API'

vonage.message.sendSms(from, to, text, (err, responseData) => {
    if (err) {
        console.log(err);
    } else {
        if(responseData.messages[0]['status'] === "0") {
            console.log("Message sent successfully.");
        } else {
            console.log(`Message failed with error: ${responseData.messages[0]['error-text']}`);
        }
    }
})

View full source

Run your code

Save this file to your machine and run it:

node send.js

Prerequisites

Add the following to `build.gradle`:

compile 'com.vonage:client:5.5.0'

Create a class named SendMessage and add the following code to the main method:

VonageClient client = VonageClient.builder().apiKey(VONAGE_API_KEY).apiSecret(VONAGE_API_SECRET).build();

View full source

Write the code

Add the following to the main method of the SendMessage class:

TextMessage message = new TextMessage(VONAGE_BRAND_NAME,
        TO_NUMBER,
        "A text message sent using the Vonage SMS API"
);

SmsSubmissionResponse response = client.getSmsClient().submitMessage(message);

if (response.getMessages().get(0).getStatus() == MessageStatus.OK) {
    System.out.println("Message sent successfully.");
} else {
    System.out.println("Message failed with error: " + response.getMessages().get(0).getErrorText());
}

View full source

Run your code

We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:

  apply plugin: 'application'
  mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Run the following gradle command to execute your application, replacing com.vonage.quickstart.sms with the package containing SendMessage:

gradle run -Pmain=com.vonage.quickstart.sms.SendMessage

Prerequisites

Install-Package Vonage

Create a file named SendSms.cs and add the following code:

using Vonage;
using Vonage.Request;

View full source

Add the following to SendSms.cs:

var credentials = Credentials.FromApiKeyAndSecret(
    VONAGE_API_KEY,
    VONAGE_API_SECRET
    );

var VonageClient = new VonageClient(credentials);

View full source

Write the code

Add the following to SendSms.cs:

var response = VonageClient.SmsClient.SendAnSms(new Vonage.Messaging.SendSmsRequest()
{
    To = TO_NUMBER,
    From = VONAGE_BRAND_NAME,
    Text = "A text message sent using the Vonage SMS API"
});

View full source

Prerequisites

composer require vonage/client

Create a file named send-sms.php and add the following code:

$basic  = new \Vonage\Client\Credentials\Basic(VONAGE_API_KEY, VONAGE_API_SECRET);
$client = new \Vonage\Client($basic);

View full source

Write the code

Add the following to send-sms.php:

$response = $client->sms()->send(
    new \Vonage\SMS\Message\SMS(TO_NUMBER, BRAND_NAME, 'A text message sent using the Nexmo SMS API')
);

$message = $response->current();

if ($message->getStatus() == 0) {
    echo "The message was sent successfully\n";
} else {
    echo "The message failed with status: " . $message->getStatus() . "\n";
}

View full source

Run your code

Save this file to your machine and run it:

php send-sms.php

Prerequisites

pip install vonage

Create a file named send-an-sms.py and add the following code:

client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
sms = vonage.Sms(client)

View full source

Write the code

Add the following to send-an-sms.py:

responseData = sms.send_message(
    {
        "from": VONAGE_BRAND_NAME,
        "to": TO_NUMBER,
        "text": "A text message sent using the Nexmo SMS API",
    }
)

if responseData["messages"][0]["status"] == "0":
    print("Message sent successfully.")
else:
    print(f"Message failed with error: {responseData['messages'][0]['error-text']}")

View full source

Run your code

Save this file to your machine and run it:

python send-an-sms.py

Prerequisites

gem install vonage

Create a file named send.rb and add the following code:

client = Vonage::Client.new(
  api_key: VONAGE_API_KEY,
  api_secret: VONAGE_API_SECRET
)

View full source

Write the code

Add the following to send.rb:

client.sms.send(
  from: VONAGE_BRAND_NAME,
  to: TO_NUMBER,
  text: 'A text message sent using the Vonage SMS API'
)

View full source

Run your code

Save this file to your machine and run it:

ruby send.rb

Troubleshooting

If you have problems when making API calls be sure to check the returned status field for specific error codes.

Concepts

Before using the Vonage SMS API, familiarize yourself with the following:

  • Number format - The SMS API requires phone numbers in E.164 format.

  • Authentication - The SMS API authenticates using your account API key and secret.

  • Webhooks - The SMS API makes HTTP requests to your application web server so that you can act upon them. For example: inbound SMS and delivery receipts.

Guides

Code Snippets

Use Cases

Reference