Site icon apilayer Blog

How To Detect Any Device And OS In Real-Time With Node.js

How To Detect Any Device And OS In Real-Time With Node.js

What is userstack API?

The userstack API is a real-time, easy-to-use REST API interface capable of parsing User-Agent strings to precisely detect device, browser, and operating system information. 

Implementing the JSON/XML API in your frontend or backend system will give you all the tools you need to take control of your website’s or application’s user experience and the customer journey that goes with it. This API can also be combined with a geolocation api.

Why UserStack API?

UserStack API stands out because of these features:

How to use UserStack API?

Just head over and sign up for a free account, and you will get your free API access key. With your uniquely generated API access key you can start using the UserStack API from anywhere.

Here is a sample of an API request with the access_key parameter.

And here is the result of the request:

[crayon-66405d4e3b380976594405/]

How to do a Bulk User-Agent Lookup?

If you want to check multiple User-Agent strings, you can use the base endpoint with the ua_batch parameter that contains the User-Agent strings to process.

Here is an example of executing a Bulk-User-Agent request:

Specify Response Fields

When you make a bulk lookup, the response can be large. It is usually better to specify your response fields for only the data you need.

Here is how to configure your response fields:

How do I integrate userstack API into the Node.js web app?

In this demonstration, we will create a Node.js web app that integrates the UserStack API. 

How to set up Node.js with UserStack API?

Open your terminal, PowerShell or Bash. Next, type these commands to create a folder and initialize the basic project files.

Then go back to your terminal and type this command to install the required node modules.

Now in this part, copy the static (CSS, js, images) files from our repository. Here is our project file structure:

Next,  install nodemon. nodemon is a tool that helps you develop NodeJS-based applications by automatically restarting the node application when file changes are detected. It improves your productivity.

You can follow these steps to configure nodemon.

[crayon-66405d4e3b38b812712390/]

Now, go back to your code editor and enter this code:

[crayon-66405d4e3b38f994743404/]
[crayon-66405d4e3b391321667795/]
[crayon-66405d4e3b393706871606/]

Here is our final result. Check out the full source code here. 

Hope you liked this demonstration!

Head over now and get your free subscription plan on UserStack

Exit mobile version