Cloudflare Turnstile solver · Puppeteer
Make captchas disappear.
Solve Cloudflare Turnstile in Puppeteer
Solve the page's Cloudflare Turnstile through the API and hand the token to Puppeteer.
A Puppeteer script on a site you may automate can finish a Turnstile widget in four steps: read the site key, action and cData, ask ZeroCaptcha for a token, set the form field, submit. The sample uses the JavaScript client, which is coming to npm; until then, the Node.js page makes the same calls with fetch.
Sample uses a coming clientNeeds Node.js 20 or later, puppeteer and @zerocaptcha/sdkPlain HTTP today: the Node.js guide
The steps in Puppeteer
- Get an API key
Sign up with an email and a password, create your key in the dashboard and add funds in crypto, from $10.
- Read the widget's site key, action and cData
Open the page in Puppeteer and read data-sitekey, data-action and data-cdata from the Turnstile element, or the sitekey, action and cData options of its turnstile.render() call.
- Solve it with ZeroCaptcha
Send the page's URL, the site key, and the action and cData when the widget sets them, to ZeroCaptcha and wait for the token. Many sites check both when they verify it.
- Put the token in the form
Set the cf-turnstile-response field to the token, then submit the form as a person would, within 300 seconds.
// Fill a Cloudflare Turnstile widget in Puppeteer with a token from ZeroCaptcha, on a page you may automate.
// Needs Node.js 20 or later, puppeteer and @zerocaptcha/sdk:
// ZEROCAPTCHA_API=https://api.zerocaptcha.io ZEROCAPTCHA_KEY=zc_live_... node fill-turnstile.mjs
import puppeteer from "puppeteer";
import { TaskFailedError, ZeroCaptcha } from "@zerocaptcha/sdk";
const zerocaptcha = new ZeroCaptcha({
apiKey: process.env.ZEROCAPTCHA_KEY,
baseUrl: process.env.ZEROCAPTCHA_API,
});
const browser = await puppeteer.launch();
const page = await browser.newPage();
// A widget rendered by script takes its settings from turnstile.render(container, options):
// note each call's options before the page's own scripts run.
await page.evaluateOnNewDocument(() => {
let api;
Object.defineProperty(window, "turnstile", {
configurable: true,
get: () => api,
set(value) {
const render = value.render;
value.render = (container, options = {}) => {
window.__turnstileRenders = [...(window.__turnstileRenders ?? []), options];
return render.call(value, container, options);
};
api = value;
},
});
});
await page.goto("https://shop.example.com/login", { waitUntil: "networkidle2" });
// The widget's site key, action and cData: its data-sitekey, data-action and data-cdata
// attributes, or the sitekey, action and cData it was rendered with. Many sites check the action
// and cData when they verify the token, so send them exactly as the widget sets them.
const widget = await page.evaluate(() => {
const element = document.querySelector("[data-sitekey]");
const rendered = window.__turnstileRenders?.[0] ?? {};
return {
websiteKey: element?.dataset.sitekey ?? rendered.sitekey,
action: element?.dataset.action ?? rendered.action,
cdata: element?.dataset.cdata ?? rendered.cData,
callback: element?.dataset.callback,
};
});
let token;
try {
// The client sends an Idempotency-Key with the create, so its own retries never make two tasks.
token = await zerocaptcha.solve({
websiteURL: page.url(),
websiteKey: widget.websiteKey,
action: widget.action, // left out of the request when the widget sets none
cdata: widget.cdata,
// proxy: "http://user:pass@proxy.example.net:8080", // to solve through your own proxy
});
} catch (error) {
// A task that fails or expires costs nothing; its code says why.
if (error instanceof TaskFailedError) throw new Error(`Not solved: ${error.code}`);
throw error;
}
// Put the token where the widget would: its cf-turnstile-response field, then its callback.
await page.evaluate(
(value, callback) => {
for (const input of document.querySelectorAll('[name="cf-turnstile-response"]')) input.value = value;
if (callback && typeof window[callback] === "function") window[callback](value);
},
token,
widget.callback,
);
await Promise.all([page.waitForNavigation(), page.click('button[type="submit"]')]);
await browser.close();- File
- fill-turnstile.mjs 73 lines
- Needs
- Node.js 20 or later, puppeteer and @zerocaptcha/sdk
- Run
node fill-turnstile.mjs
New to the API? The quickstart walks through sign-up, the key and the first task.
Good to know
- Send the widget's action and cData with the task whenever it sets them: many sites refuse a token whose action or cData does not match.
- Solve right before you submit: the token works once, within 300 seconds.
- Some pages read the token from the widget's callback rather than the form field. On those, call the page's own callback with the token.
{
"errorId": 0,
"taskId": "0192a6c4-7f1e-7b2a-9d41-3c5e8a10f2b7",
"status": "ready",
"solution": {
"token": "0.Kx8mR2…vQ",
"type": "turnstile"
},
"cost": "0.000400",
"createTime": 1791537240,
"endTime": 1791537243,
"solveCount": 1,
"expiresAt": "2026-10-09T09:19:03Z"
}Use the token straight away: it works once, for 300 seconds. cost is what you paid, in US dollars.
{
"errorId": 0,
"taskId": "0192a6c4-7f1e-7b2a-9d41-3c5e8a10f2b7",
"status": "processing"
}Ask again in a second or two. Nothing is charged until a token is ready.
{
"errorId": 1,
"errorCode": "ERROR_CAPTCHA_UNSOLVABLE",
"errorDescription": "Every attempt to solve the challenge failed. Nothing was charged.",
"taskId": "0192a6c4-7f1e-7b2a-9d41-3c5e8a10f2b7",
"status": "failed",
"cost": "0.000000"
}A failed task costs nothing. Every code, and what to do about it, is in the errors reference.
Read next
- Cloudflare TurnstileSubmit a Cloudflare Turnstile Token: Form Fields and CallbacksWhere a solved Turnstile token goes: the cf-turnstile-response field, the widget's callback, or a JSON body. With examples for forms, fetch and browsers.
- Cloudflare TurnstileCloudflare Turnstile action and cData: When a Task Needs ThemWhat Turnstile's action and cData parameters do, where to find them in a page, and how to pass them in createTask, in.php or REST so the token is accepted.
- Cloudflare TurnstileCloudflare Turnstile Modes: Managed, Non-Interactive, InvisibleCloudflare Turnstile's three widget modes compared: what visitors see in each, how to tell which one a page uses, and why a solving task is the same for all.
FAQ
Puppeteer questions
Where does the token go?
Where the widget puts it: the hidden cf-turnstile-response field of the form, or the callback the page registered. Then submit as a person would.
How long does a Cloudflare Turnstile token last?
A Cloudflare Turnstile token works once and expires 300 seconds after it is issued, so solve right before you submit. Every result tells you when its token expires.
What does a failed task cost?
Nothing. The price is held when you create a task and released at once if it fails or expires, and a refused task holds nothing; you pay only when a token is ready.
ZeroCaptcha You build. We solve.
Get your first Cloudflare Turnstile token
Create an account, add $10 and run your first task.