Code examples for REST API

In "Developer mode" inside a database collection, you can quickly get "ready-to-run" code for your restdb.io database endpoint in the following languages: cURL, Javascript, Python, PHP, Java, C#, Objective-C and Swift (see example below).

API Docs

If you just want some generic examples right now, you can select your language below and copy code for the HTTP REST method you want to use.

Code examples in popular languages

GET all documents from the motorbikes collection
curl -k -i -H "Content-Type: application/json"\
-H "x-apikey: 560bd47058e7ab1b2648f4e7"\
-X GET 'https://inventory-fac4.restdb.io/rest/motorbikes'
GET one record by ID
curl -k -i -H "Content-Type: application/json"\
-H "x-apikey: 560bd47058e7ab1b2648f4e7"\
-X GET 'https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7'
GET records by query
curl -k -i -H "Content-Type: application/json"\
-H "x-apikey: 560bd47058e7ab1b2648f4e7"\
-G 'https://inventory-fac4.restdb.io/rest/motorbikes' -d 'q={"field":"value"}'
POST a new record to collection
curl -k -H "Content-Type: application/json"\
-H "x-apikey: 560bd47058e7ab1b2648f4e7"\
-X POST -d '{"field1":"xyz","field2":"xyz"}' 'https://inventory-fac4.restdb.io/rest/motorbikes'
PUT an updated record to collection by ID
curl -k -H "Content-Type: application/json"\
-H "x-apikey: 560bd47058e7ab1b2648f4e7"\
-X PUT -d '{"field2":"xyz"}' 'https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7'
DELETE a record by ID
curl -k -H "Content-Type: application/json"\
-H "x-apikey: 560bd47058e7ab1b2648f4e7"\
-X DELETE 'https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7'
JavaScript using jQuery AJAX

GET all documents from the motorbikes collection
var settings = {
    "async": true,
    "crossDomain": true,
    "url": "https://inventory-fac4.restdb.io/rest/motorbikes",
    "method": "GET",
    "headers": {
      "content-type": "application/json",
      "x-apikey": "560bd47058e7ab1b2648f4e7",
      "cache-control": "no-cache"
    }
}
                  
$.ajax(settings).done(function (response) {
    console.log(response);
});
                    
POST a new document to the motorbikes collection
var jsondata = {"field1": "xyz","field2": "abc"};
var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://inventory-fac4.restdb.io/rest/motorbikes",
  "method": "POST",
  "headers": {
    "content-type": "application/json",
    "x-apikey": "560bd47058e7ab1b2648f4e7",
    "cache-control": "no-cache"
  },
  "processData": false,
  "data": JSON.stringify(jsondata)
}

$.ajax(settings).done(function (response) {
  console.log(response);
});
                    
PUT a updated document to the motorbikes collection
var jsondata = {"field1": "new value","field2": "xxx"};
var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7",
  "method": "PUT",
  "headers": {
    "content-type": "application/json",
    "x-apikey": "560bd47058e7ab1b2648f4e7",
    "cache-control": "no-cache"
  },
  "processData": false,
  "data": JSON.stringify(jsondata)
}

$.ajax(settings).done(function (response) {
  console.log(response);
});
                    
DELETE a document from the motorbikes collection
var settings = {
    "async": true,
    "crossDomain": true,
    "url": "https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7",
    "method": "DELETE",
    "headers": {
    "content-type": "application/json",
    "x-apikey": "560bd47058e7ab1b2648f4e7",
    "cache-control": "no-cache"
    }
}

$.ajax(settings).done(function (response) {
    console.log(response);
});
JavaScript using XHR AJAX

GET all documents from the motorbikes collection
var data = null;

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
    if (this.readyState === 4) {
    console.log(this.responseText);
    }
});

xhr.open("GET", "https://inventory-fac4.restdb.io/rest/motorbikes");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("x-apikey", "560bd47058e7ab1b2648f4e7");
xhr.setRequestHeader("cache-control", "no-cache");

xhr.send(data);
                    
POST a new document to the motorbikes collection
var data = JSON.stringify({
    "field1": "xyz",
    "field2": "xyz"
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
    if (this.readyState === 4) {
    console.log(this.responseText);
    }
});

