Adding a New Line in SMS Using Vonage REST API Adding a New Line in SMS Using Vonage REST API

Adding a New Line in SMS Using Vonage REST API

Nexmo Support

Objective

Adding a new line or carriage return in SMS using the Vonage REST API.

Applies To

  • REST API
  • SMS

Procedure

A newline character (\n or LF) should be represented in your HTTP request with 1 character in UTF8: %0A

CRLF (Carriage Return Line Feed) should be represented with 2 characters in UTF8: %0D%0A

The following code snippets demonstrate how to add newline characters to SMS messages.

PHP

<?php
$url = 'https://rest.nexmo.com/sms/json?' . http_build_query(
    [
      'api_key' =>  'KEY',
      'api_secret' => 'SECRET',
      'to' => 'TO_NUMBER',
      'from' => 'FROM_NUMBER',
      'text' => "Here is a newline\nWow!"
    ]
);
​
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
​
echo $response;

Python

import urllib
import urllib2

params = {
    'api_key': 'KEY',
    'api_secret': 'SECRET',
    'to': 'TO_NUMBER',
    'from': 'FROM_NUMBER',
    'text': 'Here is a newline:\nWow!'
}

url = 'https://rest.nexmo.com/sms/json?' + urllib.urlencode(params)

request = urllib2.Request(url)
request.add_header('Accept', 'application/json')
response = urllib2.urlopen(request)

print response.read()