Welcome to Nicereply API reference. It will give you an intro to Nicereply API and how to implement it into your project. Let's get started.
The acronym "API" stands for "Application Programming Interface". An API is just a defined way for a program to accomplish a task, usually retrieving or modifying data. In Nicereply's case, we provide an API method for just about every feature you can see on our website. Programmers use the Nicereply API to make applications, websites, widgets, and other projects that interact with Nicereply. Programs talk to the Nicereply API over HTTP, the same protocol that your browser uses to visit and interact with web pages.
You don't need to write all the code to talk to the Nicereply API from scratch, as our developers created some open source code that demonstrates interaction with Nicereply via it's API. You can try/download the example code at the bottom of this page.
API calls are avaiable at http://www.nicereply.com/api/. You can call the API function by GET or POST request. Attach any necessary parameters in standard way. Most common parameter is apikey. To make an API call of some specific function via GET request, you can use this URL:
http://www.nicereply.com/api/getUserRatingsCount?apikey=YOURAPIKEY&username=john%20doe
As you can see we attached name of the function to the basic URL, then attached apikey & username variables. Values of variable must be encoded by so-called URL encoding. Name of the API function is case insensitive, name of the parameters is case sensitive.
API response is in form of a JSON object. You either get requested value(s) or descriptive error message(s).
{error:"Invalid API key!"}
Now we provide valid API key and username:
{userRatingsCount:5}
It's always a good practice to check if the response object contains an error property.
Each Company signed up for Nicereply gets a pair of API keys. These are 40 chars long strings. One is signed as public, while the second is private. Public API key acts as gate to company's attributes which we considered perfectly safe for sharing publicly. This key is mainly designed to be used in Javascript files (which by the nature are publicly avaialble). With this key, users are able to retrieve things like company's average rating, number of ratings, week-by-week difference in average etc.
On the other hand, the private API key is your secret key to all advanced attributes of your Nicereply data. Aside from retrieving all your public methods with the private key, you can also retrieve individual ratings of agents, list of agents and their details, etc. Also, a new rating can be inserted with a private API key. IMPORTANT! Never include your private API key into Javascript files!. It is designed to be used on server-side scripts, such as PHP or Ruby files.
Nicereply's API contains pack of various functions. Theyr'e divided into two groups: getters & setters. Getters are used for retrieval of informations. Setters are used for inserting new data to Nicereply's storage.
List of avaiable getter functions follows:
| getCompanyAverage | retrieve average score of all ratings for given Company identified by API key |
| Permission | public |
| Parameters: |
|
| Sample response: | {"companyAverage":7.8} |
| getCompanyRatingsCount | retrieve total number of all ratings for given Company identified by API key |
| Permission | public |
| Parameters: |
|
| Sample response: | {"companyRatingsCount":15} |
| getCompanyAverageLastWeek | retrieve average score of ratings arrived last week for given Company identified by API key |
| Permission | public |
| Parameters: |
|
| Sample response: | {"companyAverageLastWeek":7.9091} |
| getCompanyRatingsCountLastWeek | retrieve number of ratings arrived last week for given Company identified by API key |
| Permission | public |
| Parameters: |
|
| Sample response: | {"companyRatingsCountLastWeek":11} |
| getCompanyAverageLastMonth | retrieve average score of ratings arrived last month for given Company identified by API key |
| Permission | public |
| Parameters: |
|
| Sample response: | {"companyAverageLastMonth":7.9091} |
| getCompanyRatingsCountLastMonth | retrieve number of ratings arrived last month for given Company identified by API key |
| Permission | public |
| Parameters: |
|
| Sample response: | {"companyRatingsCountLastMonth":11} |
| getCompanyDifference | retrieve percentage difference of average score of ratings arrived this and previous week for given Company identified by API key |
| Permission | public |
| Parameters: |
|
| Sample response: | {"companyRatingsDifference":97.7275} |
| getCompanyStats | retrieve all public attributes for given Company identified by API key |
| Permission | public |
| Parameters: |
|
| Sample response: | { "companyAverage":7.8, "companyRatingsCount":15, "companyAverageLastWeek":7.9091, "companyRatingsCountLastWeek":11, "companyRatingsDifference":97.7275 } |
| getCompanyRatings | retrieve pagged list of complete ratings of all agents for given Company identified by API key. In reverse time order. |
| Permission | private |
| Parameters: |
|
| Sample response: | { "ratings":[ { "score":"9", "userName":"John Doe", "from":"Betty Lee", "ipAddr":"39.75.252.10", "comment":"Good response, problem solved", "ticket":"https://otrs.example.com/index.pl?Action=AgentTicketZoom&TicketID=1", "created":"1316454269" }, { "score":"10", "userName":"Ashlee Defreyne", "from":"Walfrid Bodkin", "ipAddr":"89.95.157.157", "comment":"", "ticket":"https://otrs.example.com/index.pl?Action=AgentTicketZoom&TicketID=2", "created":"1316485564" } ], "pageSize":25, "currentPage":0, "itemCount":2 } |
| getUserAverage | retrieve average score of all ratings of given agent for given Company identified by API key. |
| Permission | public |
| Parameters: |
|
| Sample response: | {"userAverage":8.6} |
| getUserRatingsCount | retrieve total number of all ratings of given agent for given Company identified by API key. |
| Permission | public |
| Parameters: |
|
| Sample response: | {"userRatingsCount":8.6} |
| getUserAverageLastMonth | retrieve total number of all ratings of given agent for given Company identified by API key. |
| Permission | public |
| Parameters: |
|
| Sample response: | {"userAverageLastMonth":8.6} |
| getUserRatingsCountLastMonth | retrieve number of ratings arrived last month for concrete agent within given Company identified by API key. |
| Permission | public |
| Parameters: |
|
| Sample response: | {"userRatingsCountLastMonth":5} |
| getUserAverageLastWeek | retrieve total number of all ratings of given agent for given Company identified by API key. |
| Permission | public |
| Parameters: |
|
| Sample response: | {"userAverageLastWeek":8.6} |
| getUserRatingsCountLastWeek | retrieve number of ratings arrived last week for concrete agent within given Company identified by API key. |
| Permission | public |
| Parameters: |
|
| Sample response: | {"userRatingsCountLastWeek":5} |
| getUserDifference | retrieve percentage difference of average score of ratings arrived this and previous week for concrete agent within Company identified by API key. |
| Permission | public |
| Parameters: |
|
| Sample response: | {"userRatingsDifference":-12.256} |
| getUserStats | retrieve all public attributes of concrete agent within Company identified by API key. |
| Permission | public |
| Parameters: |
|
| Sample response: | { "userAverage":4.6667, "userRatingsCount":3, "userAverageLastMonth":4.6667, "userRatingsCountLastMonth":3, "userRatingsDifference":-12.256 } |
| getUserRatings | retrieve pagged list of complete ratings of concrete agent for given Company identified by API key. In reverse time order. |
| Permission | private |
| Parameters: |
|
| Sample response: | { "ratings":[ { "score":9, "userName":"John Doe", "from":"Steve Longbow", "ipAddr":"92.240.229.10", "comment":"Satisfied. Thanks.", "ticket":"https://otrs.example.com/index.pl?Action=AgentTicketZoom&TicketID=1", "created":"1316454269" }, { "score":5, "userName":"John Doe", "from":"Khristin Kerrane", "ipAddr":"124.201.33.117", "comment":"", "ticket":"https://otrs.example.com/index.pl?Action=AgentTicketZoom&TicketID=2", "created":"1316485564" }, ], "pageSize":25, "currentPage":0, "itemCount":2 } |
| getUserList | retrieve list of agents for given Company identified by API key. |
| Permission | private |
| Parameters: |
|
| Sample response: | { "users":[ { "id":1, "username":"johndoe", "fullname":"John Doe", "email":"johndoe@example.com" }, { "id":2, "username":"ashlee", "fullname":"Ashlee Defreyne", "email":"ashlee.defreyne@example.com" } ] } |
| getPublicCompanyRatings | retrieve public profile ratings for given Company identified by API key. |
| Permission | public |
| Parameters: |
|
| Sample response: | { "ratings": [ "Satisfied. Thanks.", "Very good!!! Thanks", ], "pageSize": 10, "currentPage": 1, "itemCount": 2, "totalItemCount": 2 } |
List of avaiable setter functions follows:
| setRating | insert new rating of concrete agent within Company identified by API key |
| Permission | private |
| Parameters: |
|
| Sample response: | {"result":"OK","id":348454} |
| Sample error response: | { "error":{ "score":["Score cannot be blank."], "from":["From cannot be blank."] } } |
| updateRating | update rating identified by id or ticket |
| Permission | private |
| Parameters: |
|
| Sample response: | {"result":"OK"} |
| Sample error response: | { "error":{ "score":["Score cannot be blank."] } } |
Use od Nicereply API in your project is possible by two ways: on server-side and on client-side.
You have to communicate with Nicereply API via HTTP protocol. HTTP request has to be either GET or POST type. The response from our servers comes in form of JSON strings with application/json MIME type. The best way for communicatating with our API is cURL. Here is a small code snippet in PHP:
<?php $apiUrl = 'http://www.nicereply.com/api/setRating'; $apiKey = 'YOUR_API_KEY'; // mostly private API key $username = 'johndoe'; // rating of John Doe $data = '&score=9&from=Betty%20Lee&comment=Lorem%20ipsum%20dolor%20sit%20amet'; $data = 'apikey=' . $apiKey . $data; $data .= '&username=' . rawurlencode($username); $curl = curl_init($apiUrl); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = json_decode(curl_exec($curl)); echo '<pre>'; print_r($result); echo '</pre>';
The above code uses setRating API function. First, we setup some basic variables such as the API URL or API key, In the next step, we setup data, which will be sent to the server. In the third step, we use cURL to handle HTTP communication with Nicereply API. And on the last two rows, we pick up response from the server and list it in a browser.
Use this example as a skeleton for your real application. In other server scripting languages, the steps are almost the same. We prepared ready-to-deploy sample PHP application in form of a .zip archive. Follow these simple steps to install this application to your server:
.zip archive.index.php file in your text editor and paste the key on line 3 between apostrophes.On the client side, the primary scripting language is Javascript. You can use Nicereply API to list some public attributes of your Company or your agents on your page. There are two options on how to implement it on your page:
If you don't have any experience in Javascript, you can simply include a small code snippet of HTML of your page. The snippet is dynamic - you can modify displayed attribute (company average, num of company ratings, etc.) by simply editing one row in the snippet.
To start using it, just paste this code anywhere into your HTML code of the page.
<script type="text/javascript" src="http://www.nicereply.com/site/js/apikey/YOUR_PUBLIC_APIKEY"></script> <div id="nicereplyAverage"></div>
The snippet will fetch average rating of your company (identified by YOUR_PUBLIC_APIKEY) and inject it into <div id="nicereplyAverage">. Fetched attribute is determined by ID of the div element. There are 6 ID codes avaiable now:
You are free to include multiple divs with various IDs on one page (they can even be at different places). Just don't forget to set the API key correctly.
If you have a good knowledge of Javascript and want to have full control over communication with Nicereply API, you can write your own Javascript code. In the next example, we will create a little widget, which lists a Company average score and week-by-week difference in average score. We will use jQuery Javascript library as it is making AJAX communication with Nicereply a lot easier than raw Javascript.
First, prepare a placeholder for API call results. In this case, it's a DIV with id="widget". Then, include jQuery library into your page. Also, prepare a DOM ready function. This will be the place where you put all javascript code of this example. As you can see, we've used an alias to ready function, and this is perfectly OK & safe. One little note - we recommend to include the jQuery from Google CDN servers. That's a good thing for the speed of your page, because there is some possibility that your visitors already have this library cached in their browser.
<body> ... <div id="widget"> <h3>Our rating:</h3> <p id="companyAverage"></p> <p id="companyDiff"></p> </div> ... <script type="text/javascript" "src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ }); </script> </body> </html>
Now let's start writing some useful javascript code:
First, we'll have to define a function for handling errors. As mentioned before, if an error on our server occurs, the response JSON object will contain a property named error. Because the content of this property can be eihter string or object, your error handling function must be ready for both scenarios. Insert the following code into your DOM ready() function.
function handleError(err) { if (typeof(err) == 'object') { for (error in err) { if (typeof(error) == 'array') { for (var i = 0, len = error.length; i < len; i++) { alert(error[i]); } } else { alert(error); } } } else { alert(err); } }
Next, let's create some useful variables (insert it after the error handlig function):
var url = 'http://www.nicereply.com/api/', apikey = 'YOUR_PUBLIC_API_KEY', $widget = $('#widget');
Once again we must warn you - inside of client scripts (javascript files) use only public API key!
Now, create the part of code which will execute a cross-domain ajax call to Nicereply API functions. We'll be using the ajax function included with jQuery. Cross-domain communication is achieved by using jsonp dataType for communication. If you review getCompanyAverage and getCompanyDifference API calls, you see that they both require only apikey as required parameter. Also, we will concatenate the URL with our required API function. Ajax call is fired immediately after the page loads.
$.ajax({ type : "GET", url : url + 'getCompanyAverage', data : {apikey:apikey}, dataType : 'jsonp', success : function(data){ if (typeof(data.error) == 'undefined') { // response error? // no error $widget.children('#companyAverage').html('<strong>Company Average:</strong> ' + data.companyAverage.toFixed(2)); } else { // error handleError(data.error); } } }); $.ajax({ type : "GET", url : url + 'getCompanyDifference', data : {apikey:apikey}, dataType : 'jsonp', success : function(data){ if (typeof(data.error) == 'undefined') { // response error? // no error $widget.children('#companyDiff').html('<strong>Company Difference:</strong> ' + data.companyRatingsDifference.toFixed(2)); } else { // error handleError(data.error); } } });
Both AJAX calls will run at the same time. We created the so-called callback function for success AJAX event. When AJAX call is successfully completed, this callback function will be executed. This technique is known as asynchronous code execution. As you can see in the code of success callback function, we are checking for presence of an error property in the response object. If that object is present, we pass the error property to our error handling function.
If an error was not occured, we place the received data to the placeholder. We've used a function called toFixed() to round received numbers. You are free to style your widget any way you want, for example like this:
widget sample
In this last section, we share some open-source code snippets, which are useful in building your application communicating with the Nicereply API. Examples include:
You can use the Nicereply API as a little rectangular block on your page. Just include the latest jQuery in the <head> of your page and insert this code snippet to the appropriate place on your page. Don't forget to set up your apiKey and the required API function.
<div id="NRwidget"> <h3>Our rating:</h3> <p><strong>Company Average:</strong> <span id="NRvalue"></span></p> </div> <script type="text/javascript">
$(function(){
var apiUrl = 'http://www.nicereply.com/api/',
apiKey = 'YOUR_PUBLIC_API_KEY',
$widget = $('#NRwidget');
$.ajax({
type : "GET",
url : apiUrl + 'getCompanyAverage',
data : {apikey:apiKey},
dataType : 'jsonp',
success : function(data){
if (typeof(data.error) == 'undefined') {
$widget.find('#NRvalue').html(data.companyAverage.toFixed(2));
} else {
var errMsg = '';
if (typeof(data.error) == 'object') {
for (error in data.error) {
if (typeof(error) == 'array') {
for (var i = 0, len = error.length; i < len; i++) {
errMsg += error[i] + ', ';
}
} else {
errMsg = error;
}
}
} else {
errMsg = data.error;
}
$widget.find('#NRvalue').html(errMsg);
}
}
});
});
</script>