NEWStandard createTask and in.php APIs. Change the address and you are connected.View API docs
GATESOLVER

AI agents and automation

How to solve Cloudflare Turnstile in n8n

This workflow reads a Turnstile widget straight from the page HTML, gets a token from the API at $0.84 per 1,000, and finishes with the HTTP request the site expects, all inside n8n.

What you will build

The workflow fetches a page, finds the Turnstile parameters, solves the widget and submits the form. Unlike the reCAPTCHA version, the polling here happens inside a single Code node, which keeps the canvas small: seven nodes become four. Both shapes work; choose the one your team finds easier to read. The nodes are:

  • Fetch page: HTTP Request, GET the form page and keep the response headers.
  • Read widget: Code node that pulls the site key, optional action and cdata, and the session cookie.
  • Solve: Code node that creates the task and polls until the token is ready.
  • Submit form: HTTP Request that posts the form with cf-turnstile-response.

Labels and options refer to n8n 1.x at the time of writing. Check the n8n docs if your version names them differently.

Fetch the page with its headers

Add an HTTP Request node named Fetch page, method GET, URL of the form page. Under Options, set the response to include headers and status, and set the response format to text. You now get the HTML in a data field and the headers alongside it, including any set-cookie the site sends. Some sites tie the form to that cookie, so you will send it back on submit.

Read the widget parameters

A Code node in JavaScript mode can parse the HTML with regular expressions. Turnstile's site key starts with 0x4, which makes it easy to find. The node also collects the optional data-action and data-cdata attributes, because if a site sets them the task must carry them in metadata.

// Code node "Read widget", mode: Run Once for All Items
const res = $input.first().json;
const html = String(res.data ?? res.body ?? '');
const attr = (name) => {
  const hit = html.match(new RegExp(`data-${name}="([^"]+)"`));
  return hit ? hit[1] : null;
};
const sitekey = html.match(/data-sitekey="(0x4[\w-]+)"/)?.[1] ?? null;
const raw = res.headers?.['set-cookie'] ?? [];
const cookies = (Array.isArray(raw) ? raw : [raw]).map((c) => c.split(';')[0]).join('; ');
if (!sitekey) throw new Error('No Turnstile site key on this page');
return [{ json: {
  pageUrl: 'https://example.com/contact', sitekey,
  action: attr('action'), cdata: attr('cdata'), cookies,
} }];

Replace the pageUrl value with the page you fetched, or read it from an earlier node. If the widget is rendered from JavaScript and the key is not in the HTML, the guide on how to find Turnstile parameters shows where to look.

Solve inside a Code node

Recent n8n versions let Code nodes make HTTP calls with this.helpers.httpRequest. That helper is documented for the Code node at the time of writing; if your instance does not allow it, use the HTTP Request plus Wait plus IF loop from the n8n reCAPTCHA workflow instead.

// Code node "Solve"
const w = $input.first().json;
const key = $vars.GATESOLVER_KEY ?? 'YOUR_API_KEY';
const call = (path, body) => this.helpers.httpRequest({
  method: 'POST', url: `https://gatesolver.com/${path}`,
  body: { clientKey: key, ...body }, json: true,
});
const task = { type: 'AntiTurnstileTaskProxyLess', websiteURL: w.pageUrl, websiteKey: w.sitekey };
if (w.action || w.cdata) task.metadata = { action: w.action ?? undefined, cdata: w.cdata ?? undefined };
const made = await call('createTask', { task });
if (made.errorId !== 0) {
  throw new Error('Turnstile task refused: ' + made.errorCode);
}
for (let round = 0; round < 40; round++) {
  await new Promise(function (done) { setTimeout(done, 3000); });
  const st = await call('getTaskResult', { taskId: made.taskId });
  if (st.status === 'ready') return [{ json: { ...w, token: st.solution.token, ua: st.solution.userAgent } }];
  if (st.errorId || st.status === 'failed') throw new Error(`no token: ${st.errorCode ?? 'failed'}`);
}
throw new Error('Turnstile token not ready after two minutes');

Throwing an error is intentional: it marks the execution as failed, so n8n's error workflow (if you have one) can alert you. Failed tasks and tasks that stay unsolved past 120 seconds are not charged.

Send the follow-up request

Add an HTTP Request node named Submit form. Method POST, URL the form's action address, Body Content Type "Form Urlencoded". Add the form's own fields, then one more parameter:

NameValue
cf-turnstile-response{{ $json.token }}
emailyour form value
messageyour form value

Under Send Headers, add Cookie with {{ $json.cookies }} and User-Agent with {{ $json.ua }}. Open the form in your browser's developer tools once to list every field it posts, including hidden ones such as CSRF tokens, and add those too. If the site posts JSON through an API call instead, switch the body type to JSON and use the field name from that request.

Check the outcome

The submit response tells you whether the site accepted the token. Add an IF node that looks for a success marker in the body (a thank you message, a redirect status, a JSON ok) and routes failures to a notification. A rejection usually means the token sat too long, a required field was missing, or the cookie from the first fetch was not sent.

Tokens are single use

Each Turnstile token is valid for one submit and a short time. Do not store it for later runs, and do not share one token across several Submit nodes. If a workflow submits several forms, solve once per form. Tokens usually arrive under 3 seconds, so this adds little to the run time.

When the page itself is blocked

If Fetch page returns a "Just a moment" interstitial instead of the form, the site is showing the Cloudflare challenge page, not a Turnstile widget. That needs an AntiCloudflareTask with a proxy, and every later request must go through the same proxy with the returned cookie and User-Agent. See Cloudflare Challenge for its fields.

Typical permitted uses: testing your own contact forms, submitting your own data to a portal that allows automation, or QA on staging environments.

Scheduling and error handling

Once the workflow runs by hand, replace the Manual Trigger with a Schedule Trigger or a Webhook. Then set an error workflow in the workflow settings, so a thrown error in Solve or a rejected submit sends a message to your team channel or mailbox. The error message from the Code node already names the reason (createTask: ERROR_ZERO_BALANCE, for example), which makes the alert actionable without opening n8n.

Turn on the HTTP Request node's retry setting for Fetch page and Submit form with a small count and a pause of a few seconds. Network hiccups are common on long running schedules, and a retry there costs nothing. Do not put a blind retry on the Solve node; it already handles the cases worth repeating, and a key or balance error will not fix itself.

Keeping executions readable

n8n stores the data of each execution. Rename nodes so the history reads like a sentence, and avoid writing the full token into fields you do not need, since it only adds noise to saved executions.

Common questions

Do I need a proxy for Turnstile in n8n?

No. AntiTurnstileTaskProxyLess works without one. Only the Cloudflare challenge page needs a proxy.

Why poll inside a Code node instead of with a Wait node?

It keeps the workflow short. The Wait and IF loop works just as well and is easier to inspect node by node.

What if this.helpers.httpRequest is not available?

Use HTTP Request nodes with a Wait and IF loop instead, as in the n8n reCAPTCHA guide.

Why send the cookie from the first fetch?

Some forms check that the submit belongs to the same session that loaded the page, often through a CSRF cookie.

For how long can the token wait before the submit?

Only a few minutes and for one submit, so solve right before the form is posted.

Keep reading