SMS Content

If you are sending an SMS, use the following parameters.

Parameter

Type

Description

name

string

Required for SMS Messages.

An identifier for tracking message within the OneSignal dashboard or export analytics.

Not shown to end user.

sms_from

string

Phone Number used to send SMS. Should be a registered Twilio phone number in E.164 format.

contents

object

Text content for the SMS

sms_media_urls

array_string

URLs for the media files to be attached to the SMS content.
Limit: 10 media urls with a total max. size of 5MBs.

Examples Send SMS

Send to specific Phone Numbers

params = {
  "app_id" => "5eb5a37e-b458-11",
  "name": "Identifier for SMS Message",
  "sms_from": "+15555555555",
  "contents" => { en: "Welcome to Cat Facts!", es: "Bienvenidos a Factos del Gato" },
  "sms_media_urls": ["https://cat.com/cat.jpg"],
  "include_phone_numbers": ["+1999999999"]}
}
uri = URI.parse('https://onesignal.com/api/v1/notifications')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.path,
                              'Content-Type'  => 'application/json;charset=utf-8',
                              'Authorization' => "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5")
request.body = params.as_json.to_json
response = http.request(request)
puts response.body
import requests
import json
header = {"Content-Type": "application/json; charset=utf-8",
          "Authorization": "Basic NGEwMGZmMjItY2NkNy0xMWUzLT"}
payload = {
            "app_id": "5eb5a37e-b458-11",
            "name": "Identifier for SMS Message",
            "sms_from": "+15555555555",
            "contents": { "en": "Welcome to Cat Facts!", "es": "Bienvenidos a Factos del Gato" },
            "sms_media_urls": ["https://cat.com/cat.jpg"],
            "include_phone_numbers": ["+19999999999"]
          }
req = requests.post("https://onesignal.com/api/v1/notifications", headers=header, data=json.dumps(payload))
print(req.status_code, req.reason)
var sendNotification = function(data) {
  var headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic NGEwMGZmMjItY2NkNy0xMWj"
  };
  var options = {
    host: "onesignal.com",
    port: 443,
    path: "/api/v1/notifications",
    method: "POST",
    headers: headers
  };
  var https = require('https');
  var req = https.request(options, function(res) {  
    res.on('data', function(data) {
      console.log("Response:");
      console.log(JSON.parse(data));
    });
  });
  req.on('error', function(e) {
    console.log("ERROR:");
    console.log(e);
  });
  req.write(JSON.stringify(data));
  req.end();
};
var message = {
  "app_id": "5eb5a37e-b458-11",
  "name": "Identifier for SMS Message",
  "sms_from": "+15555555555",
  "contents" => { en: "Welcome to Cat Facts!", es: "Bienvenidos a Factos del Gato" },
  "sms_media_urls": ["https://cat.com/cat.jpg"],
  "include_phone_numbers": ["+19999999999"]
};
sendNotification(message);

Send to a specific segment or all subscribers

params = {
  "app_id" => "5eb5a37e-b458-11",
  "name" => "Identifier for SMS Message",
  "sms_from" => "+15555555555",
  "contents" => { en: "Welcome to Cat Facts!", es: "Bienvenidos a Factos del Gato" },
  "sms_media_urls" => ["https://cat.com/cat.jpg"],
  "included_segments" => ["Subscribed Users"]
}
uri = URI.parse('https://onesignal.com/api/v1/notifications')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.path,
                              'Content-Type'  => 'application/json;charset=utf-8',
                              'Authorization' => "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5")
request.body = params.as_json.to_json
response = http.request(request)
puts response.body
import requests
import json
header = {"Content-Type": "application/json; charset=utf-8",
          "Authorization": "Basic NGEwMGZmMjItY2NkNy0xMWUzLT"}
payload = {
            "app_id": "5eb5a37e-b458-11",
            "name": "Identifier for SMS Message",
            "sms_from": "+15555555555",
            "contents": { "en": "Welcome to Cat Facts!", "es": "Bienvenidos a Factos del Gato" },
            "sms_media_urls": ["https://cat.com/cat.jpg"],
            "included_segments": ["Subscribed Users"]
          }
req = requests.post("https://onesignal.com/api/v1/notifications", headers=header, data=json.dumps(payload))
print(req.status_code, req.reason)
var sendNotification = function(data) {
  var headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic NGEwMGZmMjItY2NkNy0xMWj"
  };
  var options = {
    host: "onesignal.com",
    port: 443,
    path: "/api/v1/notifications",
    method: "POST",
    headers: headers
  };
  var https = require('https');
  var req = https.request(options, function(res) {  
    res.on('data', function(data) {
      console.log("Response:");
      console.log(JSON.parse(data));
    });
  });
  req.on('error', function(e) {
    console.log("ERROR:");
    console.log(e);
  });
  req.write(JSON.stringify(data));
  req.end();
};
var message = {
  "app_id": "5eb5a37e-b458-11",
  "name": "Identifier for SMS Message",
  "sms_from": "+15555555555",
  "contents": { en: "Welcome to Cat Facts!", es: "Bienvenidos a Factos del Gato" },
  "sms_media_urls": ["https://cat.com/cat.jpg"],
  "included_segments": ["Subscribed Users"]
};
sendNotification(message);

Send based on filters

