REST API
This page contains simple examples for the Mobile Gateway REST API
Curl
Send an SMS message using the cURL utility
curl -v \
-H 'Content-Type: application/json' \
-u '<USERNAME>:<PASSWORD>' \
-d '{"content": "Hello world!", "destination": "+64510123456"}' \
https://api.sms.optus.com.au/rest/gateway/messages
Python
Send an SMS message using the requests module
import requests
import json
from base64 import b64encode
uri = 'https://api.sms.optus.com.au/rest/gateway/messages'
username = "gw_username"
password = "abcde12345"
# Authorization token
def basic_auth(username, password):
token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii")
return f'Basic {token}'
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization' : basic_auth(username, password)
}
json_payload = json.dumps({
"destination": "+64211234567",
"content": "Hello world!"
})
response = requests.post(uri, headers=headers, data=json_payload)
if response.status_code == 201:
print(response.json())
else:
print("Error:", response.status_code, response.text)
Output:
[message_id]
Get an SMS message using the requests module
import requests
uri = 'https://api.sms.optus.com.au/rest/gateway/messages/message_id'
response = requests.get(uri, auth=('user', 'password'))
print 'HTTP %s' % response.status_code
print response.text
Output:
HTTP 200
{
"id": message_id,
"content": "Hello world!",
"source": "+64211234567",
"destination": "+64211234567",
}
PHP
Send an SMS message using the cURL extension
<?php
$uri = "https://api.sms.optus.com.au/rest/gateway/messages";
$json_payload ='{"content": "Hello world!", "destination": "+64211234567"}';
$request = curl_init($uri);
curl_setopt($request, CURLOPT_POST, True);
curl_setopt($request, CURLOPT_USERPWD, "user:password");
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($request, CURLOPT_POSTFIELDS, $json_payload);
$response = curl_exec($request);
$http_status = curl_getinfo($request, CURLINFO_HTTP_CODE);
echo "HTTP ".$http_status."\n".$response."\n";
curl_close($request);
?>
Output:
HTTP 201
[message_id]
Get an SMS message using the cURL extension
<?php
$uri = "https://api.sms.optus.com.au/rest/gateway/messages/message_id";
$request = curl_init($uri);
curl_setopt($request, CURLOPT_USERPWD, "user:password");
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($request);
$http_status = curl_getinfo($request, CURLINFO_HTTP_CODE);
echo "HTTP ".$http_status."\n".$response."\n";
curl_close($request);
?>
Output:
HTTP 200
{
"id": <message_id>,
"content": "Hello world!",
"source": "+6412345678",
"destination": "+6412345678",
}
Ruby
Send an SMS message using the Net::HTTP library
require 'net/http'
uri = URI.parse('https://api.sms.optus.com.au/rest/gateway/messages')
json_payload = '{"content": "Hello world!", "destination": "+64211234567"}'
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.basic_auth('username', 'password')
request.body = json_payload
response = http.request(request)
puts 'HTTP %s' % response.code
puts response.body
Output:
HTTP 201
[message_id]
Get an SMS message using the Net::HTTP library
require 'net/http'
uri = URI.parse('https://api.sms.optus.com.au/rest/gateway/messages/message_id')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth('username', 'password')
response = http.request(request)
puts 'HTTP %s' % response.code
puts response.body
Output:
HTTP 200
{
"id": message_id,
"content": "Hello world!",
"source": "+64211234567",
"destination": "+64211234567",
}
Perl
Send an SMS message using the LWP library
use strict;
use warnings;
use JSON;
use LWP::UserAgent;
#
# Alter these to your supplied credentials
#
my $username = 'username';
my $password = 'password';
my $message = { content => 'Hello world!',
destination => '+64510123456' };
my $json_payload = to_json($message);
my $URI = 'https://api.sms.optus.com.au/rest/gateway/messages';
my $request = HTTP::Request->new('POST', $URI);
$request->authorization_basic($username, $password);
$request->content($json_payload);
my $ua = LWP::UserAgent->new();
my $response = $ua->request($request);
printf("HTTP %s\n", $response->code);
print $response->content . "\n";
Output:
HTTP 201
[message_id]
Get an SMS message using the LWP library
use LWP::UserAgent;
my $uri = 'https://api.sms.optus.com.au/rest/gateway/messages/message_id';
my $request = HTTP::Request->new('GET', $uri);
$request->authorization_basic('username', 'password');
my $ua = LWP::UserAgent->new(ssl_opts => {verify_hostname => 0});
my $response = $ua->request($request);
printf("HTTP %s\n", $response->code);
print $response->content . "\n;
Output:
HTTP 200
{
"id": message_id,
"content": "Hello world!",
"source": "+64211234567",
"destination": "+64211234567",
}