xhr.open("POST", "https://inventory-fac4.restdb.io/rest/motorbikes");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("x-apikey", "560bd47058e7ab1b2648f4e7");
xhr.setRequestHeader("cache-control", "no-cache");

xhr.send(data);
                    
PUT a updated document to the motorbikes collection
var data = JSON.stringify({
    "field2": "new value"
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
    if (this.readyState === 4) {
    console.log(this.responseText);
    }
});

xhr.open("PUT", "https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("x-apikey", "560bd47058e7ab1b2648f4e7");
xhr.setRequestHeader("cache-control", "no-cache");

xhr.send(data);
                    
DELETE a document from the motorbikes collection
var data = null;
                    
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
    if (this.readyState === 4) {
    console.log(this.responseText);
    }
});

xhr.open("DELETE", "https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("x-apikey", "560bd47058e7ab1b2648f4e7");
xhr.setRequestHeader("cache-control", "no-cache");

xhr.send(data);
                    
NodeJS using the request library
GET all documents from the motorbikes collection
var request = require("request");

var options = {
    method: 'GET',
    url: 'https://inventory-fac4.restdb.io/appdeploy/motorbikes',
    headers: 
    {   'cache-control': 'no-cache',
        'x-apikey': '560bd47058e7ab1b2648f4e7' 
    } 
};

request(options, function (error, response, body) {
    if (error) throw new Error(error);
    console.log(body);
});
                    
POST a new document to the motorbikes collection
var request = require("request");

var options = { 
    method: 'POST',
    url: 'https://inventory-fac4.restdb.io/rest/motorbikes',
    headers: 
    {   'cache-control': 'no-cache',
        'x-apikey': '560bd47058e7ab1b2648f4e7',
        'content-type': 'application/json' },
    body: { field1: 'xyz', field2: 'abc' },
    json: true 
};

request(options, function (error, response, body) {
    if (error) throw new Error(error);
    console.log(body);
});

                    
PUT an updated document to the motorbikes collection
var request = require("request");

var options = { 
    method: 'PUT',
    url: 'https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7',
    headers: 
    {   'cache-control': 'no-cache',
        'x-apikey': '560bd47058e7ab1b2648f4e7',
        'content-type': 'application/json' },
    body: { field2: 'new value' },
    json: true 
};

request(options, function (error, response, body) {
    if (error) throw new Error(error);
    console.log(body);
});

                    
DELETE a document from the motorbikes collection
var request = require("request");

var options = { 
    method: 'DELETE',
    url: 'https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7',
    headers: 
    {   'cache-control': 'no-cache',
        'x-apikey': '560bd47058e7ab1b2648f4e7',
        'content-type': 'application/json' 
    }
};

request(options, function (error, response, body) {
    if (error) throw new Error(error);
    console.log(body);
});
Java using Unirest
GET all documents from the motorbikes collection
HttpResponse response = Unirest.get("https://inventory-fac4.restdb.io/appdeploy/motorbikes")
.header("x-apikey", "560bd47058e7ab1b2648f4e7")
.header("cache-control", "no-cache")
.asString();
                    
POST a new document to the motorbikes collection
HttpResponse response = Unirest.post("https://inventory-fac4.restdb.io/rest/motorbikes")
.header("content-type", "application/json")
.header("x-apikey", "560bd47058e7ab1b2648f4e7")
.header("cache-control", "no-cache")
.body("{\"field1\":\"xyz\",\"field2\":\"abc\"}")
.asString();
                    
PUT an updated document to the motorbikes collection
HttpResponse response = Unirest.put("https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7")
.header("content-type", "application/json")
.header("x-apikey", "560bd47058e7ab1b2648f4e7")
.header("cache-control", "no-cache")
.body("{\"field2\":\"new value\"}")
.asString();
                    
DELETE a document from the motorbikes collection
HttpResponse response = Unirest.delete("https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7")
.header("content-type", "application/json")
.header("x-apikey", "560bd47058e7ab1b2648f4e7")
.header("cache-control", "no-cache")
.asString();
Pythod using request
GET all documents from the motorbikes collection
import requests

url = "https://inventory-fac4.restdb.io/rest/motorbikes"

headers = {
    'content-type': "application/json",
    'x-apikey': "560bd47058e7ab1b2648f4e7",
    'cache-control': "no-cache"
    }

response = requests.request("GET", url, headers=headers)

print(response.text)
                    
POST a new document to the motorbikes collection
import requests
import json

url = "https://inventory-fac4.restdb.io/rest/motorbikes"

payload = json.dumps( {"field1": "xyz","field2": "abc"} )
headers = {
    'content-type': "application/json",
    'x-apikey': "560bd47058e7ab1b2648f4e7",
    'cache-control': "no-cache"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)
                    
PUT an updated document to the motorbikes collection
import requests

url = "https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7"

payload = "{\"field2\":\"new value\"}"
headers = {
    'content-type': "application/json",
    'x-apikey': "560bd47058e7ab1b2648f4e7",
    'cache-control': "no-cache"
    }

response = requests.request("PUT", url, data=payload, headers=headers)

print(response.text)
                    
DELETE a document from the motorbikes collection
import requests

url = "https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7"

headers = {
    'content-type': "application/json",
    'x-apikey': "560bd47058e7ab1b2648f4e7",
    'cache-control': "no-cache"
    }

response = requests.request("DELETE", url, headers=headers)

print(response.text)
                    
PHP using HttpRequest
GET all documents from the motorbikes collection
setUrl('https://inventory-fac4.restdb.io/rest/motorbikes');
$request->setMethod(HTTP_METH_GET);

$request->setHeaders(array(
'cache-control' => 'no-cache',
'x-apikey' => '560bd47058e7ab1b2648f4e7',
'content-type' => 'application/json'
));

try {
    $response = $request->send();
    echo $response->getBody();
} catch (HttpException $ex) {
    echo $ex;
}
                    
POST a new document to the motorbikes collection
setUrl('https://inventory-fac4.restdb.io/rest/motorbikes');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
'cache-control' => 'no-cache',
'x-apikey' => '560bd47058e7ab1b2648f4e7',
'content-type' => 'application/json'
));

