Copy ‘n’ Paste Code Snippets

The Personalization API is easy to implement in a variety of languages. The code snippets below use the libraries on our GitHub Page to query our API and output the results. For more details, please consult each library's accompanying README docs.

Ruby | Python | PHP | Java | Perl | C# 

Ruby

This snippit requires the rapleaf_api gem (gem install rapleaf_api).

require 'rapleaf_api'
begin
  api = RapleafApi::Api.new("API_KEY") # Set API key here
  hash = api.query_by_email("test@example.com")
  puts hash.inspect
rescue Exception => e
  puts e.message
end

back to top

Python

This snippit requires the rapleafApi python module, which depends on the urllib3 module. (easy_install urllib3 rapleafApi).

from rapleafApi import RapleafApi
api = RapleafApi.RapleafApi('API_KEY')
try:
  response = api.query_by_email('test@example.com')
  for k, v in response.iteritems():
    print '%s = %s' % (k, v)
except Exception as e:
  print e

back to top

PHP

This snippit requires the RapleafApi library for PHP5. Make sure to set your API key at the top of RapleafApi.php. (Not on PHP5? Check out our devkit for older versions of PHP.)

<?php
  namespace Rapleaf;
  include "RapleafApi.php";
  $api = new RapleafApi(YOUR_RAPLEAF_API_KEY);
  try {
    $response = $api -> query_by_email('test@example.com', 
                                       $hash_email = true);
    foreach ($response as $key => $value) {
     echo $key . " = " . $value . "n";
    }
  } catch (Exception $e) {
    echo 'Caught exception: ' .  $e->getMessage() . "n";
  }
?>

back to top

Java

This snippit relies on the rapleaf-api-complete jar, which you can find in Rapleaf's Java devkit on GitHub.

import org.json.JSONObject;
import com.rapleaf.api.personalization.RapleafApi;

public class Test {
  public static void main(String[] args) {
    RapleafApi api = new RapleafApi("API_KEY"); // Set API key here
    try {
      JSONObject response = api.queryByEmail("test@example.com", 
                                             true);
      System.out.println(response);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

back to top

Perl

This snippit requires the RapleafApi library for Perl, which depends on the JSON module (perl -MCPAN -e 'install JSON'). Make sure to set your API key at the top of RapleafApi.pl.

do 'RapleafApi.pl';
eval {
  my $response = query_by_email('test@example.com');
  while(my ($k, $v) = each %$response) {
      print "$k = $v.n";
  }
};
if ($@) {
  print $@
}

back to top

C♯

This snippit relies on the rapleafApi dll, which you can find in Rapleaf's C♯ devkit on GitHub.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using personalization;

namespace MyApplication {
  class RapleafExample {
    public static void Main(string[] args) {
      RapleafApi api = new RapleafApi("SET_ME"); // Set API key here
      try {
        Dictionary response = api.queryByEmail("test@example.com",
                                               true);
        foreach (KeyValuePair kvp in response) {
          Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
        }
      }
      catch (System.Net.WebException e) {
        Console.WriteLine(e.Message);
      }
    }
  }
}

back to top

1,000 free age and gender queries. Get Your API Key