@fingerprintjs/fingerprintjs-pro-spa

中文文档
1.3.3发布于 2025/09/15MITTypeScriptESM + CJS近 30 天下载 76.5万

FingerprintJS Pro JavaScript agent for Single-Page Applications (SPA)

原始使用文档
文档目录14 节
  1. Table of Contents
  2. Requirements
  3. Installation
  4. Getting Started
  5. 1. Get your Fingerprint Pro Public API key
  6. 2. Create the client
  7. 3. Initialize the JS Agent
  8. 4. Identify visitors
  9. Caching
  10. Creating a custom cache
  11. Cache time
  12. Support and feedback
  13. Documentation
  14. License

Fingerprint logo

Current NPM version coverage Monthly downloads from NPM MIT license Discord server Documentation

Fingerprint Pro SPA

Fingerprint is a device intelligence platform offering industry-leading accuracy

This library is a framework-agnostic wrapper around the Fingerprint Pro JavaScript Agent, adding multiple built-in caching mechanisms with recommended default settings.

  • If you just need the Fingerprint Pro JS agent without caching, you can use it directly.
  • If you're looking for a framework-specific integration, we have dedicated SDKs for React (including Next, Preact), Vue, Svelte and Angular.
  • If you have a single-page web application that needs caching, but also more low-level control over the agent than framework-specific SDKs provide, this library is for you.

[!NOTE] This library assumes you have a Fingerprint Pro subscription or trial, it is not compatible with the source-available FingerprintJS. See our documentation to learn more about the differences between Fingerprint Pro and FingerprintJS.

Table of Contents

Requirements

  • For TypeScript users: Typescript 4.5 or higher

Installation

Using npm:

npm install @fingerprintjs/fingerprintjs-pro-spa

Using yarn:

yarn add @fingerprintjs/fingerprintjs-pro-spa

Using pnpm:

pnpm add @fingerprintjs/fingerprintjs-pro-spa

Getting Started

1. Get your Fingerprint Pro Public API key

In order to identify visitors you'll need a Fingerprint Pro account (you can sign up for free).

  • Go to Fingerprint Dashboard > App settings > API Keys.
  • Find your Public API key.

2. Create the client

Create a FpjsClient instance before rendering or initializing your application. You should only have one instance of the client. You need to specify your public API key and other configuration options based on your chosen region and active integration.

import { FpjsClient, FingerprintJSPro } from '@fingerprintjs/fingerprintjs-pro-spa'

const fpjsClient = new FpjsClient({
  // You can also pass these options later in `.init()` method
  loadOptions: {
    apiKey: '<PUBLIC_API_KEY>',
    endpoint: [
      //'https://metrics.yourwebsite.com',
      FingerprintJSPro.defaultEndpoint,
    ],
    scriptUrlPattern: [
      // 'https://metrics.yourwebsite.com/web/v<version>/<apiKey>/loader_v<loaderVersion>.js',
      FingerprintJSPro.defaultScriptUrlPattern,
    ],
    // region: "eu"
  },
})

[!NOTE] You must provide loadOptions containing your public API key either in the constructor or in the init method. If you don't, the SDK will throw an error. You can learn more about different load options here in the JS Agent documentation.

3. Initialize the JS Agent

Before you start making identification requests to the Fingerprint Pro API, you need to initialize the JS Agent. This downloads the latest client-side logic from Fingerprint CDN. Call init() before the getVisitorData() method to avoid errors.

// with async/await
await fpjsClient.init()

const visitorData = await fpjsClient.getVisitorData()

// with promises
const visitorData = fpjsClient.init().then(() => {
  return fpjsClient.getVisitorData()
})

You can also pass the loadOptions into the init method here. They will be merged with the options passed to the constructor.

await fpjsClient.init({
  apiKey: '<PUBLIC_API_KEY>',
  // endpoint: ["<CUSTOM_ENDPOINT>", FingerprintJSPro.defaultEndpoint],
  // scriptUrlPattern: ["<CUSTOM_SCRIPT_URL>", FingerprintJSPro.defaultScriptUrlPattern],
  // region: "eu"
})

4. Identify visitors

The getVisitorData method returns visitor identification data based on the request options. Set ignoreCache to true to call the API even if the data is present in the cache.

// with async/await
const visitorData = await fpjsClient.getVisitorData({ extendedResult: true }, false)

// with promises
const visitorData = fpjsClient.getVisitorData({ extendedResult: true }).then((visitorData) => {
  // use visitor data in your fraud prevention logic
  checkIfFingerprintIsFraudulent(visitorData.visitorId) // this method is just an example, this SDK doesn't actually supply it
})

See the JS Agent API reference for more details.

Linking and tagging information

The visitorId provided by Fingerprint Identification is especially useful when combined with information you already know about your users, for example, account IDs, order IDs, etc. To learn more about various applications of the linkedId and tag, see Linking and tagging information.

