Sending an SMS

Sending an SMS message with Nexmo is straightforward. Sign up for an account and replace the following variables in the example below:

Key Description
TO_NUMBER The number you are sending the SMS to in E.164 format. For example 447700900000.
NEXMO_API_KEY You can find this in your account overview
NEXMO_API_SECRET You can find this in your account overview
curl -X "POST" "https://rest.nexmo.com/sms/json" \
  -d "from=Acme Inc" \
  -d "text=A text message sent using the Nexmo SMS API" \
  -d "to=TO_NUMBER" \
  -d "api_key=NEXMO_API_KEY" \
  -d "api_secret=NEXMO_API_SECRET"

Prerequisites

Install the Nexmo library
$ npm install nexmo
Initialize the library
const Nexmo = require('nexmo')

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET
})

Code

const from = 'Acme Inc'
const to = TO_NUMBER
const text = 'A text message sent using the Nexmo SMS API'

nexmo.message.sendSms(from, to, text)

Prerequisites

Install the Nexmo library

Add the following to your build.gradle file:

$ compile 'com.nexmo:client:3.1.0'
Initialize the library
AuthMethod auth = new TokenAuthMethod(NEXMO_API_KEY, NEXMO_API_SECRET);
NexmoClient client = new NexmoClient(auth);

Code

SmsClient client = new NexmoClient(auth).getSmsClient();

TextMessage exampleMessage = new TextMessage(
    "Acme Inc",
    TO_NUMBER,
    "A text message sent using the Nexmo SMS API"
);

Prerequisites

Install the Nexmo library via the NuGet Package Manager Console
Install-Package Nexmo.Csharp.Client
Initialize the library
var client = new Client(creds: new Nexmo.Api.Request.Credentials
{
    ApiKey = "NEXMO_API_KEY",
    ApiSecret = "NEXMO_API_SECRET"
});

Code

var results = client.SMS.Send(request: new SMS.SMSRequest
{
    from = "Acme Inc",
    to = TO_NUMBER,
    text = "A test SMS sent using the Nexmo SMS API"
});

Prerequisites

Install the Nexmo library
$ composer require nexmo/client
Initialize the library
$basic  = new \Nexmo\Client\Credentials\Basic(NEXMO_API_KEY, NEXMO_API_SECRET);
$client = new \Nexmo\Client($basic);

Code

$message = $client->message()->send([
    'to' => TO_NUMBER,
    'from' => 'Acme Inc',
    'text' => 'A text message sent using the Nexmo SMS API'
]);

Prerequisites

Install the Nexmo library
$ pip install nexmo
Initialize the library
import nexmo

client = nexmo.Client(key=NEXMO_API_KEY, secret=NEXMO_API_SECRET)

Code

client.send_message({
    'from': 'Acme Inc',
    'to': TO_NUMBER,
    'text': 'A text message sent using the Nexmo SMS API',
})

Prerequisites

Install the Nexmo library
$ gem install nexmo
Initialize the library
require 'nexmo'

client = Nexmo::Client.new(
  api_key: NEXMO_API_KEY,
  api_secret: NEXMO_API_SECRET
)

Code

client.sms.send(
  from: 'Acme Inc',
  to: RECIPIENT_NUMBER,
  text: 'A text message sent using the Nexmo SMS API'
)