If you’ve generated an API Token on the Developer Dashboard or successfully made your app CORS compliant, we can now move on to retrieving information from a Merchant’s account using the WEB API.
Every programmer knows, it’s not customary to type the same code over and over for each API call. This means, it would be better to create a class, function, or interface in PHP or Java and send relevant information to it. This makes your app relatively smaller in size and once you debug and fix any errors, you won’t have to repetitively type the same code that calls curl or okHTTP3.
Before continuing with this tutorial, I assume you are familiar with HTML/PHP or Android Studio (Java). The authorization flow is similar with both cURL in PHP and okHTTP3 in Android Studio and will include sample code for both.
Let’s get started with a simple request to retrieve Merchant information and output it as JSON.
The first thing you want to do is setup your curl or http request. You will have to pass the Authorization header with your request.
PHP
<?php
$mid = YOUR_MERCHANT_ID;
$token = YOUR_API_TOKEN;
// If testing on production use with same endpoint
//$restapi = 'https://api.clover.com';
$restapi = 'https://apisandbox.dev.clover.com';
// Set header.
$header = array();
$header[] = sprintf('Authorization: Bearer %s', $token);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => sprintf('%s/v3/merchants/%s', $restapi, $mid),
CURLOPT_HTTPHEADER => $header
));
$result = curl_exec($ch);
if ($result === false) {
// In case of error, Print CURL Error.
echo sprintf('%s: %s', "CURL Error", curl_error($ch));
} else {
// If successful, print JSON response.
header('Content-Type: application/json');
echo $result->elements;
curl_close($ch);
}
?>
JAVA
String merchantId = YOUR_MERCHANT_ID;
String apiToken = API_TOKEN;
// If testing on production use with same endpoint
//String restapi = 'https://api.clover.com';
String restapi = 'https://apisandbox.dev.clover.com';
String uri = String.format('%s/v3/merchants/%s', restapi, merchantId);
JSONObject data = null;
OkHttpClient client = new OkHttpClient();
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body;
HttpUrl reqUrl;
reqUrl = HttpUrl.parse(uri);
Request.Builder builder = new Request.Builder();
builder.header("Authorization", String.format("Bearer %s", apiToken));
assert reqUrl != null;
builder.url(reqUrl);
Request request = builder.build();
try {
Call call = client.newCall(request);
Response nqResponse = call.execute();
if (String.valueOf(nqResponse.code()).equals("200")) {
assert nqResponse.body() != null;
String response = nqResponse.body().string();
data = new JSONObject(response);
} else {
data = new JSONObject();
}
} catch (IOException | JSONException e) {
e.printStackTrace();
}
console.log(data.optString("elements"));
}
In the two examples, Merchant Information will be returned as JSON.
From here you can get the Merchant Name, Address, Phone Number, etc from this endpoint.
You can further expand such fields using the query string ?expand={EXPANDED_FIELD} to get the address, tax_rates, printers, tenders, etc. Or you can send a GET request to the appropriate endpoint and get information directly.
These methods can be used to retrieve item information, categories, orders, and so on by changing the endpoint. Just remember that every GET, PUT, DELETE has to have the API Token in the header otherwise the server will respond with
{"message":"401 Unauthorized"}
More information can be found about available endpoints on their website at https://docs.clover.com/build/web-api/.
The next part of this tutorial will be an API call to add an item to the merchant’s database. I will be posting it soon so if you’re interested, keep an eye out.
If there is any part of this code that you don’t understand, have questions, or found an error, feel free to leave a comment. If you like this tutorial, be sure to share it on social media. If you really like this tutorial and would like to see more tutorials like this in the future, my “Tip Jar” is on the right.