OpenSRS API Quick Start Guide

Follow These Three Easy Steps

Getting started should be simple and straightforward. With this in mind, we've divided our Quick Start Guide into three easy steps and included code snippets for several of the major languages. We hope this will assist you in connecting to the OpenSRS system and allow you to begin selling domain and SSL services.

Step 1.

Go to Reseller Control Panel, and click on Account Settings. Under the gear icon, click on the API settings tab, and copy your API key.

Step 2.

(Required for the Live environment but not the test environment)
On the Account Settings page, scroll down to IP Access Rules and select the edit button. Here, you can add the IP address from which you will send API calls.

Step 3.

Plugin your API key and reseller username into one of the following code snippets:

#!/usr/bin/python

import requests
import hashlib

TEST_MODE = 1

connection_options = {
        'live' : {
         # IP whitelisting required
             'reseller_username': '<YOUR RESELLER USERNAME>',
             'api_key':'<YOUR API KEY>',
             'api_host_port': 'https://rr-n1-tor.opensrs.net:55443',
        },
        'test' : {
             # IP whitelisting not required
             'reseller_username': '<YOUR RESELLER USERNAME>',
             'api_key':'<YOUR API KEY>',
             'api_host_port': 'https://horizon.opensrs.net:55443',

        }
}

if TEST_MODE == 1:
    connection_details = connection_options['test']
else:
    connection_details = connection_options['live']

xml = '''
<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
<OPS_envelope>
<header>
    <version>0.9</version>
</header>
<body>
<data_block>
    <dt_assoc>
        <item key="protocol">XCP</item>
        <item key="action">LOOKUP</item>
        <item key="object">DOMAIN</item>
        <item key="attributes">
         <dt_assoc>
                <item key="domain">myfirstopensrsapitest.com</item>
         </dt_assoc>
        </item>
    </dt_assoc>
</data_block>
</body>
</OPS_envelope>
'''

md5_obj = hashlib.md5()
md5_obj.update((xml + connection_details['api_key']).encode())
signature = md5_obj.hexdigest()

md5_obj = hashlib.md5()
md5_obj.update((signature + connection_details['api_key']).encode())
signature = md5_obj.hexdigest()

headers = {
        'Content-Type':'text/xml',
        'X-Username': connection_details['reseller_username'],
        'X-Signature':signature,
};

print("Request to {} as reseller {}:".format(connection_details['api_host_port'],connection_details['reseller_username']))
print(xml)

r = requests.post(connection_details['api_host_port'], data = xml, headers=headers )

print("Response:")
if r.status_code == requests.codes.ok:
    print(r.text)
else:
    print (r.status_code)
    print (r.text)
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket::SSL;
use Digest::MD5 qw/md5_hex/;
use LWP::UserAgent;
use Data::Dumper;

my $TEST_MODE = 1;    # Will connect to OpenSRS's test environment

my $connection_options = {
    'live' => {

        # IP whitelisting required
        api_host_port => 'rr-n1-tor.opensrs.net:55443',
        api_key => '<YOUR API KEY>',
        reseller_username => '<YOUR RESELLER USERNAME>',
    },
    'test' => {

        # IP whitelisting not required
        api_host_port => 'horizon.opensrs.net:55443',
        api_key => '<YOUR API KEY>',
        reseller_username => '<YOUR RESELLER USERNAME>',
    },

};

my $connection_details;

if ($TEST_MODE) {
    $connection_details = $connection_options->{'test'};
}
else {
    $connection_details = $connection_options->{'live'};
}

my $ua = new LWP::UserAgent;

# Simple lookup command
my $xml = <<DATA;
<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
<OPS_envelope>
<header>
    <version>0.9</version>
</header>
<body>
<data_block>
    <dt_assoc>
        <item key="protocol">XCP</item>
        <item key="action">LOOKUP</item>
        <item key="object">DOMAIN</item>
        <item key="attributes">
         <dt_assoc>
                <item key="domain">myfirstopensrsapitest.com</item>
         </dt_assoc>
        </item>
    </dt_assoc>
</data_block>
</body>
</OPS_envelope>
DATA

my $response = $ua->post(
    "https://$connection_details->{api_host_port}",
    'Content-Type' => 'text/xml',
    'X-Username'   => $connection_details->{reseller_username},
    'X-Signature'  => md5_hex(
        md5_hex( $xml, $connection_details->{api_key} ),
        $connection_details->{api_key}
    ),
    'Content' => $xml
);

print "Request to $connection_details->{api_host_port} as reseller: $connection_details->{reseller_username}\n$xml\n";

print "Response:\n";
if ( $response->is_success ) {
    print Dumper( $response->content );
}
else {
    print Dumper($response);
}
import java.util.HashMap;
import java.io.*;
import java.security.MessageDigest;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;

public class OpenSRSAPITest {

