Need help? Call Live Support at +31 (0) 38 453 07 59

The provided code samples demonstrate how to build a GET and a POST request and send it to Realtime Register. They do not provide a complete implementation of the API. For a complete overview of all available API calls look at the menu on the left. These samples come as-is and are only meant as an example and are not to be used as production code. (I.e.: no error checking)

APIExample.php

Class demonstrating REST calls in PHP

<?php

const API_KEY = "...";

class RestClient {
    public $conn_handle;

    private function do_request($method, $uri, $json=NULL) {
        if (!isset($conn_handle)) {
            $this->conn_handle = curl_init();
        }

        $options = [
            CURLOPT_URL => "https://api.yoursrs.com/v2/" . $uri,
            CURLOPT_CUSTOMREQUEST => $method,
            CURLOPT_POSTFIELDS => $json,
            CURLOPT_HTTPAUTH => CURLAUTH_ANY,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_HTTPHEADER => [
                'Content-Type: application/json',
                'Authorization: ApiKey ' . API_KEY,
                'Content-Length: ' . strlen($json)
            ]
        ];

        curl_setopt_array($this->conn_handle, $options);

        return json_decode(curl_exec($this->conn_handle), 1);
    }

    public function get_domains() {
        return $this->do_request("GET", "domains/");
    }

    public function register_domain($domain_name, $registrant, $admin) {
        $body = [
            'customer' => 'test',
            'period' => 12,
            'registrant' => $registrant,
            'contacts' => [
                ['handle' => $admin, 'role' => 'ADMIN'],
                ['handle' => 'test', 'role' => 'BILLING'],
                ['handle' => 'test', 'role' => 'TECH'],
            ]
        ];
        return $this->do_request("POST", "domains/" . $domain_name, json_encode($body));
    }
}

$client = new RestClient;
print_r($client->register_domain("testdomain.com", "registrant", "admin"));
print_r($client->get_domains());

APIExample.py

Class demonstrating REST calls in Python

import requests

API_KEY = '...'


class RestClient(object):
    def do_request(self, method, url, **kwars):
        headers = {
            'Authorization': 'ApiKey %s' % API_KEY
        }
        return requests.request(method, 'https://api.yoursrs.com/v2/%s' % url, headers=headers, **kwars).json()

    def get_domains(self):
        result = self.do_request('get', 'domains')

        list_domains = []

        for domain in result['entities']:
            list_domains.append(domain['domainName'])

        return list_domains

    def register_domain(self, domain_name, registrant, admin):
        body = {
            "customer": "test",
            "period": 12,
            "registrant": registrant,
            "contacts": [
                {"handle": admin, "role": "ADMIN"},
                {"handle": "test", "role": "TECH"},
                {"handle": "test", "role": "BILLING"}
            ]
        }
        result = self.do_request('post', 'domains/' + domain_name, json=body)
        return result

client = RestClient()
client.register_domain("testdomain.com", "registrant", "admin")
retval = client.get_domains()
print(retval)

APIExample.java

Class demonstrating REST calls in Java

package com.yoursrs;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Test;


public class RestClient {
    private String apiKey = "...";

    private JSONObject sendRequest(final String resource, final String method, final Map<?, ?> body)
            throws IOException, InterruptedException {
        HttpRequest.Builder request = HttpRequest.newBuilder()
                .uri(URI.create("https://api.yoursrs.com/v2/" + resource))
                .header("Content-Type", "application/json; UTF-8")
                .header("Authorization", "ApiKey " + this.apiKey);
        if (body != null) {
            request.method(method, HttpRequest.BodyPublishers.ofString(new JSONObject(body).toString()));
        } else {
            request.method(method, HttpRequest.BodyPublishers.noBody());
        }

        HttpResponse<String> response = HttpClient.newHttpClient()
                .send(request.build(), HttpResponse.BodyHandlers.ofString());

        if (response.statusCode() < 200 || response.statusCode() > 299) {
            throw new IOException("Response code :" + response.statusCode());
        }

        return new JSONObject(new JSONTokener(response.body()));
    }

    public void registerDomain(String domainName, String registrant, String admin) throws IOException, InterruptedException {
        Map<String, Object> body = Map.of(
                "customer", "test",
                "period", 12,
                "registrant", registrant,
                "contacts", List.of(
                    Map.of("role", "ADMIN",
                            "handle", admin),
                    Map.of("role", "BILLING",
                            "handle", "test"),
                    Map.of("role", "TECH",
                            "handle", "test")));
        sendRequest("domains/" + domainName, "POST", body);
    }

    public List<String> getDomains() throws IOException, InterruptedException {
        JSONObject response = sendRequest("domains", "GET", null);
        JSONArray domains = response.getJSONArray("entities");

        return IntStream.range(0, domains.length())
                .mapToObj(i -> domains.getJSONObject(i).getString("domainName"))
                .collect(Collectors.toList());
    }

    @Test
    public void test() throws IOException, InterruptedException {
        registerDomain("testdomain.com", "registrant", "admin");
        List<String> domains = getDomains();
        System.out.println(String.join(", ", domains));
    }
}