Code Examples for sending a single or bulk SMS
All examples use JSON requests. Pick your preferred language.
With OkHttp 3
- Maven
- Gradle
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.12.13</version>
</dependency>
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
String json = "{\n" +
" \"method\": \"SendSms\",\n" +
" \"userdata\": {\n" +
" \"username\": \"api_username\",\n" +
" \"password\": \"api_key\"\n" +
" },\n" +
" \"msgdata\": [\n" +
" {\n" +
" \"number\": \"256700111222\",\n" +
" \"message\": \"Hello World!\",\n" +
" \"senderid\": \"MyCompany\",\n" +
" \"priority\": 0\n" +
" },\n" +
" {\n" +
" \"number\": \"256700333444\",\n" +
" \"message\": \"Hello World 2!\",\n" +
" \"senderid\": \"MyCompany\",\n" +
" \"priority\": 0\n" +
" }\n" +
" ]\n" +
"}";
RequestBody body = RequestBody.create(mediaType, json);
Request request = new Request.Builder()
.url("https://comms.egosms.co/api/v1/json/")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();
Response response = client.newCall(request).execute();
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"method": "SendSms",
"userdata": {
"username": "api_username",
"password": "api_key"
},
"msgdata": [
{
"number": "256700111222",
"message": "Hello World!",
"senderid": "MyCompany",
"priority": 0
},
{
"number": "256700333444",
"message": "Hello World 2!",
"senderid": "MyCompany",
"priority": 0
}
]
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://comms.egosms.co/api/v1/json/", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
curl --location --request POST 'https://comms.egosms.co/api/v1/json/' \
--header 'Content-Type: application/json' \
--data-raw '{
"method":"SendSms",
"userdata":{
"username":"api_username",
"password":"api_key"
},
"msgdata":[
{
"number":"256700111222",
"message":"Hello World!",
"senderid":"MyCompany",
"priority":0
},
{
"number":"256700333444",
"message":"Hello World 2!",
"senderid":"MyCompany",
"priority":0
}
]
}'
Add the reqwest and serde_json crates
Cargo.toml
[dependencies]
reqwest = { version = "0.12", features = ["json", "blocking"] }
serde_json = "1.0"
use reqwest::blocking::Client;
use serde_json::json;
fn main() -> Result<(), reqwest::Error> {
let client = Client::new();
let body = json!({
"method": "SendSms",
"userdata": {
"username": "api_username",
"password": "api_key"
},
"msgdata": [
{
"number": "256700111222",
"message": "Hello World!",
"senderid": "MyCompany",
"priority": 0
},
{
"number": "256700333444",
"message": "Hello World 2!",
"senderid": "MyCompany",
"priority": 0
}
]
});
let response = client
.post("https://comms.egosms.co/api/v1/json/")
.header("Content-Type", "application/json")
.json(&body)
.send()?;
println!("{}", response.text()?);
Ok(())
}
Install the RestSharp package using dotnet cli
dotnet add package RestSharp
var client = new RestClient("https://comms.egosms.co/api/v1/json/");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
var body = @"{
""method"": ""SendSms"",
""userdata"": {
""username"": ""api_username"",
""password"": ""api_key""
},
""msgdata"": [
{
""number"": ""256700111222"",
""message"": ""Hello World!"",
""senderid"": ""MyCompany"",
""priority"": 0
},
{
""number"": ""256700333444"",
""message"": ""Hello World 2!"",
""senderid"": ""MyCompany"",
""priority"": 0
}
]
}";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
require "uri"
require "json"
require "net/http"
url = URI("https://comms.egosms.co/api/v1/json/")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = "application/json"
request.body = JSON.dump({
"method": "SendSms",
"userdata": {
"username": "api_username",
"password": "api_key"
},
"msgdata": [
{
"number": "256700111222",
"message": "Hello World!",
"senderid": "MyCompany",
"priority": 0
},
{
"number": "256700333444",
"message": "Hello World 2!",
"senderid": "MyCompany",
"priority": 0
}
]
})
response = https.request(request)
puts response.read_body
Add guzzle package
composer require guzzlehttp/guzzle
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client();
$headers = [
'Content-Type' => 'application/json'
];
$body = '{
"method": "SendSms",
"userdata": {
"username": "api_username",
"password": "api_key"
},
"msgdata": [
{
"number": "256700111222",
"message": "Hello World!",
"senderid": "MyCompany",
"priority": 0
},
{
"number": "256700333444",
"message": "Hello World 2!",
"senderid": "MyCompany",
"priority": 0
}
]
}';
$request = new Request('POST', 'https://comms.egosms.co/api/v1/json/', $headers, $body);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();
Ensure you have the http dependency
pubspec.yaml
dependencies:
http: ^1.1.0 # Use the latest version
dart pub get
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() async {
var headers = {
'Content-Type': 'application/json'
};
var request = http.Request('POST', Uri.parse('https://comms.egosms.co/api/v1/json/'));
request.body = json.encode({
"method": "SendSms",
"userdata": {
"username": "api_username",
"password": "api_key"
},
"msgdata": [
{
"number": "256700111222",
"message": "Hello World!",
"senderid": "MyCompany",
"priority": 0
},
{
"number": "256700333444",
"message": "Hello World 2!",
"senderid": "MyCompany",
"priority": 0
}
]
});
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
print(await response.stream.bytesToString());
} else {
print(response.reasonPhrase);
}
}
package main
import (
"bytes"
"fmt"
"io"
"net/http"
)
func main() {
url := "https://comms.egosms.co/api/v1/json/"
jsonPayload := `{
"method": "SendSms",
"userdata": {
"username": "api_username",
"password": "api_key"
},
"msgdata": [
{
"number": "256700111222",
"message": "Hello World!",
"senderid": "MyCompany",
"priority": 0
},
{
"number": "256700333444",
"message": "Hello World 2!",
"senderid": "MyCompany",
"priority": 0
}
]
}`
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(jsonPayload)))
if err != nil {
fmt.Println("Error creating request:", err)
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return
}
fmt.Println(string(body))
}
Install libcurl development package
- Ubuntu/Debian -
sudo apt install libcurl4-openssl-dev - CentOS/RHEL/Fedora -
sudo yum install libcurl-develorsudo dnf install libcurl-devel - macOS (Homebrew) -
brew install curl - Windows - Download pre-built binaries from curl.se/windows or use vcpkg:
vcpkg install curl
#include <stdio.h>
#include <curl/curl.h>
// Callback function to write response data to stdout
static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t total_size = size * nmemb;
fwrite(contents, 1, total_size, stdout);
return total_size;
}
int main(void) {
CURL *curl;
CURLcode res;
struct curl_slist *headers = NULL;
// Initialize global libcurl (optional but recommended)
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
// Set URL
curl_easy_setopt(curl, CURLOPT_URL, "https://comms.egosms.co/api/v1/json/");
// Set POST method (CURLOPT_CUSTOMREQUEST is not needed for POST, use CURLOPT_POST)
curl_easy_setopt(curl, CURLOPT_POST, 1L);
// Follow redirects
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
// Set headers
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Set request body
const char *data =
"{"
" \"method\": \"SendSms\","
" \"userdata\": {"
" \"username\": \"api_username\","
" \"password\": \"api_key\""
" },"
" \"msgdata\": ["
" {"
" \"number\": \"256700111222\","
" \"message\": \"Hello World!\","
" \"senderid\": \"MyCompany\","
" \"priority\": 0"
" },"
" {"
" \"number\": \"256700333444\","
" \"message\": \"Hello World 2!\","
" \"senderid\": \"MyCompany\","
" \"priority\": 0"
" }"
" ]"
"}";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(data)); // Explicit length
// Set callback to capture response
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
// Perform the request
res = curl_easy_perform(curl);
// Check for errors
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
// Cleanup
curl_easy_cleanup(curl);
curl_slist_free_all(headers); // Free the headers list
} else {
fprintf(stderr, "Failed to initialize curl\n");
}
curl_global_cleanup(); // Clean up global state
return 0;
}