    public static void main (String[] args) {

        final boolean TEST_MODE = true;

        HashMap <String, String> liveDetails = new HashMap <String,String>();
        liveDetails.put("apiHostPort","https://rr-n1-tor.opensrs.net:55443");
        liveDetails.put("apiKey", "<YOUR_API_KEY>");
        liveDetails.put("resellerUsername", "<YOUR_RESELLER_USERNAME>");

        HashMap <String, String> testDetails = new HashMap <String,String>();
        testDetails.put("apiHostPort", "https://horizon.opensrs.net:55443");
        testDetails.put("apiKey", "<YOUR_API_KEY>");
        testDetails.put("resellerUsername", "<YOUR_RESELLER_USERNAME>");

        HashMap <String, String> connectionDetails;

        if (TEST_MODE)
            connectionDetails = testDetails;
        else
            connectionDetails = liveDetails;

        // Simple lookup command
        String xml = "<?xml version='1.0' encoding='UTF-8' standalone='no' ?>" +
        "<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>" +
        "<OPS_envelope>" +
        "<header>" +
            "<version>0.9</version>" +
        "</header>" +
        "<body>" +
        "<data_block>" +
            "<dt_assoc>" +
                "<item key='protocol'>XCP</item>" +
                "<item key='action'>LOOKUP</item>" +
                "<item key='object'>DOMAIN</item>" +
                "<item key='attributes'>" +
                    "<dt_assoc>" +
                        "<item key='domain'>myfirstopensrsapitest.com</item>" +
                    "</dt_assoc>" +
                "</item>" +
            "</dt_assoc>" +
        "</data_block>" +
        "</body>" +
        "</OPS_envelope>";

// Create signature
        String signature = md5_hex( md5_hex(xml + connectionDetails.get("apiKey")) + connectionDetails.get("apiKey"));

        System.out.println("Request as reseller: " + connectionDetails.get("resellerUsername"));
        System.out.println(xml);

        // Make HTTP Post
        try {
            URL url = new URL(connectionDetails.get("apiHostPort"));
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");

            conn.setRequestProperty("Content-Type", "text/xml");
            conn.setRequestProperty("X-Username", connectionDetails.get("resellerUsername"));
            conn.setRequestProperty("X-Signature", signature);

            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
            writer.write(xml);
            writer.flush();

            System.out.println("Response Code: " + conn.getResponseCode());
            System.out.println("Response Message: " + conn.getResponseMessage());
            System.out.println("Response XML:");
            String line;
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = reader.readLine()) != null) {
              System.out.println(line);
            }
            writer.close();
            reader.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String md5_hex(String s) {
        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(s.getBytes(), 0, s.length());
            BigInteger i = new BigInteger(1,m.digest());
            return String.format("%1$032x", i);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
<?php

// Note: Requires cURL library
$TEST_MODE = true;

$connection_options = [
    'live' => [
        'api_host_port' => 'https://rr-n1-tor.opensrs.net:55443',
        'api_key' => '<YOUR API KEY>',
        'reseller_username' => '<YOUR RESELLER USERNAME>'
    ],
    'test' => [
        'api_host_port' => 'https://horizon.opensrs.net:55443',
        'api_key' => '<YOUR API KEY>',
        'reseller_username' => '<YOUR RESELLER USERNAME>'
    ]
];

if ($TEST_MODE) {
    $connection_details = $connection_options['test'];
} else {
    $connection_details = $connection_options['live'];
}

$xml = <<<EOD
<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
<OPS_envelope>
<header>
    <version>0.9</version>
</header>
<body>
<data_block>
    <dt_assoc>
        <item key="protocol">XCP</item>
        <item key="action">LOOKUP</item>
        <item key="object">DOMAIN</item>
        <item key="attributes">
         <dt_assoc>
                <item key="domain">myfirstopensrsapitest.com</item>
         </dt_assoc>
        </item>
    </dt_assoc>
</data_block>
</body>
</OPS_envelope> 
EOD;

$data = [
    'Content-Type:text/xml',
    'X-Username:' . $connection_details['reseller_username'],
    'X-Signature:' . md5(md5($xml . $connection_details['api_key']) .  $connection_details['api_key']),
];

$ch = curl_init($connection_details['api_host_port']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

$response = curl_exec($ch);

echo 'Request as reseller: ' . $connection_details['reseller_username'] . "\n" .  $xml . "\n";

echo "Response\n";
echo $response . "\n";
?>
#!/usr/bin/ruby

require 'uri'
require 'net/http'
require 'net/https'
require 'digest'

TEST_MODE = 1
connection_options = {
        'live' => {
         # IP whitelisting required
             'reseller_username' => '<YOUR RESELLER USERNAME>',
             'api_key' => '<YOUR API KEY>',
             'api_host_port' => 'https://rr-n1-tor.opensrs.net:55443',
        },
        'test' => {
             # IP whitelisting not required
             'reseller_username' => '<YOUR RESELLER USERNAME>',
             'api_key' => '<YOUR API KEY>',
             'api_host_port' => 'https://horizon.opensrs.net:55443',

        }
}

if TEST_MODE == 1
    connection_details = connection_options['test']
else
    connection_details = connection_options['live']
end

xml = <<-eos
<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
<OPS_envelope>
<header>
    <version>0.9</version>
</header>
<body>
<data_block>
    <dt_assoc>
        <item key="protocol">XCP</item>
        <item key="action">LOOKUP</item>
        <item key="object">DOMAIN</item>
        <item key="attributes">
         <dt_assoc>
                <item key="domain">myfirstopensrsapitest.com</item>
         </dt_assoc>
        </item>
    </dt_assoc>
</data_block>
</body>
</OPS_envelope>
eos

md5 = Digest::MD5.new
md5.update(xml + connection_details['api_key'])
signature = md5.hexdigest

md5 = Digest::MD5.new
md5.update(signature + connection_details['api_key'])
signature = md5.hexdigest

uri = URI.parse("#{connection_details['api_host_port']}/")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path, initheader = { 'Content-Type' =>'text/xml', 
	'X-Username' => connection_details['reseller_username'], 
	'X-Signature' => signature,
})

req.body = xml

puts "Request to #{connection_details['api_host_port']} as reseller #{connection_details['reseller_username']}:"
res = https.request(req)
puts xml

puts "Response:"
if res.code == "200"
	puts res.body
else
	puts res.code
	puts res.message
end