Server to Server
For this integration method, the DeviceAssure library collects device data and makes it available to your application. Your application then sends this data to your backend server, which makes the validation request to the DeviceAssure API.
This approach is useful when you want to:
- Keep your licence key secure on the server side
- Integrate validation into existing server-side workflows
- Have full control over when and how validation requests are made
Note: A licence key is not required on the client side. The licence key must be provided when your server calls the DeviceAssure API.
Overview
There are five main steps to validate a device:
- Initialize the DeviceAssure library with the application context
- Call the
getData()method with a callback handler to collect device data - Transmit the collected data securely to your server
- Your server makes a request to the DeviceAssure API with the collected data and HTTP headers
- Validation results are returned directly to the server
Implementation
Step 1: Initialize the Library
Initialize DeviceAssure in your Application class's onCreate() method:
Step 2: Define the Callback Interface
Create a delegate interface to handle the collected data:
Step 3: Create a Callback Handler
import com.deviceassure.android.library.Callback
import org.json.JSONObject
class DeviceAssureCallback(private val delegate: DeviceAssureDelegate) : Callback {
override fun taskComplete(collectedData: JSONObject) {
// The data collected from the device is passed here
// Send it securely to your server for validation
delegate.onResponse(collectedData)
}
override fun handleError(errorStr: String) {
delegate.onError(errorStr)
}
override fun progressUpdate(percent: Int?) {
// Track progress if needed
}
}
import org.json.JSONObject;
import com.deviceassure.android.library.Callback;
public class DeviceAssureCallback implements Callback {
private DeviceAssureDelegate delegate;
public DeviceAssureCallback(DeviceAssureDelegate delegate) {
this.delegate = delegate;
}
@Override
public void taskComplete(JSONObject collectedData) {
// The data collected from the device is passed here
// Send it securely to your server for validation
delegate.onResponse(collectedData);
}
@Override
public void handleError(String errorStr) {
delegate.onError(errorStr);
}
@Override
public void progressUpdate(Integer percent) {
// Track progress if needed
}
}
Note:
Callbackalso declares two deprecated hooks,statusUpdate()and a no-argumenttaskComplete(). The library never calls either and they do not need to be implemented.
Step 4: Collect Device Data
Call the getData() method from anywhere in your application:
Complete Example
import android.app.Application
import android.util.Log
import com.deviceassure.android.library.DeviceAssure
import org.json.JSONObject
class MyApplication : Application(), DeviceAssureDelegate {
override fun onCreate() {
super.onCreate()
// Initialize the library
DeviceAssure.init(applicationContext)
// Collect device data
DeviceAssure.getData(DeviceAssureCallback(this))
}
override fun onResponse(json: JSONObject) {
Log.d(TAG, "Device data collected: $json")
// Send this data to your server for validation
sendToServer(json)
}
override fun onError(error: String) {
Log.e(TAG, "Collection error: $error")
}
private fun sendToServer(deviceData: JSONObject) {
// Implement secure transmission to your server
// Your server will then call the DeviceAssure API
}
companion object {
private const val TAG = "DeviceAssure"
}
}
import android.app.Application;
import android.util.Log;
import org.json.JSONObject;
import com.deviceassure.android.library.DeviceAssure;
public class MyApplication extends Application implements DeviceAssureDelegate {
private static final String TAG = "DeviceAssure";
@Override
public void onCreate() {
super.onCreate();
// Initialize the library
DeviceAssure.init(this.getApplicationContext());
// Collect device data
DeviceAssure.getData(new DeviceAssureCallback(this));
}
@Override
public void onResponse(JSONObject json) {
Log.d(TAG, "Device data collected: " + json.toString());
// Send this data to your server for validation
sendToServer(json);
}
@Override
public void onError(String error) {
Log.e(TAG, "Collection error: " + error);
}
private void sendToServer(JSONObject deviceData) {
// Implement secure transmission to your server
// Your server will then call the DeviceAssure API
}
}
The Suspending API
Kotlin callers can use getDataAsync() instead of a callback. It suspends until the device data has been collected and returns a ValidationResult. No call is made to the DeviceAssure API — the returned data is for your application to send to your own server.
Note: The suspending API is for Kotlin callers. A
suspendfunction compiles to a method that takes aContinuationparameter, which is not a usable API from Java, so Java integrators use the callback API shown above — it collects the same data.
import com.deviceassure.android.library.DeviceAssure
import com.deviceassure.android.library.ValidationResult
lifecycleScope.launch {
when (val result = DeviceAssure.getDataAsync()) {
is ValidationResult.Success -> sendToServer(result.response)
is ValidationResult.Error -> Log.e(TAG, "Collection error: ${result.message}")
}
}
Note: This example uses two dependencies you need to declare yourself, for different reasons:
launchcomes fromorg.jetbrains.kotlinx:kotlinx-coroutines-android— the library already supplies this at runtime, but not on your compile classpath, so writing this code needs your own compile-time declaration too — andlifecycleScopefromandroidx.lifecycle:lifecycle-runtime-ktx, which the library does not use at all. See Runtime Dependencies for the coroutines compile-classpath requirement.
See Client to Server for the full ValidationResult API, including getOrNull(), getOrThrow(), onSuccess { } and onError { }.
When to Call Data Collection
The getData() method can be called from anywhere in the application, but it is recommended to call it:
- On application install
- On application create (
onCreate())
Exception: If certain permissions are required before calling the library (e.g., to allow collection of the TAC), wait until those permissions are granted.
Server-Side Implementation
Once your server receives the collected data from the client, it must call the DeviceAssure REST API. See the Server to Server API documentation for:
- API endpoint URLs
- Required HTTP headers (DV-Client-IP, DV-User-Agent)
- Request format and examples
- Response handling
See Also
- Client to Server - Direct validation from the app
- How It Works - Client to Server and Server to Server models
- Server to Server API - Backend API implementation details
- API Responses - Understanding validation results and classifications