params = {
  "app_id" => "5eb5a37e-b458-11",
  "name" => "Identifier for SMS Message",
  "sms_from" => "+15555555555",
  "contents" => { en: "Welcome to Cat Facts!", es: "Bienvenidos a Factos del Gato" },
  "sms_media_urls" => ["https://cat.com/cat.jpg"],
  "filters" => [{"field": "tag", "key": "level", "relation": ">", "value": "10"},{"operator": "OR"},{"field": "amount_spent", "relation": ">","value": "0"}]
}
uri = URI.parse('https://onesignal.com/api/v1/notifications')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.path,
                              'Content-Type'  => 'application/json;charset=utf-8',
                              'Authorization' => "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5")
request.body = params.as_json.to_json
response = http.request(request)
puts response.body
import requests
import json
header = {"Content-Type": "application/json; charset=utf-8",
          "Authorization": "Basic NGEwMGZmMjItY2NkNy0xMWUzLT"}
payload = {
            "app_id": "5eb5a37e-b458-11",
            "name": "Identifier for SMS Message",
            "sms_from": "+15555555555",
            "contents": { "en": "Welcome to Cat Facts!", "es": "Bienvenidos a Factos del Gato" },
            "sms_media_urls": ["https://cat.com/cat.jpg"],
            "filters": [{"field": "tag", "key": "level", "relation": ">", "value": "10"},{"operator": "OR"},{"field": "amount_spent", "relation": ">","value": "0"}]
          }
req = requests.post("https://onesignal.com/api/v1/notifications", headers=header, data=json.dumps(payload))
print(req.status_code, req.reason)
var sendNotification = function(data) {
  var headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic NGEwMGZmMjItY2NkNy0xMWj"
  };
  var options = {
    host: "onesignal.com",
    port: 443,
    path: "/api/v1/notifications",
    method: "POST",
    headers: headers
  };
  var https = require('https');
  var req = https.request(options, function(res) {  
    res.on('data', function(data) {
      console.log("Response:");
      console.log(JSON.parse(data));
    });
  });
  req.on('error', function(e) {
    console.log("ERROR:");
    console.log(e);
  });
  req.write(JSON.stringify(data));
  req.end();
};
var message = {
  "app_id": "5eb5a37e-b458-11",
  "name": "Identifier for SMS Message",
  "sms_from": "+15555555555",
  "contents": { en: "Welcome to Cat Facts!", es: "Bienvenidos a Factos del Gato" },
  "sms_media_urls": ["https://cat.com/cat.jpg"],
  "filters": [{"field": "tag", "key": "level", "relation": ">", "value": "10"},{"operator": "OR"},{"field": "amount_spent", "relation": ">","value": "0"}]
};
sendNotification(message);

Send based on External User IDs

params = {
  "app_id" => "5eb5a37e-b458-11",
  "name" => "Identifier for SMS Message",
  "sms_from" => "+15555555555",
  "contents" => { en: "Welcome to Cat Facts!", es: "Bienvenidos a Factos del Gato" },
  "sms_media_urls" => ["https://cat.com/cat.jpg"],
  "include_external_user_ids" => ["6392d91a-b206-4b7b-a620-cd68e32c3a76","76ece62b-bcfe-468c-8a78-839aeaa8c5fa","8e0f21fa-9a5a-4ae7-a9a6-ca1f24294b86"] 
}
uri = URI.parse('https://onesignal.com/api/v1/notifications')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.path,
                              'Content-Type'  => 'application/json;charset=utf-8',
                              'Authorization' => "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5")
request.body = params.as_json.to_json
response = http.request(request)
puts response.body
import requests
import json
header = {"Content-Type": "application/json; charset=utf-8",
          "Authorization": "Basic NGEwMGZmMjItY2NkNy0xMWUzLT"}
payload = {
            "app_id": "5eb5a37e-b458-11",
            "name": "Identifier for SMS Message",
            "sms_from": "+15555555555",
            "contents": { "en": "Welcome to Cat Facts!", "es": "Bienvenidos a Factos del Gato" },
            "sms_media_urls": ["https://cat.com/cat.jpg"],
            "include_external_user_ids" => ["6392d91a-b206-4b7b-a620-cd68e32c3a76","76ece62b-bcfe-468c-8a78-839aeaa8c5fa","8e0f21fa-9a5a-4ae7-a9a6-ca1f24294b86"]
          }
req = requests.post("https://onesignal.com/api/v1/notifications", headers=header, data=json.dumps(payload))
print(req.status_code, req.reason)
var sendNotification = function(data) {
  var headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic NGEwMGZmMjItY2NkNy0xMWj"
  };
  var options = {
    host: "onesignal.com",
    port: 443,
    path: "/api/v1/notifications",
    method: "POST",
    headers: headers
  };
  var https = require('https');
  var req = https.request(options, function(res) {  
    res.on('data', function(data) {
      console.log("Response:");
      console.log(JSON.parse(data));
    });
  });
  req.on('error', function(e) {
    console.log("ERROR:");
    console.log(e);
  });
  req.write(JSON.stringify(data));
  req.end();
};
var message = {
  "app_id": "5eb5a37e-b458-11",
  "name": "Identifier for SMS Message",
  "sms_from": "+15555555555",
  "contents": { en: "Welcome to Cat Facts!", es: "Bienvenidos a Factos del Gato" },
  "sms_media_urls": ["https://cat.com/cat.jpg"],
  "include_external_user_ids": ["6392d91a-b206-4b7b-a620-cd68e32c3a76","76ece62b-bcfe-468c-8a78-839aeaa8c5fa","8e0f21fa-9a5a-4ae7-a9a6-ca1f24294b86"]
};
sendNotification(message);