Get a device ID in JavaScript with browser fingerprinting
Browsers have no device ID API. How to recognise a returning device with a stored ID and a browser fingerprint, what breaks each one, and the 7.5 kB library I built for it.
In short A web page can't read a real device ID: no IMEI, no MAC address, no serial number. What works is two IDs used together: a random one stored in a cookie and localStorage, and a fingerprint built from stable device values for when the stored one is gone. VisitoJS, the open-source library I built, returns both in 7.5 kB and makes no network requests.
Sooner or later every website wants the answer to one question: has this device been here before?
A shop wants to know whether ten orders under ten names came from one phone. A login page wants to warn you about a sign-in from a device it has never seen. A SaaS wants one free trial per person, not one per email address. An analytics page wants to count people, not page loads.
All of them go looking for a device ID in JavaScript, and all of them find the same thing: there isn’t one.
There is no device ID in the browser
A native app can ask the phone for an identifier. A web page can’t. JavaScript in a browser has no access to the IMEI, the MAC address, the serial number, the Android ID or the advertising ID. That is on purpose. If any website could read a permanent hardware number, every site you visit could follow you forever, and clearing your cookies would change nothing.
So every “device ID” on the web is built from one of two things: something the site stored on the device, or something it observed about the device. Both work. Both break, in different ways.
Option 1: store a random ID
This is the answer you’ll find on Stack Overflow, and it’s a good start:
let id = localStorage.getItem('device_id')
if (!id) {
id = crypto.randomUUID() // needs an HTTPS page
localStorage.setItem('device_id', id)
}
It’s exact: two phones never share an ID. But the ID belongs to the browser’s storage, not to the device, and storage is fragile.
- Clearing site data deletes it. The next visit is a new device.
- A private window starts empty, so it gets a new ID every time.
- Safari deletes script-written storage by itself. WebKit caps cookies set from JavaScript at seven days, and removes a site’s localStorage after seven days of Safari use without the visitor interacting with that site.
- Every browser on the phone has its own storage. That includes the browsers hidden inside apps. A shopper taps your ad in Instagram, looks at a product in Instagram’s in-app browser, then comes back the next day in Chrome. Same phone, same person, two IDs.
That last one matters more than it looks. For a shop that advertises on Facebook, Instagram or TikTok, a large share of visits start inside those apps.
Option 2: fingerprint the browser
Browser fingerprinting (also called device fingerprinting) goes the other way. It stores nothing. It reads values the browser exposes to any script, such as the user agent, the screen size, the language, the amount of memory, or how the device draws a hidden canvas, and hashes them into one ID. The same device should give the same hash tomorrow, even after its cookies are gone.
It solves the storage problem and brings three of its own.
Fingerprints change when the device hasn’t. The user agent contains the browser version, and Chrome ships a new one about every four weeks. Canvas and WebGL output can change with a browser or graphics driver update. A fingerprint that hashes those values gives a returning visitor a new ID every month.
Browsers fight back. Brave randomises canvas and audio output for each site and session, so a fingerprint built on them is different on every visit. Safari and Firefox hide or blur some values too. Chrome on Android no longer puts the real phone model in the user agent: every phone reports Android 10; K, and the model has to be requested separately through client hints.
And a fingerprint is not unique. Two phones of the same model, with the same language and settings, look the same from inside a web page. A fingerprint narrows the field. It doesn’t name one device.
There is also the cost of the library itself. The two best-known open-source ones weigh 11 and 16 kB gzipped, and when I measured them, each could make network requests of its own with default options.
The setup that holds: use both
Neither ID is enough alone, and they fail in opposite situations. The stored ID is exact but easy to lose. The fingerprint survives a cleared cookie but is shared by similar devices. So the dependable setup is:
- Give every browser a stored ID, and treat it as the main key.
- Build a fingerprint from values that don’t change with updates, and use it only when the stored ID is missing.
- Record how much each reading can be trusted, so your server knows a firm ID from a weak one.
I wanted that in one small file with no surprises, and couldn’t find it. So I built it.
VisitoJS: both IDs in 7.5 kB
VisitoJS is an open-source (MIT) script that returns a stored ID, a fingerprint ID and a set of flags. The core is 7,460 bytes gzipped, has no dependencies, and makes no network requests. You can watch it run on your own device in the live demo.
Load it from the CDN:
<script src="https://cdn.jsdelivr.net/npm/visitojs@1.0.0/dist/visito.min.js"
integrity="sha384-tRACvKo0CYtFU1/Ku84LiHwYALdV9Qxc7ZWmqGw8lkqfKUqlnkj/ROePV2J2ZgZZ"
crossorigin="anonymous"></script>
<script>
Visito.get(function (visitor) {
if (!visitor) return // null inside a frame
console.log(visitor.did) // stored id
console.log(visitor.id) // fingerprint id
})
</script>
Or install it:
npm install visitojs
import { get } from 'visitojs'
get((visitor) => {
if (!visitor) return
fetch('/api/visit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(visitor),
keepalive: true,
})
})
The callback receives a reading like this one:
{
"v": 3,
"did": "3f1c9a52-7d0e-4b6a-9c1e-2a5b8d4f6e10",
"didNew": false,
"id": "a46245e0...",
"idStrict": "110d1171...",
"model": "Pixel 10",
"os": "android",
"osVer": "17",
"app": "chrome",
"appVer": "157.0.0.0",
"flags": { "partial": [], "auto": [], "private": null, "weak": false }
}
didis the stored ID. It lives in a first-party cookie and in localStorage, and each copy restores the other if only one is deleted.idis the fingerprint ID.idStrictis the fingerprint plus the OS and app versions, for telling similar devices apart inside a short window.flagssay what the reading could not establish.
How it answers each problem
Each of the problems above shaped a decision in the library.
- There are no version numbers in the fingerprint.
idis built from the device model, a memory bucket, the language and the text-size setting. On devices that name no model, such as desktops, the screen size and core count stand in. A browser or app update alone doesn’t change it. In my tests it stayed the same across 20 reloads, a user-agent version bump, a screen rotation and cleared storage. - Randomised values are left out, and the reading says so. Where Brave, Safari or Firefox hide or randomise a value, VisitoJS doesn’t hash it.
flags.weakis set when the browser names no device model, so your server knows that more devices share that ID. - A failed read gives no ID, not a wrong one. If a value the ID depends on can’t be read,
idisnullandflags.partialnames what failed. A wrong ID that looks valid is worse than no ID. - One phone can keep one ID across browsers. On a Pixel 10, Chrome, its incognito window and the in-app browsers of Instagram and Facebook all gave the same
id. That is the Instagram-then-Chrome shopper from earlier, recognised as one device. - Nothing leaves the page. The core reads no canvas, WebGL or audio, and sends nothing. Your code decides what goes to your server.
Recognise a returning device on your server
Store each reading, and key your visitors by did. When a visit arrives with a did you know, you’re done: it’s the same browser.
When the did is new, the stored ID was lost or the visitor is in another browser. That is when the fingerprint earns its place. The package includes a small matcher for your server that compares the new reading with the ones you already have:
import { match } from 'visitojs/match'
// reading: what the page just sent
// earlier: readings you stored before, for example for this account
const verdict = match(reading, earlier)
if (verdict.result === 'same' || verdict.result === 'likely') {
const known = earlier[verdict.index] // the device you saw before
}
The answer is one of four words:
same: the stored IDs are equal.likely: nothing contradicts, and both readings name the same device model.unknown: nothing decides. Two candidates that fit equally well areunknown, never a guess.different: every candidate contradicts the reading.
Treat likely as a score, not a verdict. Use it to ask for a second factor or to flag an order for review. Don’t ban anyone on it.
In React, Next.js or Vue
VisitoJS is a browser script, so call it on the client. In React, load it inside an effect:
useEffect(() => {
import('visitojs').then(({ get }) => get((visitor) => setVisitor(visitor)))
}, [])
Outside a browser, get calls back with null, so a server render doesn’t crash.
What you can build with it
- New-device alerts and safer logins. Ask for a second factor when an account signs in from a device it has never used. It pairs well with the login protections in my last post, where counting failed attempts per IP alone isn’t enough.
- One trial, one coupon or one vote per device. A new email address takes ten seconds to create. A new phone doesn’t.
- Fewer fake orders. Cash-on-delivery shops lose money on orders nobody collects. Seeing that one device placed ten orders under ten names is a strong signal to call before shipping.
- Honest visitor counts. A visitor who moves from an in-app browser to Chrome can be counted once.
- Bot signals.
flags.autolists up to eight observations, such asnavigator.webdriverbeingtrueor a headless user agent.
Is VisitoJS a FingerprintJS alternative?
For open-source, client-side use, yes, with a different trade-off. FingerprintJS is the best-known library, and it is MIT licensed again since version 5. It reads many more signals, canvas and WebGL included. VisitoJS reads few, keeps a stored ID next to the fingerprint, and stays small.
On 13 November 2026 I ran VisitoJS beside the published builds of three other open-source libraries, with default options, in Chromium, Firefox and WebKit:
| Check | VisitoJS 1.0.0 | FingerprintJS 5.3.0 | ThumbmarkJS 1.12.0 | get-browser-fingerprint 5.2.4 |
|---|---|---|---|---|
| Gzipped size | 7,460 bytes | 16,418 bytes | 11,567 bytes | 4,558 bytes |
| Network requests on ordinary visits | None | 1 in 136 visits in each Chromium run, none in Firefox and WebKit | None | None |
| Requests when a sampled request is due | None | 1 | 2 | None |
| Visits with a broken API where an error reached the page, of 19 | 0 | 0 | 3 | 0 |
| Same ID after a user-agent version bump | Yes | Yes | No | No in Chromium, yes in WebKit, unclear in Firefox |
| Same ID after clearing storage | Yes | Yes | Yes | Yes |
VisitoJS loses two rows, and they belong in the picture. get-browser-fingerprint is smaller. And FingerprintJS returned a fingerprint in more of the broken-API visits in Firefox and WebKit, because VisitoJS returns no fingerprint when a read it depends on fails. The sampled requests are documented by each library and can be switched off with an option. The full table has every row, the method, and the command that reproduces it.
Two things this comparison doesn’t cover. FingerprintJS and ThumbmarkJS also sell paid services with server-side processing, and those took no part. And nobody, including me, has measured on a large set of real devices how well any of these IDs tells devices apart.
Does it detect incognito mode and bots?
Partly, and it says how sure it is.
flags.private is true when the browser shows that this session’s storage isn’t kept, as in a private window. It is checked only in Safari, Firefox and the browsers built on their engines, and only when the stored ID is new. In Chrome and other Chromium browsers it is always null, which means unknown. A script on the page can fake it, so treat it as a hint for analytics, not a security control.
The fingerprint itself doesn’t care: on the phones I measured, Chrome, Brave and Safari gave the same id in normal and private windows.
flags.auto reports automation marks. Any client-side check can be patched by a careful bot, so an empty list means “none observed”, not “this is a person”. Weigh the marks on your server with everything else you know about the visit.
Is browser fingerprinting legal?
It’s regulated, so check before you ship.
In the European Union, the data protection board’s Guidelines 2/2023 say fingerprinting falls under Article 5(3) of the ePrivacy Directive. Reading a device to identify it generally needs the visitor’s consent, like a cookie does, and the directive lists no exemption for fraud prevention. The UK’s regulator takes a similar line. Many other countries have personal-data laws of their own.
For that reason VisitoJS has a deferred build. It loads without reading or storing anything and waits for your page to call start(), for example when the visitor accepts your consent banner:
import { start, get } from 'visitojs/defer'
get((visitor) => visitor && sendToServer(visitor))
banner.onAccept(() => start())
Here banner is your consent tool and sendToServer is your own code. The library collects no consent itself, and using it doesn’t make a site compliant. The project’s legal pointers list what it reads and stores and what to read first. None of this is legal advice.
What a fingerprint can’t do
- The fingerprint ID is not unique. Devices of one model with the same settings share it. Key by the stored ID first.
- No accuracy figure has been measured on enough devices to publish one. Be careful with any library that quotes a percentage without a method.
- On Brave for desktop, so little is left that the ID rests on the language and the text size alone.
flags.weaktells you. - iPhones are less tidy than the Pixel above. In one session, Safari, Chrome and Instagram’s in-app browser gave three different IDs under the first release’s rules, and that hasn’t been measured again since.
- Everything read in a browser can be faked by whoever controls the browser. A device ID is a signal for your server to weigh, not proof.
- Inside an iframe VisitoJS reads nothing and returns
null. Read the IDs in the top page and pass them down.
Short answers
Can JavaScript read the IMEI, MAC address or serial number?
No. Browsers don’t expose any hardware identifier to web pages. Only a native app can ask the operating system for one.
How do I get a unique device ID in JavaScript?
You can’t get a truly unique one. The closest is a random ID you store in a cookie and localStorage, backed by a fingerprint for when the stored one is lost.
Does a browser fingerprint work without cookies?
Yes. The fingerprint is computed from the device each time and needs no storage. Only the stored ID needs a cookie or localStorage.
Does the fingerprint change when the browser updates?
It depends on what was hashed. A fingerprint that includes the browser version changes with every update. VisitoJS keeps versions out of id and puts them in a separate idStrict.
How big is VisitoJS?
The core is 7,460 bytes gzipped. A test in the repository fails if it ever passes 8,192 bytes.
Does it work on old phones and in WebViews?
The code is ES5 and runs back to Chrome 53. It recognises the in-app browsers of Facebook, Instagram, Messenger, TikTok, Snapchat and WhatsApp.
Try it
- Open the live demo, then open it again in a private window and in another browser, and compare the IDs.
- Install it with
npm install visitojs, or read the project page. - The source, the docs and every measurement are on GitHub. Issues and pull requests are welcome.
Sources
- WebKit: Full third-party cookie blocking and more (the seven-day cap on script-writable storage)
- Chromium: User-Agent Reduction
- Brave: Fingerprint randomization
- European Data Protection Board: Guidelines 2/2023 on the technical scope of Article 5(3) of the ePrivacy Directive
- Fingerprint: FingerprintJS version 5.0 now MIT licensed
- VisitoJS: README, API reference, comparison and method