Sending an SMS

This endpoint allows you to start sending SMSs through MailerSend SMS API.

Send an SMS

This endpoint allows you to send an asynchronous SMS. It returns the status of the SMS sent with a X-SMS-Message-Id that can be used to continuously query for the status using the SMS API.

Send an SMS using this POST request:

POST https://api.mailersend.com/v1/sms

Request Body

{
    "from": "+19191234567",
    "to": [
      "+19191234567",
      "+19199876543"
    ],
    "text": "Hey {{name}}! This is just a friendly hello :D",
    "personalization": [
      {
        "phone_number": "+19191234567",
        "data": {
          "name": "Dummy"
        }
      },
      {
        "phone_number": "+19199876543",
        "data": {
          "name": "Not Dummy"
        }
      }
    ]
  }
use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SmsParams;

$mailersend = new MailerSend(['api_key' => 'key']);

$smsParams = (new SmsParams())
    ->setFrom('+12065550101')
    ->setTo(['+12065550102'])
    ->addRecipient('+12065550103')
    ->setText('Text');
    
$sms = $mailersend->sms->send($smsParams);

More examplesopen in new window

const MailerSend = require("mailersend");
const SmsParams  = require("mailersend").SmsParams;

const mailersend = new MailerSend({
  api_key: "key",
});

const recipients = [
  "+18332647501",
  "+18332647500",
];

const smsParams = new SmsParams()
      .setFrom("+18332647501")
      .setRecipients(recipients)
      .setText("This is the text content");

mailersend.sendSms(smsParams);

More examplesopen in new window

from mailersend import sms_sending

api_key = "API key here"

mailer = sms_sending.NewSmsSending(api_key)

# Number belonging to your account in E164 format
number_from = "+11234567890"

# You can add up to 50 recipient numbers
numbers_to = [
    "+11234567891",
    "+11234567892"
]
text = "This is the text content"

print(mailer.send_sms(number_from, numbers_to, text))

More examplesopen in new window

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/mailersend/mailersend-go"
)

var APIKey = "Api Key Here"

func main() {
	// Create an instance of the mailersend client
	ms := mailersend.NewMailersend(APIKey)

	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
	defer cancel()

	message := ms.Sms.NewMessage()
	message.SetFrom("your-number")
	message.SetTo([]string{"client-number"})
	message.SetText("This is the message content {{ var }}")

	personalization := []mailersend.SmsPersonalization{
		{
			PhoneNumber: "client-number",
			Data: map[string]interface{}{
				"var": "foo",
			},
		},
	}

	message.SetPersonalization(personalization)

	res, _ := ms.Sms.Send(context.TODO(), message)
	fmt.Printf(res.Header.Get("X-SMS-Message-Id"))
}

More examplesopen in new window

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;

public void sendSms() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        String messageId = ms.sms().builder().from("from phone number")
        .addRecipient("to phone number")
        .text("test sms {{name}}")
        .addPersonalization("to phone number", "name", "name personalization")
        .send();
        
        System.out.println(messageId);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

More examplesopen in new window

require "mailersend-ruby"

# Intialize the SMS class
ms_sms = Mailersend::SMS.new

# Add parameters
ms_sms.add_from('your-number')
ms_sms.add_to('client-number')
ms_sms.add_text('This is the message content')
personalization = {
  phone_number: 'client-number',
  data: {
    test: 'Test Value'
  }
}
ms_sms.add_personalization(personalization)

# Send the SMS
ms_sms.send

More examplesopen in new window

Request parameters

JSON parameters are provided in dot notation

JSON parameterTypeRequiredLimitationsDetails
fromstringyesNumber belonging to your account in E164 format
tostring[]yesmin:1,max:50
to.*stringyesUS, CAThe phone number(s) the message should be sent to in E164 format
textstringyesMax lenght:2048The contents of the text message. Must be 2048 characters or less.
If the body of your message is more than 160 GSM-7 characters or 70 UCS-2 characters, MailerSend will split the message into a segmented SMS and add a special header (User Data Headeropen in new window). Please note you are billed by segment.
The User Data Header (UDH) takes up 6 bytes and instructs the receiving device how to reassemble the segments so that your whole message will be shown as one SMS on the receiving handset. The maximum number of characters per concatenated (long) message is slightly reduced due to the inclusion of concatenation headers (UDH).
You can see the difference between GSM-7 and UCS-2 and how they're segmented hereopen in new window.
personalizationobject[]noAllows using personalization in {{ var }} syntax. Can be used in the text fields. Read more about advanced personalization.
personalization.*.phone_numberstringyesPhone number that personalization will be applied to.
personalization.*.dataobject[]yesObject with key: value pairs. Values will be added to your template using {{ key }} syntax.

Responses

Sending queued

Response Code: 202 Accepted
Response Headers:
	Content-Type: text/plain; charset=utf-8
	X-SMS-Message-Id: 5e42957d51f1d94a1070a733
Response Body: [EMPTY]

Sending paused

Response Code: 202 Accepted
Response Headers:
	Content-Type: text/plain; charset=utf-8
	X-SMS-Message-Id: 5e42957d51f1d94a1070a733
  X-SMS-Send-Paused: true
Response Body: [EMPTY]

Validation error

Response Code: 422 Unprocessable Entity
Response Headers:
	Content-Type: application/json
{
  "message": "The given data was invalid.",
  "errors": {
    "from": [
      "The from field contains an invalid number."
    ]
  }
}

See - Validations errors

Last Updated: