7 Simple REST Client Examples for Retrieving API Data

You’ve installed DreamFactory and connected to your database, and executed a few test API calls in the API Docs interface. So what’s next? Fortunately DreamFactory has already taken care of autogenerating your API endpoints so you can skip right past that and proceed to creating a client which will talk to the DreamFactory-managed API.

In this article we will demonstrate seven simple REST client examples involving sending a GET request to an HTTP-based API using an API key for authentication. We will present examples demonstrating NodeJS, Python, Ruby, PHP, and Perl. Even if you’re not a DreamFactory user (you should check it out, our OSS version is available for free download) there’s still plenty to learn from this post!

Configuring the Sample Project

If you’d like to follow along with these examples using live data, we suggest configuring the Address Book for JavaScript sample application. If you haven’t already installed this application within your DreamFactory instance, click on the Apps tab. On the left side of the screen click Import and Select next to the Address Book for JavaScript project. Scroll to the bottom of the screen and click the Import button. This is enough to ensure the application example data set is loaded into your DreamFactory instance, however if you want to experiment with other features you’ll also want to complete a few other configuration steps found in the aforementioned project README.

In either case, head over to API docs, choose the db service, and scroll down to the following endpoint:

GET /db/_schema

You should see the output depicted in the following screenshot:

Viewing API Docs

If the GET request executes successfully you should receive a response Code of 200 indicating success, as well as a JSON representation of the tables in your schema. Now that some sample data is available let’s experiment with a few GET requests. In our examples we will be querying the contact_info table using the following API endpoint:

GET http://localhost/api/v2/db/_table/contact_info

NodeJS REST API Example

For our first example we will look at two simple NodeJS scripts. Below is an example of a native NodeJS HTTP GET request. In your favorite text editor create a new file called rest.js and enter the following code:

const https = require("https");

