SimpleCaptcha is a lightweight CAPTCHA provider for protecting forms and APIs from automated submissions.
It can be embedded directly into your website using an
<iframe>.
Add the following iframe to the page where you want the CAPTCHA challenge to appear:
<iframe
id="captcha-frame"
src="https://captcha.apistack.eu/captcha.html"
title="CAPTCHA"
style="
width: 100%;
max-width: 440px;
height: 170px;
border: 0;
"
></iframe>
Create a Captcha object and use
getResponse() to request the current
CAPTCHA token.
const CAPTCHA_ORIGIN = "https://captcha.apistack.eu";
const captchaFrame =
document.getElementById("captcha-frame");
function Captcha(iframe) {
return {
getResponse() {
return new Promise((resolve) => {
const handler = (event) => {
if (event.origin !== CAPTCHA_ORIGIN) {
return;
}
if (event.source !== iframe.contentWindow) {
return;
}
if (
event.data?.type !==
"SimpleCaptcha-status"
) {
return;
}
window.removeEventListener(
"message",
handler
);
resolve(event.data.token);
};
window.addEventListener(
"message",
handler
);
iframe.contentWindow.postMessage(
{
type: "SimpleCaptcha-status"
},
CAPTCHA_ORIGIN
);
});
}
};
}
window.captcha = Captcha(captchaFrame);
You can then request the current CAPTCHA token:
<button
type="button"
onclick="
window.captcha
.getResponse()
.then(token =>
alert(token || 'CAPTCHA not completed')
)
"
>
Get CAPTCHA token
</button>
When the user submits your form, request the current CAPTCHA response before sending the request to your backend:
const captchaToken =
await window.captcha.getResponse();
if (!captchaToken) {
// CAPTCHA has not been completed
return;
}
The resulting token should then be included in your API request.
The captcha_token should be sent to your
backend along with the user's form data.
Your backend should then verify the token with SimpleCaptcha before accepting the request.
Do not rely on the valid value received
by the browser as proof that the CAPTCHA was completed.
The token should be validated server-side.
You can interact with the API as shown in the following Python example:
try:
async with httpx.AsyncClient(timeout=10.0) as client:
captcha_response = await client.post(
"https://captcha.apistack.eu/api/verify_token",
json={
"captcha_token": data.captcha_token,
},
)
except httpx.RequestError as exc:
print("CAPTCHA REQUEST ERROR:", repr(exc))
raise HTTPException(
status_code=502,
detail={
"code": "CAPTCHA_SERVICE_UNAVAILABLE",
"message": "CAPTCHA verification service is unavailable.",
},
)
# Check HTTP status before parsing the response
if captcha_response.status_code != 200:
raise HTTPException(
status_code=502,
detail={
"code": "CAPTCHA_SERVICE_ERROR",
"message": "CAPTCHA verification service returned an error.",
},
)
# Parse CAPTCHA response
try:
captcha_data = captcha_response.json()
except ValueError:
raise HTTPException(
status_code=502,
detail={
"code": "CAPTCHA_INVALID_RESPONSE",
"message": "CAPTCHA verification service returned an invalid response.",
},
)
# Check CAPTCHA result
if not captcha_data.get("valid", False):
raise HTTPException(
status_code=400,
detail={
"code": "INVALID_CAPTCHA",
"message": "Invalid or expired CAPTCHA token.",
},
)
If you use a React frontend, you can use the following:
import { useRef } from "react";
const CAPTCHA_ORIGIN =
"https://captcha.apistack.eu";
function Captcha(iframe) {
return {
getResponse() {
return new Promise((resolve) => {
const handler = (event) => {
if (
event.origin !== CAPTCHA_ORIGIN
) {
return;
}
if (
event.source !==
iframe.contentWindow
) {
return;
}
if (
event.data?.type !==
"SimpleCaptcha-status"
) {
return;
}
window.removeEventListener(
"message",
handler
);
resolve(event.data.token);
};
window.addEventListener(
"message",
handler
);
iframe.contentWindow.postMessage(
{
type:
"SimpleCaptcha-status",
},
CAPTCHA_ORIGIN
);
});
},
};
}
Then, in your handleSubmit() function:
if (!captchaFrameRef.current) {
return;
}
const captcha =
Captcha(captchaFrameRef.current);
const token =
await captcha.getResponse();
Add the ref to your CAPTCHA iframe:
<iframe
ref={captchaFrameRef}
src="https://captcha.apistack.eu/captcha.html"
title="CAPTCHA"
/>
Define the ref in your React component:
const captchaFrameRef =
useRef(null);
An API key is required to use the API. You can request one using the Request API Key tab.
Complete the CAPTCHA below and then click Get CAPTCHA Token to retrieve the current CAPTCHA token.
Enter your email address below. After completing the CAPTCHA, we will send you a verification link by email.