const visitorData = await fpjsClient.getVisitorData({
  linkedId: 'user_1234',
  tag: {
    userAction: 'login',
    analyticsId: 'UA-5555-1111-1',
  },
})

Caching

Fingerprint Pro usage is billed per API call. To avoid unnecessary API calls, it is a good practice to cache identification results. The SDK provides three ways to cache visitor data out of the box:

  • Session storage (default) - sessionStorage
  • Local storage - localStorage
  • Memory - memory
  • No cache - nocache

You can specify the cacheLocation option when creating the FpjsClient:

const fpjsClient = new FpjsClient({
  loadOptions: {
    apiKey: 'your-fpjs-public-api-key',
  },
  cacheLocation: 'localstorage',
  // You can also use the provided TypeScript enum
  // cacheLocation: CacheLocation.LocalStorage
})

Cache keys are based on the combination of GetOptions. For example, API responses for calls with extendedResult: true and extendedResult: false are stored independently.

[!NOTE] If you use data from extendedResult, pay additional attention to your caching strategy. Some fields, for example, ip or lastSeenAt, might change over time for the same visitor.

You can ignore the cached result for a specific API call and using { ignoreCache: true }:

const visitorData = await fpjsClient.getVisitorData({ ignoreCache: true })

Check if your response was retrieved from cache using the returned cacheHit flag:

const { cacheHit, ...visitorData } = await fpjsClient.getVisitorData()

Use getVisitorDataFromCache to directly retrieve responses from cache:

// Checks if request matching given options is present in cache
await fpjsClient.isInCache({ extendedResult: true })

// Returns cached visitor data based on the request options, or undefined if the data is not present in cache
const cachedResult = await fpjsClient.getVisitorDataFromCache({ extendedResult: true })

You can also use your custom cache implementation as described below.

Creating a custom cache

The SDK can use a custom cache store implemented inside your application. This is useful when a different data store is more convenient in your environment, such as a hybrid mobile app.

You can provide an object to the cache property of the SDK configuration that implements the following functions. All the functions can return a Promise or a static value.

SignatureReturn typeDescription
get(key)PromiseReturns the item from the cache with the specified key, or undefined if it was not found
set(key: string, object: any) Promise or voidSets an item into the cache
remove(key)Promise or voidRemoves a single item from the cache at the specified key, or no-op if the item was not found
allKeys()Promise<string[]> or string []Returns the list of all keys. By default, the keys we use are prefixed with @fpjs@client@ but you can pass your own custom prefix as an option when you create the FpjsClient

[!NOTE] The cache property takes priority over cacheLocation if both are set. A warning is displayed in the console if that happens.

We export the internal InMemoryCache, LocalStorageCache, SessionStorageCache, and CacheStub implementations, so you can wrap your custom cache around these implementations if you wish.

Cache time

Use the cacheTimeInSeconds client constructor option to set a custom cache time. To ensure high identification accuracy we recommend not to cache visitors data for longer than 24 hours. If you pass a value higher than 86400 (60 x 60 x 24), the FpjsClient constructor will throw an error.

Support and feedback

To report problems, ask questions, or provide feedback, please use Issues. If you need private support, you can email us at [email protected].

Documentation

This library uses Fingerprint Pro under the hood.

License

This project is licensed under the MIT license. See the LICENSE file for more information.

版本与发布记录

@fingerprintjs/fingerprintjs-pro-spa 共发布 29 个版本,首次发布于 2022/02/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.3latest2025/09/151.2 年前
1.3.22024/09/112 年前
1.3.12024/03/212 年前
1.3.1-test.1test2024/03/212 年前
1.3.02023/11/222 年前
1.2.22023/11/162 年前

关于 @fingerprintjs/fingerprintjs-pro-spa 的常见问题

@fingerprintjs/fingerprintjs-pro-spa 是什么?

FingerprintJS Pro JavaScript agent for Single-Page Applications (SPA)

如何安装 @fingerprintjs/fingerprintjs-pro-spa?

在项目目录运行 npm install @fingerprintjs/fingerprintjs-pro-spa,或使用 pnpm add / yarn add / bun add。

@fingerprintjs/fingerprintjs-pro-spa 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fingerprintjs/fingerprintjs-pro-spa 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fingerprintjs/fingerprintjs-pro-spa 可以商用吗?

@fingerprintjs/fingerprintjs-pro-spa 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fingerprintjs/fingerprintjs-pro-spa 的下载量有多少?

根据 npm 官方下载统计,@fingerprintjs/fingerprintjs-pro-spa 在 2026/10/15 – 2026/11/13 这 30 天内被下载 765,028 次,日均约 25,501 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fingerprintjs/fingerprintjs-pro-spa 还在维护吗?

最新版本 1.3.3 发布于 2025/09/15(1.2 年前);至今共发布 29 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/02/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。