@friendlycaptcha/server-sdk

中文文档
0.3.0发布于 2026/03/16MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 1.1万

Serverside client SDK for the Friendly Captcha V2 API

原始使用文档
文档目录12 节
  1. Installation
  2. Usage
  3. Initialization
  4. Verifying a Captcha Response
  5. Retrieving Risk Intelligence
  6. Configuration
  7. Configuration
  8. Development
  9. Install dependencies
  10. Run the tests
  11. Build for production
  12. License

friendly-captcha-javascript

NPM Version badge

A Javascript client for the Friendly Captcha service. This client allows for easy integration and verification of captcha responses with the Friendly Captcha API.

This library is for Friendly Captcha v2 only. If you are looking for V1, look here

This is the library you use in your backend code. For the code that you use in your frontend, see @friendlycaptcha/sdk.

Installation

Install using NPM

npm install @friendlycaptcha/server-sdk

Usage

Below are some basic examples of how to use the client.

For a more detailed example, take a look at the example directory.

Initialization

import { FriendlyCaptchaClient } from "@friendlycaptcha/server-sdk";

const frcClient = new FriendlyCaptchaClient({
  apiKey: "YOUR_API_KEY",
  sitekey: "YOUR_SITEKEY",
});

Verifying a Captcha Response

After calling verifyCaptchaResponse with the captcha response there are two functions on the result object that you should check:

  • wasAbleToVerify() indicates whether we were able to verify the captcha response. This will be false in case there was an issue with the network/our service or if there was a mistake in the configuration.
  • shouldAccept() indicates whether the captcha response was correct. If the client is running in non-strict mode (default) and wasAbleToVerify() returned false, this will be true.

Below are some examples of this behaviour.

Verifying a correct captcha response without issues when veryfing:

const result = await frcClient.verifyCaptchaResponse("CORRECT_CAPTCHA_RESPONSE_HERE");
console.log(result.wasAbleToVerify()); // true
console.log(result.shouldAccept()); // true

Verifying an incorrect captcha response without issues when veryfing:

const result = await frcClient.verifyCaptchaResponse("INCORRECT_CAPTCHA_RESPONSE_HERE");
console.log(result.wasAbleToVerify()); // true
console.log(result.shouldAccept()); // false

Verifying an incorrect captcha response with issues (network issues or bad configuration) when veryfing in non-strict mode (default):

const result = await frcClient.verifyCaptchaResponse("INCORRECT_CAPTCHA_RESPONSE_HERE");
console.log(result.wasAbleToVerify()); // false
console.log(result.shouldAccept()); // true

Verifying an incorrect captcha response with issues (network/service issues or bad configuration) when veryfing in strict mode:

const frcClient = new FriendlyCaptchaClient({
  ...
  strict: true,
});

const result = await frcClient.verifyCaptchaResponse("INCORRECT_CAPTCHA_RESPONSE_HERE");
console.log(result.wasAbleToVerify()); // false
console.log(result.shouldAccept()); // false

Retrieving Risk Intelligence

You can retrieve risk intelligence data for a given token. This provides detailed information about the risk profile of a request, including network data, geolocation, browser details, and risk scores.

const result = await frcClient.retrieveRiskIntelligence("RISK_INTELLIGENCE_TOKEN_HERE");

// Check if we were able to retrieve the risk intelligence data
if (result.wasAbleToRetrieve()) {
  // Check if the token is valid and data was retrieved successfully
  if (result.isValid()) {
    const response = result.getResponse();
    console.log("Risk Intelligence Data:", response.data);
  } else {
    // Token was invalid or expired
    const error = result.getResponseError();
    console.log("Error:", error?.error_code, error?.detail);
  }
} else {
  // Network issue or configuration problem
  if (result.isClientError()) {
    console.log("Configuration error - check your API key");
  } else {
    console.log("Network issue or service temporarily unavailable");
  }
}

Configuration

Configuration

The client offers several configuration options:

  • apiKey: Your Friendly Captcha API key.
  • sitekey: Your Friendly Captcha sitekey.
  • strict: (Optional) In case the client was not able to verify the captcha response at all (for example if there is a network failure or a mistake in configuration), by default verifyCaptchaResponse returns true regardless. By passing strict: true, it will return false instead: every response needs to be strictly verified.
  • apiEndpoint: (Optional) The endpoint domain for the site verification API. Shorthands eu or global are also accepted. Default is global.
  • fetch: (Optional) The fetch implementation to use. Defaults to globalThis.fetch.

Development

Install dependencies

npm install

Run the tests

First run the SDK Test server, then run npm test.

docker run -p 1090:1090 friendlycaptcha/sdk-testserver:latest

npm test

Build for production

npm run build:dist

License

Open source under MIT.

版本与发布记录

@friendlycaptcha/server-sdk 共发布 6 个版本,首次发布于 2024/01/08;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.3.0latest2026/03/168 个月前
0.2.12026/02/178 个月前
0.2.02026/02/059 个月前
0.1.22024/11/182 年前
0.1.12024/10/212 年前
0.1.02024/01/082 年前

关于 @friendlycaptcha/server-sdk 的常见问题

@friendlycaptcha/server-sdk 是什么?

Serverside client SDK for the Friendly Captcha V2 API

如何安装 @friendlycaptcha/server-sdk?

在项目目录运行 npm install @friendlycaptcha/server-sdk,或使用 pnpm add / yarn add / bun add。

@friendlycaptcha/server-sdk 支持 TypeScript 吗?

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

@friendlycaptcha/server-sdk 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@friendlycaptcha/server-sdk 可以商用吗?

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

@friendlycaptcha/server-sdk 的下载量有多少?

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

@friendlycaptcha/server-sdk 还在维护吗?

最新版本 0.3.0 发布于 2026/03/16(8 个月前);至今共发布 6 个版本,其中过去 12 个月发布了 3 个;首次发布于 2024/01/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。