Metadata-Version: 2.5
Name: turnstile-curl
Version: 0.2.2
Summary: Solve Cloudflare Turnstile for curl_cffi with no browser, via the Peak API, keeping curl_cffi's TLS/JA3 speed.
Project-URL: Source, https://github.com/CircuitSavage/turnstile-curl
Project-URL: Homepage, https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=turnstile-curl
Project-URL: Documentation, https://peak.fo/docs/turnstile?utm_source=github&utm_medium=pypi&utm_campaign=packages&utm_content=turnstile-curl
Author: Peak
License: MIT
License-File: LICENSE
Keywords: bypass,captcha,cloudflare,curl-cffi,curl_cffi,impersonate,solver,tls,turnstile
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.8
Provides-Extra: curl
Requires-Dist: curl-cffi>=0.7; extra == 'curl'
Provides-Extra: dev
Requires-Dist: curl-cffi>=0.7; extra == 'dev'
Description-Content-Type: text/markdown

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

# turnstile-curl

**turnstile-curl** is a curl_cffi companion that solves Cloudflare Turnstile without a browser, so a curl_cffi request that hits a Turnstile wall comes back with a real token instead of a 403.

## Why

curl_cffi forges a browser's TLS/JA3 handshake, which is why people reach for it. It has no JavaScript engine. Cloudflare Turnstile is a JS widget, so a managed or interactive challenge hands curl_cffi an interstitial HTML page or a `403`, and there is nothing in-process that can run the challenge. This is the "curl_cffi cloudflare blocked" wall: the handshake is right, the request still fails.

turnstile-curl reads the Turnstile sitekey out of the returned markup, sends it to [Peak](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=turnstile-curl), and gives you back the `cf-turnstile-response` token. curl_cffi still does the fetching, so you keep its speed and fingerprint.

