Skip to main content
Version: v2

Add Friendly Captcha with an AI agent

This page is for AI coding agents that add Friendly Captcha to a project. If you're a person, point your agent here and give it your sitekey.

You'll protect a form on the frontend and the backend. The widget alone doesn't stop bots. They're only stopped once the backend verifies the response.

Before you start​

You need the sitekey and the API key. The sitekey starts with FC and is public, so it can go in the code. The API key is secret. Don't ask for it in the chat and don't write it into the code. Load it the way the project already loads its other secrets, like an environment variable, a secrets manager, or a config file that stays out of git. If the project has no convention yet, use an FRC_API_KEY environment variable and add it to .env.example or wherever the project documents its config.

Ask whether the app uses the EU endpoint. If it does, set the endpoint to eu in the widget and in the server SDK.

Look at the project's framework and language first, and follow its conventions.

Use v2, not v1​

This page covers Friendly Captcha v2. You may know the v1 packages from older code. Don't use them.

v2 (use this)v1 (don't use)
Frontend package@friendlycaptcha/sdkfriendly-challenge
Form fieldfrc-captcha-responsefrc-captcha-solution
Verify endpoint/api/v2/captcha/siteverify/api/v1/siteverify

If the project already uses v1, follow Upgrading from v1 instead.

Add the widget​

With a bundler or a framework like React, Vue, or Svelte, install the npm package and create the widget in code, as shown in Install the widget. For React and Vue, the React and Vue pages have a ready-made component.

npm install @friendlycaptcha/sdk

Without one, add the scripts and put the widget element inside the form.

<script type="module" src="https://cdn.jsdelivr.net/npm/@friendlycaptcha/sdk@1.1.5/site.min.js" async defer></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/@friendlycaptcha/sdk@1.1.5/site.compat.min.js" async defer></script>

<div class="frc-captcha" data-sitekey="<sitekey>"></div>

For the EU endpoint, add data-api-endpoint="eu" to the element, or pass apiEndpoint: "eu" to new FriendlyCaptchaSDK() or createWidget().

The widget adds a hidden frc-captcha-response field to the form. Once it's done, the field holds the response your backend verifies.

Forms sent with JavaScript​

The hidden field only reaches the backend when the browser submits the form itself. Most React, Vue, and Svelte apps send forms with fetch instead, so add the response to the request yourself.

Read it with widget.getResponse(), or listen for the frc:widget.complete event and use event.detail.response. createWidget() returns the widget. For widgets created from the HTML element, window.frcaptcha.getAllWidgets() returns them. Send the response as frc-captcha-response, next to the other fields the backend reads.

Until the widget is done, getResponse() returns a placeholder that the backend rejects. Call widget.reset() after each submission. A response can only be verified once, so sending it again fails.

import { FriendlyCaptchaSDK } from "@friendlycaptcha/sdk";

const form = document.querySelector("#contact-form");
const sdk = new FriendlyCaptchaSDK();
const widget = sdk.createWidget({ element: form.querySelector(".frc-captcha"), sitekey: "<sitekey>" });

form.addEventListener("submit", async (event) => {
event.preventDefault();
try {
await fetch("/contact", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: form.elements.message.value, "frc-captcha-response": widget.getResponse() }),
});
} finally {
widget.reset();
}
});

Put the widget element inside the form, even if the form is never submitted natively. By default the widget starts when something in its form gets focus. Without a form around it, it waits until the user clicks it.

Events lists all widget events, and Lifecycle explains the widget's states.

Verify the response on the backend​

In the handler that receives the form, read frc-captcha-response and check it with the official server SDK for the backend's language.

LanguagePackageInstall
JavaScript and TypeScript@friendlycaptcha/server-sdknpm install @friendlycaptcha/server-sdk
PHPfriendlycaptcha/sdkcomposer require friendlycaptcha/sdk
Pythonfriendly-captcha-clientpip install friendly-captcha-client
Gofriendly-captcha-gogo get github.com/friendlycaptcha/friendly-captcha-go

Give the client the API key, the sitekey, and the EU endpoint if the app uses it. Each SDK's README lists the options.

Reject the submission when the SDK says to. If the frc-captcha-response field is missing, pass an empty string instead. The SDKs reject an empty response, but the JavaScript SDK accepts a missing one the same way it accepts submissions during an outage. When the SDK couldn't verify at all, log an error. That points to a wrong API key, a config mistake, or an outage. The SDK accepts the submission in that case, so real users can still send the form while it's fixed.

Here's the JavaScript SDK as an example, with the key in an FRC_API_KEY environment variable.

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

const frc = new FriendlyCaptchaClient({
apiKey: process.env.FRC_API_KEY,
sitekey: "<sitekey>",
});

// In the handler that receives the form:
const result = await frc.verifyCaptchaResponse(req.body["frc-captcha-response"] ?? "");
if (!result.wasAbleToVerify()) {
console.error("Friendly Captcha verification failed", result.getErrorCode());
}
if (result.shouldReject()) {
// Reject the submission.
}

If the language has no SDK, send the request yourself as described in Verify the response. Use https://global.frcapi.com/api/v2/captcha/siteverify, or https://eu.frcapi.com/api/v2/captcha/siteverify for the EU endpoint.

Sites on a CMS​

For WordPress, TYPO3, Magento, or another CMS or shop system, use its Friendly Captcha plugin instead of writing code. The integrations page lists them.

Check that it works​

Load the page with the form and click into it. The widget should start solving. Depending on the app's widget mode, it finishes on its own or asks for a click. Submit the form and check that the backend accepts it. Then submit it without the frc-captcha-response field and check that the backend rejects it.

For automated tests, see Automated testing.

When you're done​

Tell the user which files you changed, where to put the API key, and how to test it.