$request->setBody('{"field1":"xyz","field2":"abc"}');

try {
    $response = $request->send();
    echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
                    
PUT an updated document to the motorbikes collection
setUrl('https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7');
$request->setMethod(HTTP_METH_PUT);

$request->setHeaders(array(
'cache-control' => 'no-cache',
'x-apikey' => '560bd47058e7ab1b2648f4e7',
'content-type' => 'application/json'
));

$request->setBody('{"field2":"new value"}');

try {
    $response = $request->send();
    echo $response->getBody();
} catch (HttpException $ex) {
    echo $ex;
}
                    
DELETE a document from the motorbikes collection
setUrl('https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7');
$request->setMethod(HTTP_METH_DELETE);

$request->setHeaders(array(
'cache-control' => 'no-cache',
'x-apikey' => '560bd47058e7ab1b2648f4e7',
'content-type' => 'application/json'
));

try {
    $response = $request->send();
    echo $response->getBody();
} catch (HttpException $ex) {
    echo $ex;
}
                    
C# using Resharpr
GET all documents from the motorbikes collection
var client = new RestClient("https://inventory-fac4.restdb.io/rest/motorbikes");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("x-apikey", "560bd47058e7ab1b2648f4e7");
request.AddHeader("content-type", "application/json");
IRestResponse response = client.Execute(request);
                    
POST a new document to the motorbikes collection
var client = new RestClient("https://inventory-fac4.restdb.io/rest/motorbikes");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("x-apikey", "560bd47058e7ab1b2648f4e7");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"field1\":\"xyz\",\"field2\":\"abc\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
                    
PUT an updated document to the motorbikes collection
var client = new RestClient("https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7");
var request = new RestRequest(Method.PUT);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("x-apikey", "560bd47058e7ab1b2648f4e7");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"field2\":\"new value\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
                    
DELETE a document from the motorbikes collection
var client = new RestClient("https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7");
var request = new RestRequest(Method.DELETE);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("x-apikey", "560bd47058e7ab1b2648f4e7");
request.AddHeader("content-type", "application/json");
IRestResponse response = client.Execute(request);
                    
Objective-C using NSURL
GET all documents from the motorbikes collection
#import 

NSDictionary *headers = @{ @"content-type": @"application/json",
                        @"x-apikey": @"560bd47058e7ab1b2648f4e7",
                        @"cache-control": @"no-cache";

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://inventory-fac4.restdb.io/rest/motorbikes"]
                                                    cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                    timeoutInterval:10.0];
[request setHTTPMethod:@"GET"];
[request setAllHTTPHeaderFields:headers];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask =   [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];
                    
POST a new document to the motorbikes collection
#import 

NSDictionary *headers = @{ @"content-type": @"application/json",
                        @"x-apikey": @"560bd47058e7ab1b2648f4e7",
                        @"cache-control": @"no-cache" };
NSDictionary *parameters = @{ @"field1": @"xyz",
                            @"field2": @"abc" };

NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://inventory-fac4.restdb.io/rest/motorbikes"]
                                                    cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                    timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask =   [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];
                    
PUT an updated document to the motorbikes collection
#import 

NSDictionary *headers = @{ @"content-type": @"application/json",
                        @"x-apikey": @"560bd47058e7ab1b2648f4e7",
                        @"cache-control": @"no-cache" };
NSDictionary *parameters = @{ @"field2": @"new value" };

NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https:///inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7"]
                                                    cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                    timeoutInterval:10.0];
