Metadata-Version: 2.5
Name: pydoll-turnstile
Version: 0.1.1
Summary: Solve Cloudflare Turnstile in pydoll via the Peak API: read the sitekey, get a token, inject it. Async.
Project-URL: Homepage, https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=pydoll-turnstile
Project-URL: Documentation, https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=pydoll-turnstile
Project-URL: Pricing, https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=pydoll-turnstile#pricing
Project-URL: Source, https://github.com/CircuitSavage/pydoll-turnstile
Author: Peak
License: MIT
License-File: LICENSE
Keywords: automation,bypass,captcha,cf-clearance,cloudflare,pydoll,scraping,solver,turnstile
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Internet :: WWW/HTTP :: Browsers
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.10
Provides-Extra: pydoll
Requires-Dist: pydoll-python>=2.0; extra == 'pydoll'
Description-Content-Type: text/markdown

<a href="https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=pydoll-turnstile">
  <img src="https://raw.githubusercontent.com/CircuitSavage/pydoll-turnstile/master/assets/peak-banner.png" alt="Peak: a solver API for Cloudflare Turnstile and the 5-second challenge" width="100%">
</a>

# pydoll-turnstile

**pydoll-turnstile** solves Cloudflare Turnstile on a [pydoll](https://github.com/autoscrape-labs/pydoll) tab. It reads the sitekey, gets a token from Peak, and writes it into the widget so the protected form or navigation proceeds.

## Why

pydoll drives Chrome over the DevTools Protocol with no webdriver, and it ships its own Turnstile helper, `tab.expect_cloudflare_turnstile()`, which clicks the checkbox for you. On a clean residential IP that click is often enough.

On a datacenter IP it often isn't. The challenge renders, the click lands, and no token arrives, because Cloudflare scores the IP and the session before it scores the click. For those pages this package asks Peak for a token and injects it as the `cf-turnstile-response` value, then calls the widget's success callback.

## Powered by Peak

This package uses [Peak](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=pydoll-turnstile) to solve Turnstile.

- $0.90 per 1,000 successful Turnstile solves, down to $0.35 at volume
- A failed solve is not billed
- 1,000 free solves to start, no card

→ [Get your free API key](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=pydoll-turnstile) · [Docs](https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=pydoll-turnstile) · [Pricing](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=pydoll-turnstile#pricing)

## Install

```bash
pip install pydoll-turnstile pydoll-python
```

Use 0.1.1 or later. Version 0.1.0 does not work with pydoll (see Troubleshooting).

## Quickstart

```python
import asyncio
import os
from pydoll.browser import Chrome
from pydoll_turnstile import solve_turnstile

os.environ["PEAK_API_KEY"] = "pk_your_api_key"


async def main():
    async with Chrome() as browser:
        tab = await browser.start()
        await tab.go_to("https://protected.example/login")

        # Reads the sitekey, solves via Peak, injects the token.
        await solve_turnstile(tab)

        # the form / navigation can now proceed


asyncio.run(main())
```

## How it works

1. **Read the sitekey.** An `execute_script` DOM scan finds the `data-sitekey` attribute, or a `0x...` sitekey set through a JS `render()` call in the page source.
2. **Solve via Peak.** `POST https://api.peak.fo/solve` with `task_type: "turnstiletask"`, the sitekey, and the tab's current URL. The call runs in a worker thread so the event loop stays free. Pass `proxy=` to mint the token from your browser's egress IP.
3. **Inject the token.** `execute_script` sets every `cf-turnstile-response` field, fires `input`/`change`, and calls the function named in the widget's `data-callback` attribute.

## API

```python
await solve_turnstile(
    tab,                  # a pydoll tab with execute_script
    api_key=None,         # defaults to env PEAK_API_KEY
    proxy=None,           # optional http://user:pass@ip:port, forwarded to Peak
    sitekey=None,         # override auto-detection (e.g. widget in a frame)
    url=None,             # defaults to the tab's current URL
    task_type="turnstiletask",
    timeout=180.0,
) -> str                  # the injected token
```

`read_sitekey(tab)`, `inject_token(tab, token)`, and `current_url(tab)` are exposed for finer control. `inject_token` returns `True` when it found and called a `data-callback` function, and `False` when it only filled the field.

The API key is read from `PEAK_API_KEY` by default; you can also pass it explicitly. Never hardcode a real key.

## Troubleshooting

Every failure raises `PeakError` with a string you can grep for. The last three cases raise nothing.

- **Every solve fails on 0.1.0.** pydoll's `execute_script` returns the whole CDP response (`{"id": ..., "result": {"result": {"value": ...}}}`). Version 0.1.0 unwrapped one level too few and sent that dictionary to Peak as the sitekey and the URL. 0.1.1 fixes the unwrap; it was checked against pydoll 3.0.1 on a live Chrome. Run `pip install -U pydoll-turnstile`.
- **`PeakError: No Turnstile sitekey found on the page.`** The call ran before the widget rendered, or the widget sits in a frame or a closed shadow root the DOM scan can't see. Wait for the `challenges.cloudflare.com` iframe first, or pass `sitekey=`. [How to find a Cloudflare Turnstile sitekey](https://blog.peak.fo/how-to-find-a-cloudflare-turnstile-sitekey/) shows where sites hide it.
- **`PeakError: No Peak API key.`** Neither `api_key=` nor `PEAK_API_KEY` held a key. Keys start with `pk_`; the free one comes from the [dashboard](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=pydoll-turnstile).
- **`PeakError: Peak request failed: ...`** The HTTPS call to `api.peak.fo` failed on your side: DNS, TLS, a firewall, or the `timeout=`. The `proxy=` argument is forwarded to Peak for the solve and is not used for this call. `urllib` does honour an `HTTPS_PROXY` variable in the environment.
- **`PeakError:` followed by Peak's own reason, or `Peak solve failed`.** Peak answered with `success: false`. The usual causes are an unknown key, an empty balance, a sitekey that does not belong to the page URL, or a `proxy=` Peak could not connect through. The [Cloudflare Turnstile error codes](https://blog.peak.fo/cloudflare-turnstile-error-codes/) post covers the widget-side codes (110200, 300030, 600010 and the rest).
- **The token came back and the form still does nothing.** `inject_token` returned `False`: the field is filled, but the site passed a closure to `turnstile.render({callback: ...})`, and a closure has no global name to call. Submit the form yourself with pydoll, or call the site's handler with the token through `tab.execute_script`. [cf-turnstile-response is empty](https://blog.peak.fo/cf-turnstile-response-empty/) and [the cf-turnstile-response token explained](https://blog.peak.fo/the-cf-turnstile-response-token-explained/) cover the field.
- **The submit returns 403 or the form page again.** The site refused the token. A token works once and expires 300 seconds after Cloudflare issues it. Pass `proxy=` with the browser's own egress so Peak mints the token from the address that submits it. [Cloudflare Turnstile 403](https://blog.peak.fo/cloudflare-turnstile-403-scraper-fix/) walks through the refusal.
- **The page shows "Just a moment..." and has no widget.** That page is the managed 5-second challenge, a separate check from the Turnstile widget. It wants a `cf_clearance` cookie, and this package injects widget tokens. [Turnstile token vs cookie](https://blog.peak.fo/cloudflare-turnstile-token-vs-cookie/) explains the difference, and [the Cloudflare 5-second challenge explained](https://blog.peak.fo/the-cloudflare-5-second-challenge-explained/) covers the interstitial.

Two longer reads: [How to scrape Cloudflare-protected sites without getting blocked](https://blog.peak.fo/how-to-scrape-cloudflare-protected-sites-without-getting-blocked/) and the [2026 Turnstile developer guide](https://blog.peak.fo/how-to-solve-cloudflare-turnstile-in-2026-dev-guide/).

## Related

Other CDP and browser wrappers for the same API: [nodriver-turnstile](https://github.com/CircuitSavage/nodriver-turnstile) · [drissionpage-turnstile](https://github.com/CircuitSavage/drissionpage-turnstile) · [patchright-turnstile](https://github.com/CircuitSavage/patchright-turnstile) · [playwright-turnstile](https://github.com/CircuitSavage/playwright-turnstile) · [camoufox-turnstile](https://github.com/CircuitSavage/camoufox-turnstile) · [selenium-turnstile](https://github.com/CircuitSavage/selenium-turnstile) · [botasaurus-turnstile](https://github.com/CircuitSavage/botasaurus-turnstile) · [puppeteer-extra-plugin-turnstile](https://github.com/CircuitSavage/puppeteer-extra-plugin-turnstile) · [crawlee-turnstile](https://github.com/CircuitSavage/crawlee-turnstile). No browser: [cloudscraper-turnstile](https://github.com/CircuitSavage/cloudscraper-turnstile) · [turnstile-curl](https://github.com/CircuitSavage/turnstile-curl) · [scrapy-turnstile](https://github.com/CircuitSavage/scrapy-turnstile) · [scrapling-turnstile](https://github.com/CircuitSavage/scrapling-turnstile). Agents: [browser-use-turnstile](https://github.com/CircuitSavage/browser-use-turnstile) · [crewai-turnstile](https://github.com/CircuitSavage/crewai-turnstile) · [langchain-turnstile](https://github.com/CircuitSavage/langchain-turnstile). Full list in [awesome-turnstile-solvers](https://github.com/CircuitSavage/awesome-turnstile-solvers).

[cloudflare-turnstile-reversed](https://github.com/CircuitSavage/cloudflare-turnstile-reversed) documents the challenge flow, the widget parameters and the fingerprinting the widget runs before it hands out a token.

## Legitimate use

For automation, QA, and scraping public data you are allowed to access. Respect each target's Terms of Service and robots directives, and do not use this for credential stuffing or other abuse. Not affiliated with Cloudflare or the pydoll project.

## License

MIT. See [LICENSE](./LICENSE).