## Powered by Peak
This package uses [Peak](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=turnstile-curl) to solve Turnstile.
- Solves Cloudflare Turnstile and the 5s challenge
- $0.90 per 1,000 successful Turnstile solves, down to $0.35 at volume; failed solves cost nothing
- 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=turnstile-curl) · [Docs](https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=turnstile-curl) · [Pricing](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=turnstile-curl#pricing)

## Install

```bash
pip install turnstile-curl curl_cffi
```

`curl_cffi` is the transport you fetch pages with. It is an optional dependency here so the library imports cleanly without it; install it to fetch pages.

## Quickstart

```python
import os
from turnstile_curl import solve

# Free key at https://peak.fo. Reads PEAK_API_KEY by default.
os.environ.setdefault("PEAK_API_KEY", "pk_your_api_key")

# Fetch the protected page with curl_cffi, read the sitekey, solve via Peak.
token = solve("https://your-target.example/login")
print(token)  # -> "0.AgAAA..."  (the cf-turnstile-response value)
```

Send `token` as the `cf-turnstile-response` form field (or into the widget callback) on your next request.

## Session: solve and resubmit in one shot

`TurnstileSession` wraps a curl_cffi session and keeps the same browser fingerprint through the whole flow.

```python
from turnstile_curl import TurnstileSession

with TurnstileSession(api_key="pk_your_api_key", impersonate="chrome") as s:
    # Token only:
    token = s.solve("https://your-target.example/login")

    # Or solve + resubmit the form with the token injected, and keep the
    # authenticated response and cookies on the session:
    resp = s.solve_and_submit(
        "https://your-target.example/login",
        submit_url="https://your-target.example/verify",
        data={"username": "demo", "password": "demo"},
    )
    print(resp.status_code)
```

## Already have the sitekey

Skip the fetch and call Peak directly.

```python
from turnstile_curl import PeakClient

peak = PeakClient(api_key="pk_your_api_key")
token = peak.solve_turnstile(
    sitekey="0x4AAAAAAADnPIDROrmt1Wwj",
    url="https://your-target.example/login",
    # proxy="http://user:pass@ip:port",  # optional
)
```

## Proxies

Pass `proxy="http://user:pass@ip:port"` to `TurnstileSession`, `solve()`, or `PeakClient.solve_turnstile()`. The same proxy is used for the page fetch and forwarded to Peak so the token is minted from the IP you will use it on.

## Earn with your app ID

Pass your Peak app id and you earn 5% of every solve this tool makes, paid as solve credit. It is optional and does not affect solve success or speed; it only tells Peak which app to credit. Create an app id at [peak.fo/dashboard/developer](https://peak.fo/dashboard/developer), details at [peak.fo/earn](https://peak.fo/earn).

```python
from turnstile_curl import solve

# Pass app_id=, or set the PEAK_APP_ID env var instead.
token = solve("https://your-target.example/login", app_id="app_your_app_id")
```

`app_id` works the same on `TurnstileSession(...)` and `PeakClient(...)`, and falls back to `PEAK_APP_ID` when omitted.

## How it works

1. curl_cffi fetches the page with a forged browser TLS/JA3 fingerprint.
2. turnstile-curl parses the sitekey (and `cData` / `action` / `pagedata` when present) from the `data-sitekey` container or a `turnstile.render(...)` call.
3. It calls `POST https://api.peak.fo/solve` with `task_type: "turnstiletask"`, the sitekey, and the URL.
4. Peak returns the token; you resubmit the form with it as `cf-turnstile-response`.

Peak also solves the Cloudflare 5s challenge (`task_type: "cloudflare5stask"`); this package focuses on Turnstile.

## API

- `solve(url, *, api_key=None, impersonate="chrome", proxy=None, app_id=None) -> str`: one-shot fetch + solve, returns the token.
- `TurnstileSession(api_key=None, *, impersonate="chrome", proxy=None, app_id=None)`: `.get()`, `.solve(url)`, `.detect_challenge(url)`, `.solve_and_submit(url, ...)`.
- `PeakClient(api_key=None, *, app_id=None)`: `.solve(challenge)`, `.solve_turnstile(sitekey, url, ...)`.
- `parse_challenge(html, url)` / `find_sitekey(html)`: the extractor, if you fetch pages yourself.

The API key is read from `PEAK_API_KEY` unless you pass `api_key=`. The optional revenue-share app id is read from `PEAK_APP_ID` unless you pass `app_id=`.

## Troubleshooting

Every exception subclasses `TurnstileCurlError`, so one `except` catches them all. Each has its own class and a message you can grep for.

- **`SitekeyNotFound: No Turnstile sitekey found in the page.`** The HTML curl_cffi received has no `data-sitekey` and no `turnstile.render(...)` call. Either the URL is not behind Turnstile, or the widget is built by a script bundle after load, and curl_cffi runs no JavaScript. Open the page in a browser, copy the sitekey from the network tab or the bundle, and call `PeakClient.solve_turnstile(sitekey, url)`. [How to find a Cloudflare Turnstile sitekey](https://blog.peak.fo/how-to-find-a-cloudflare-turnstile-sitekey/) shows where sites hide it.
- **`MissingAPIKey: 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=turnstile-curl).
- **`PeakAPIError`** Peak answered with `success: false`, and the message is Peak's own reason. The exception carries `.status_code` and `.payload` for logging. Common causes: an empty balance, a sitekey that does not belong to the 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) that sit behind many failed solves.
- **`TurnstileCurlError: curl_cffi is not installed.`** The package imports without curl_cffi, but `solve(url)` and `TurnstileSession` need it to fetch the page. Run `pip install curl_cffi`, or skip the fetch with `PeakClient.solve_turnstile(sitekey, url)`.
- **`solve_and_submit` returns 403 or the form page again.** The token was minted and the site refused the submit. Check three things. The field name: most sites read `cf-turnstile-response`, some read their own JSON key. The age: a token is single-use and expires 300 seconds after it is issued, so submit it right away and never reuse it. The IP: pass `proxy=` so the token is minted from the address that submits it. [The cf-turnstile-response token explained](https://blog.peak.fo/the-cf-turnstile-response-token-explained/) and [cf-turnstile-response is empty](https://blog.peak.fo/cf-turnstile-response-empty/) cover the field, and [Cloudflare Turnstile 403](https://blog.peak.fo/cloudflare-turnstile-403-scraper-fix/) covers the refusal.
- **The first response is a "Just a moment..." page with no sitekey.** That page is the managed 5-second challenge, a separate check from the Turnstile widget. It wants a `cf_clearance` cookie, which this package does not fetch. [cloudscraper-turnstile](https://github.com/CircuitSavage/cloudscraper-turnstile) handles that path without a browser. [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 for this stack: [How to scrape Cloudflare-protected sites without getting blocked](https://blog.peak.fo/how-to-scrape-cloudflare-protected-sites-without-getting-blocked/) (also on [Hashnode](https://peakfo.hashnode.dev/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

Same pattern for other stacks: [cloudscraper-turnstile](https://github.com/CircuitSavage/cloudscraper-turnstile) (a `requests.Session` drop-in, no browser) · [scrapy-turnstile](https://github.com/CircuitSavage/scrapy-turnstile) · [scrapling-turnstile](https://github.com/CircuitSavage/scrapling-turnstile) · [playwright-turnstile](https://github.com/CircuitSavage/playwright-turnstile) · [patchright-turnstile](https://github.com/CircuitSavage/patchright-turnstile) · [camoufox-turnstile](https://github.com/CircuitSavage/camoufox-turnstile) · [nodriver-turnstile](https://github.com/CircuitSavage/nodriver-turnstile) · [pydoll-turnstile](https://github.com/CircuitSavage/pydoll-turnstile) · [drissionpage-turnstile](https://github.com/CircuitSavage/drissionpage-turnstile) · [selenium-turnstile](https://github.com/CircuitSavage/selenium-turnstile) · [botasaurus-turnstile](https://github.com/CircuitSavage/botasaurus-turnstile) · [crawlee-turnstile](https://github.com/CircuitSavage/crawlee-turnstile) · [puppeteer-extra-plugin-turnstile](https://github.com/CircuitSavage/puppeteer-extra-plugin-turnstile) · [browser-use-turnstile](https://github.com/CircuitSavage/browser-use-turnstile) · [crawl4ai-turnstile](https://github.com/CircuitSavage/crawl4ai-turnstile). Full list in [awesome-turnstile-solvers](https://github.com/CircuitSavage/awesome-turnstile-solvers).

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

## Legitimate use

For automation, QA, and scraping public data you are allowed to access. Respect each target's Terms of Service and `robots.txt`, and rate-limit yourself. Do not use this for credential stuffing or to access data you have no right to.

## License

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