# DeviceAtlas Device Identification API # This document describes the DeviceAtlas API and how it is used. The DeviceAtlas Device Identification API consists of two libraries. A core library containing all of the main identification logic and a secondary extension library to allow for easier integration into a web application context. ## Data File ## ### Loading the data file ### The DeviceAtlas API provides multiple methods to load the data file. #### Downloading and loading the data file #### The following examples download the data file, save it to the file system, load it into the DeviceAtlas API and schedule a background task to download and load a fresh data file at a configured time in the future. Please see the [Data File Configuration documentation](README.DataFileConfig.html) to obtain the data file download URL and for data file configuration options. ```csharp using Com.DeviceAtlas.Device; using Com.DeviceAtlas.DataFile; DeviceApi deviceApi = new DeviceApi(); string dataFileUrl = <data file download URL>; // Load using custom configuration DataFileConfig dataFileConfig = DataFileConfigBuilder.Create(<data file download URL>).FileDirectory(<persistent download directory>).BuildDevice(); deviceApi.DownloadAndLoadDataFile(dataFileConfig); // Load using default configuration deviceApi.DownloadAndLoadDataFile(dataFileUrl); ``` #### Loading an existing data file #### The following examples show how to load a data file that is already on the filesystem. ```csharp using Com.DeviceAtlas.Device; DeviceApi deviceApi = new DeviceApi(); // Load data from file path deviceApi.LoadDataFromFile(<data file path>); // Load data from a Stream deviceApi.LoadDataFromStream(<Stream instance>); ``` The exception DataLoadingException is thrown by the above methods in case the data file cannot be loaded. ## 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 <!-- Adding the DeviceAtlas client side component to get client side properties --> <script type="text/javascript" src="https://cs.deviceatlas-cdn.com/dacs.js" async></script> ``` By default, the client-side library returns the data via a cookie. If this is present in the `HttpRequest` 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. ### Usage Examples ### The following sections shows how to call the DeviceApi. Please note that exception handling has been omitted for brevity. #### EXAMPLE 1: Basic Usage - Single User-Agent #### ```csharp DeviceApi deviceApi = new DeviceApi(); DataFileConfig deviceDataFileConfig = DataFileConfigBuilder.Create(<data file download URL>).FileDirectory(<persistent download directory>).BuildDevice(); deviceApi.DownloadAndLoadDataFile(deviceDataFileConfig); // pass an identifier to the API string userAgent = "Mozilla/5.0 (Linux; Android 11; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Mobile Safari/537.36"; Properties properties = deviceApi.GetProperties(userAgent); // print all properties foreach (KeyValuePair<string, Property> kv : properties) { Console.WriteLine(kv.Key + ": " +kv.Value.Value() + " (" + kv.Value.GetDataType() + ")"); } // use a property if (properties.ContainsKey("mobileDevice")) { bool mobileDevice = properties.Get("mobileDevice").AsBoolean(); // do something } // or get the property value as a string with an internal check to verify if the property exists in the set, // otherwise the defaultValue is returned instead. string browserName = properties.GetOrDefault("browserName", ""); ``` #### EXAMPLE 2: Detection via HttpRequest #### To use the API in a web application where a `HttpRequest` object is available the following two step approach could be taken. Please note the following: > **Note 1:** This will automatically use HTTP Client Hints if they are present in the `HttpRequest` object. > > **Note 2:** This will automatically read the client side library cookie if it is available and use the data to detect Apple devices. ##### Step 1: Create an API instance and load the data file when the servlet starts: ```csharp public class Startup { public void ConfigureServices(IServiceCollection service) { // load the data file in a try/catch block as exceptions may be thrown DeviceApiWeb deviceApi = new DeviceApiWeb(); DataFileConfig deviceDataFileConfig = DataFileConfigBuilder.Create(<data file download URL>).FileDirectory(<persistent download directory>).BuildDevice(); deviceApi.DownloadAndLoadDataFile(deviceDataFileConfig); // store the DeviceApiWeb instance on the servlet context service.AddSingleton(deviceApiWeb); } } ``` ##### Step 2: Lookup the device properties when processing requests: ```csharp public class DeviceAtlasController : Controller { public HttpRequest req => HttpContext.Request; [Route(""), HttpGet] public ActionResult Index() { DeviceApiWeb deviceApi = HttpContext.RequestServices.GetService<DeviceApiWeb>(); Properties properties = deviceApi.GetProperties(request); // pass HttpRequest object // use the properties } } ``` #### EXAMPLE 3: Passing Multiple Headers Without A HttpRequest Object #### In some cases, it may not be possible to use a `HttpRequest` object. In this scenario, an `IDictionary` of headers may be passed to the `DeviceApi` `GetProperties()` method. It is recommended to pass all of the HTTP headers provided by the client. The below example shows passing a User-Agent along with a Client Hint header. In the example, the device properties are detected from the User-Agent and the browser properties are detected from the Client Hint header. ```csharp DeviceApi deviceApi = new DeviceApi(); DataFileConfig deviceDataFileConfig = DataFileConfigBuilder.Create(<data file download URL>).FileDirectory(<persistent download directory>).BuildDevice(); deviceApi.DownloadAndLoadDataFile(deviceDataFileConfig); // pass a collection of HTTP headers IDictionary<string, string> headers = new Dictionary<string, string>(); headers["User-Agent"] = "Mozilla/5.0 (Linux; Android 12; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/102.0.0.0 Mobile Safari/537.36"; headers["Sec-CH-UA-Full-Version-List"] = "\" Not A;Brand\";v=\"99.0.0.0\", \"Chromium\";v=\"102.0.5005.125\", \"Google Chrome\";v=\"102.0.5005.25\""; Properties properties = deviceApi.GetProperties(headers); // use the properties if (properties.ContainsKey("browserVersion")) { string browserVersion = properties.Get("browserVersion").AsString(); // do something } // or get the property value as a string with an internal check to verify if the property exists in the set, // otherwise the defaultValue is returned instead. string browserName = properties.GetOrDefault("browserName", ""); ``` #### EXAMPLE 4: Passing Client Side Data #### The data collected by the DeviceAtlas Client-side Library is automatically passed when a `HttpRequest` is used. If a `HttpRequest` object is not available the client-side data can be passed manually as a second parameter to the `GetProperties()` method. ```csharp DeviceApi deviceApi = new DeviceApi(); DataFileConfig deviceDataFileConfig = DataFileConfigBuilder.Create(<data file download URL>).FileDirectory(<persistent download directory>).BuildDevice(); deviceApi.DownloadAndLoadDataFile(deviceDataFileConfig); string userAgent = "Mozilla/5.0 (Linux; Android 12; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/102.0.0.0 Mobile Safari/537.36"; string clientSideData = "scsVersion:2.1|bcookieSupport:1|bcss.animations:1|bcss.columns:1|bcss.transforms:1|bcss.transitions:1|sdeviceAspectRatio:1795/1010" Properties properties = deviceApi.GetProperties(userAgent, clientSideData); // use the properties ``` #### EXAMPLE 5: Configuration #### During instantiation of the `DeviceApi` object an optional config parameter may be passed. The example below shows the setting of a custom cookie name that the DeviceAtlas client-side data should be found in. ```csharp Config config = new Config(); config.CookieName = "DeviceAtlasClientSide"; // override default cookie name config.MaxCacheEntries = 4096; // override default cache size DeviceApi deviceApi = new DeviceApi(config); deviceApi.LoadDataFromFile("/path/to/datafile.json"); // ... use the API as per previous examples ``` #### Additional Examples #### Please find more complete examples in the /Examples directory. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - _ Copyright (c) DeviceAtlas Limited 2023. All Rights Reserved. _ _ https://deviceatlas.com _ <!-- HTML+JS for document formatting when opened in browser --> <div class="btn-group" id="main-menu" style="float:right"><a class="btn dropdown-toggle" data-toggle="dropdown" href="#">Menu<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="README.html">Main</a></li><li><a href="README.Installation.html">Enterprise API Installation</a></li><li><a href="README.DataFileConfig.html">Data File Configuration</a></li><li class="disabled"><a href="README.DeviceApi.html">Device Identification API</a></li><li><a href="README.CarrierApi.html">Carrier Identification API</a></li><li><a href="README.ClientHints.html">Client Hints Support</a></li><li><a href="README.Upgrade.html">Device API Upgrade</a></li><li class="divider"></li><li><a href="./DeviceApiDocs/classes.html">Device API Docs</a></li><li><a href="./CarrierApiDocs/classes.html">Carrier API Docs</a></li></ul></div>