const options = {
    "method": "GET",
    "hostname": "example.com",
    "port": 443,
    "path": "/api/v2/db/_table/contact_info",
    "headers": {
        "x-dreamfactory-api-key": "YOUR_API_KEY",
        "cache-control": "no-cache"

const req = https.request(options, function(res) {

    var chunks = [];

    res.on("data", function (chunk) {

    res.on("end", function() {
        var body = Buffer.concat(chunks);



After updating the hostname and x-dreamfactory-api-key fields to reflect the location and assigned application API key associated with your DreamFactory instance, save the changes and run the script via your terminal:

$ node rest.js

A large block of JSON data should be returned. If you’d like to clean it up and look a little more closely at your data you can copy the response and paste it into JSONLint. After pasting the data into the text area, click Validate JSON. This will accomplish two important tasks:

  1. Format your JSON into something a little more human readable.
  2. Validate that the JSON is in correct format as described by the JSON spec. If for any reason the JSON isn’t valid you will be given a hint as to what steps to take to correctly format your JSON.

That script felt a little heavy handed for just one simple GET call. Let’s try the same call again but this time we will use the ‘unirest’ module. NPM comes with NodeJS and allows users to install what are called Node modules. If you plan on doing any custom scripted services or events scripting in your DreamFactory this will also be useful. Let’s install the ‘unirest’ module using NPM, open up a shell and type:

$ npm install -g unirest

The -g option installs the module globally. We recommend installing NodeJS modules globally, especially if you intend to use them in your server-side scripts. Now that you’ve installed the unirest module open up your text editor and paste in the following code:

var unirest = require("unirest")

var req = unirest("GET", "https://example.com/api/v2/db/_table/contact_info")

        "cache-control": "no-cache",
        "x-dreamfactory-api-key": "YOUR_API_KEY"

req.end(function (res) {
        if (res.error) throw new Error(res.error)

Save the script as resty.js and run it like so:

$ node resty.js

As with before, you’ll receive a lengthy JSON response in return. However this time it was accomplished with a lot less code with the added bonus of formatted JSON!

Python REST API Example

Maybe NodeJS isn’t your cup of tea, and instead prefer Python. Python makes REST a cakewalk with the requests module. Let’s install the requests module using pip,the Python package manager. Open up your terminal and execute:

$ pip install requests

Now that the requests module is installed let’s again open up a text editor and enter the following code:

import requests

url = "https://example.com/api/v2/db/_table/contact_info?limit=5"

headers = {
        "cache-control": "no-cache",
        "x-dreamfactory-api-key": "YOUR_API_KEY"

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


Once you’ve confirmed everything is correct save the file as rest.py and again navigate to the directory where you saved your script and enter:

$ python rest.py
{"resource":[{"id":1,"ordinal":0,"contact_id":1,"info_type":"home","phone":"500 555-0162","email":"[email protected]","address":"3761 N. 14th St","city":"MEDINA","state":"ND","zip":"58467","country":"USA"},{"id":2,"ordinal":0,"contact_id":1,"info_type":"work","phone":"500 555-0110","email":"[email protected]","address":"2243 W St.","city":"MEDINA","state":"ND","zip":"58467","country":"USA"},...]}

As with the other examples, if everything is configured properly you’ll see the JSON returned in the script output.


Still other readers may wish to send HTTP API requests using PHP. No problem! Return to your editor and enter the following code:


$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => "https://example.com/api/v2/db/_table/contact_info?limit=5",

curl_setopt($curl, CURLOPT_HTTPHEADER, [
        "cache-control: no-cache",
        "X-DreamFactory-API-Key: YOUR_API_KEY"

$response = curl_exec($curl);
$error = curl_error($curl);


if ($error) {
        echo "cURL error #:" . $error;
} else {
        echo $response;

Save the file as rest.php and run the script like so:

$ php rest.php

Ruby REST API Example

Ruby has more HTTP client libraries then I can count on my fingers and toes. In this section we will cover two. Let’s first make a call using net/http which is built into the Ruby standard library. Open your text editor and paste in the following code:

require 'uri'
require 'net/http'

url = URI('https://example.com/api/v2/db/_table/contact_info?limit=5')

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)

request['X-DreamFactory-API-Key'] = 'YOUR_API_KEY'
request['cache-control'] = 'no-cache'

response = http.request(request)

puts response.read_body

Save these changes and execute the script like so:

$ ruby rest.rb

Now let’s try this again, however this time we are going to use the Ruby httparty gem. This incredibly useful Ruby gem which claims to “make http fun again” can be used interactively in the command line or within your Ruby, Sinatra and Ruby on Rails applications. Lets use Ruby’s package manager to install the httparty gem:

$ gem install httparty

Once you’ve installed the httparty gem open up your editor and paste the following code into a file called rest2.rb:

require 'httparty'
require 'json'

url = 'https://example.com/api/v2/db/_table/contact_info?limit=5'

headers = {
    "X-DreamFactory-API-Key" => "YOUR_API_KEY",
    "cache-control" => "no-cache"

response = HTTParty.get(url, :headers => headers)

json = JSON.parse(response.body)

puts response.body
puts response.message.to_json

Save the file and execute it like so:

$ ruby rest2.rb

Perl REST API Example

Let’s conclude this post with a Perl example. Begin by using Perl’s CPAN package manager to install the REST::Client Perl module. Open up your terminal and enter the following command:

$ cpan

Once in the cpan shell execute this command:

install REST::Client

After REST::Client finishes installing exit the cpan shell, open your editor, and paste in the following code:

use REST::Client; my $client = REST::Client->new(); $client->getUseragent()->ssl_opts(verify_hostname => 0); $client->getUseragent()->ssl_opts(SSL_verify_mode => SSL_VERIFY_NONE); $client->addHeader(‘X-DreamFactory-API-Key’, ‘YOUR_API_KEY’); $client->addHeader(‘cache-control’, ‘no-cache’); $client->GET(“https://example.com/api/v2/db/_table/contact_info?limit=56”); print $client->responseContent();

Save the file and run it using the following command:

$ perl rest.pl


Boom! So there you have it, 7 quick and easy scripts to GET and parse data from the DreamFactory REST API. Something else you may have noticed is that the API endpoint was used over and over again in each of the client calls without requiring a single line of server-side code, awesome!

If there is a topic you’d like us to cover feel free to tweet us @dfsoftwareinc and let us know! We would love to hear from you!

Get started with DreamFactory commercial edition by starting a free on-premise trial. Or, start a hosted trial now!