# DeviceAtlas Cloud Client API Usage #
This section deals with usage of the DeviceAtlas Cloud API.
## Client Side Library - Apple Device Identification ##
In addition to the server-side API, an optional client-side Javascript library
is available. This library is important for two reasons:
1. It facilitates identification of Apple devices. Apple devices are _not_
identifiable using only HTTP User-Agent data.
2. It allows for the collection of dynamic or changing properties.
When the client-side library is embedded in a web page it collects additional
properties from the client's web browser. This data is then sent back to the
server and must be passed to the DeviceAtlas API along with the HTTP headers.
The combination of the client data and the server side data allow for accurate
identification of Apple devices.
The client-side library may be included on a webpage by adding the following
snippet:
```Javascript
```
By default, the client-side library returns the data via a cookie. If this is
present in the `HttpServletRequest` object it will be automatically used. The
cookie name is configurable via the `Config` class.
Alternatively, the client data may be returned via AJAX and passed to the server
side API manually.
For additional information, please see the [Client Side Library](https://docs.deviceatlas.com/apis/clientside/latest/README.ClientSide.html) documentation.
### Basic Usage ###
The DeviceAtlas Cloud Client API can be used as follows:
* Import:
```java
import com.deviceatlas.cloud.deviceidentification.client.Client;
import com.deviceatlas.cloud.deviceidentification.client.Result;
import com.deviceatlas.cloud.deviceidentification.client.Properties;
import com.deviceatlas.cloud.deviceidentification.client.ClientException;
import com.deviceatlas.cloud.deviceidentification.cacheprovider.CacheException;
import com.deviceatlas.cloud.deviceidentification.cacheprovider.CacheProvider;
import com.deviceatlas.cloud.deviceidentification.cacheprovider.FileCacheProvider;
import java.util.HashMap;
import java.util.Map;
```
* Get an instance of the API (singleton), set your DeviceAtlas licence key and request properties from DeviceAtlas Cloud.
In a servlet container with a HttpServletRequest object:
```java
try {
/* First time usage, the cache provider needs to be set */
Client client = Client.getInstance(new FileCacheProvider());
/* for further use get the API instance (singleton) */
Client client = Client.getInstance();
/* set your licence key */
client.setLicenceKey(LICENCE_KEY);
/* get the device properties - by request object */
Result result = client.getResult(request);
Map headers = result.getHeaders();
Properties properties = result.getProperties();
} catch (ClientException ex) {
/* handle the errors */
} catch (CacheException ex) {
/* handle the errors */
}
```
* Detecting based on a set of HTTP headers:
```java
Map headers = new HashMap();
headers.put("user-agent", "THE USER AGENT ...");
/* add more headers .*/
try {
/* get the API instance (singleton) */
Client client = Client.getInstance(new FileCacheProvider());
/* set your licence key */
client.setLicenceKey(LICENCE_KEY);
/* get device properties - by passing HTTP headers */
Result result = client.getResultByHeaders(headers);
Properties properties = result.getProperties();
} catch (ClientException ex) {
/* handle the errors */
} catch (CacheException ex) {
/* handle the errors */
}
```
* Detecting based on a User-Agent string:
```java
String userAgent = "THE USER AGENT ...";
try {
/* get the API instance (singleton) */
Client client = Client.getInstance(new FileCacheProvider());
/* set your licence key */
client.setLicenceKey(LICENCE_KEY);
/* get device properties - by passing a user agent string */
Result result = client.getResultByUserAgent(userAgent);
Properties properties = result.getProperties();
} catch (ClientException ex) {
/* handle the errors */
} catch (CacheException ex) {
/* handle the errors */
}
```
* Use the device properties:
```java
try {
if (properties.containsKey("mobileDevice") && properties.get("mobileDevice").asBoolean()) {
/* example 1: Get the screen width for image optimization */
int displayWidth = properties.containsKey("displayWidth")?
properties.get("displayWidth").asInteger(): 100;
/* example 2: Get the device vendor name */
String vendor = properties.containsKey("vendor")?
properties.get("vendor").asString(): "";
/* example 3: Touch screen optimization */
boolean useBiggerIcons = properties.containsKey("touchScreen")?
properties.get("touchScreen").asBoolean(): false;
/* example 4: Send Geo Location JS to client? */
boolean supportsGeoLocation = properties.containsKey("js.geoLocation")?
properties.get("js.geoLocation").asBoolean(): false;
}
} catch(IncorrectPropertyTypeException exception) {
// Handle exception
}
```
See the list of all property names here:
https://deviceatlas.com/resources/available-properties
The availability of a property depends on the device and your licence, before
accessing a property always check if it exists in the set or not.
### Additional Examples ###
Please find more complete examples in the /Examples directory.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
_ Copyright (c) DeviceAtlas Limited 2023. All Rights Reserved. _
_ https://deviceatlas.com _