[request setHTTPMethod:@"PUT"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask =   [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];
                    
DELETE a document from the motorbikes collection
#import 

NSDictionary *headers = @{ @"content-type": @"application/json",
                        @"x-apikey": @"560bd47058e7ab1b2648f4e7",
                        @"cache-control": @"no-cache" };

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7"]
                                                    cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                    timeoutInterval:10.0];
[request setHTTPMethod:@"DELETE"];
[request setAllHTTPHeaderFields:headers];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask =   [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];
                    
Swift using NSURL
GET all documents from the motorbikes collection
import Foundation

let headers = [
"content-type": "application/json",
"x-apikey": "560bd47058e7ab1b2648f4e7",
"cache-control": "no-cache"
]

let request = NSMutableURLRequest(url: NSURL(string: "https://inventory-fac4.restdb.io/rest/motorbikes")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                        timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
    if (error != nil) {
        print(error)
    } else {
        let httpResponse = response as? HTTPURLResponse
        print(httpResponse)
    }
})

dataTask.resume()
                    
POST a new document to the motorbikes collection
import Foundation

let headers = [
"content-type": "application/json",
"x-apikey": "560bd47058e7ab1b2648f4e7",
"cache-control": "no-cache"
]
let parameters = [
"field1": "xyz",
"field2": "abc"
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://inventory-fac4.restdb.io/rest/motorbikes")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                        timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
    if (error != nil) {
        print(error)
    } else {
        let httpResponse = response as? HTTPURLResponse
        print(httpResponse)
    }
})

dataTask.resume()
                    
PUT an updated document to the motorbikes collection
import Foundation

let headers = [
"content-type": "application/json",
"x-apikey": "560bd47058e7ab1b2648f4e7",
"cache-control": "no-cache"
]
let parameters = ["field2": "new value"] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https:///inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                        timeoutInterval: 10.0)
request.httpMethod = "PUT"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
    if (error != nil) {
        print(error)
    } else {
        let httpResponse = response as? HTTPURLResponse
        print(httpResponse)
    }
})

dataTask.resume()
                    
DELETE a document from the motorbikes collection
import Foundation

let headers = [
"content-type": "application/json",
"x-apikey": "560bd47058e7ab1b2648f4e7",
"cache-control": "no-cache"
]

let request = NSMutableURLRequest(url: NSURL(string: "https://inventory-fac4.restdb.io/rest/motorbikes/560bd66201e7ab1b2648f4e7")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                        timeoutInterval: 10.0)
request.httpMethod = "DELETE"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
    if (error != nil) {
        print(error)
    } else {
        let httpResponse = response as? HTTPURLResponse
        print(httpResponse)
    }
})

dataTask.resume()