# Find the sitekey

> Where to find a Turnstile widget's sitekey, action and cdata on a page.

Source: https://docs.peak.fo/docs/find-sitekey

To solve Turnstile you need the widget's **sitekey**. Some sites also set an **action** and **cdata**. If they do, send them too, or the solve fails with `invalid_action` or `invalid_cdata`.

| Field     | Looks like                                                     | Required                  |
| --------- | -------------------------------------------------------------- | ------------------------- |
| `sitekey` | `0x4AAAAAAAB1cD2eF3gH4iJ5`                                     | Yes                       |
| `action`  | Up to 32 letters, digits, `_` or `-`, such as `login`          | Only if the page sets one |
| `cdata`   | Up to 255 letters, digits, `_` or `-`, such as `session-8f2a`  | Only if the page sets one |
| `url`     | The full URL of the page                                       | Yes                       |

Send `url` as the page that shows the widget, not the site's home page.

Real sitekeys start with `0x4`. Sitekeys that start with `1x`, `2x` or `3x`, such as `1x00000000000000000000AA`, are Cloudflare's [test sitekeys](https://developers.cloudflare.com/turnstile/troubleshooting/testing/): the site is in testing mode and doesn't need a real solve.

You don't need any of this for the [5-Second Challenge (WAF)](/docs/tasks/cloudflare-5s). Peak Solutions reads it from the `html` you send.

## In the page HTML [#in-the-page-html]

Most sites put the widget in the HTML as an element with `data-` attributes:

```html
<div
  class="cf-turnstile"
  data-sitekey="0x4AAAAAAAB1cD2eF3gH4iJ5"
  data-action="login"
  data-cdata="session-8f2a"
></div>
```

Open the page, view its source, and search for `cf-turnstile`. The `data-sitekey`, `data-action` and `data-cdata` on that element are the sitekey, action and cdata.

Search for `cf-turnstile`, not just `data-sitekey`: reCAPTCHA and hCaptcha widgets use `data-sitekey` too, and other scripts often use `data-action` for their own purposes.

## Rendered with JavaScript [#rendered-with-javascript]

Other sites create the widget from a script, with `turnstile.render()`:

```js
turnstile.render('#captcha', {
  sitekey: '0x4AAAAAAAB1cD2eF3gH4iJ5',
  action: 'login',
  cData: 'session-8f2a',
});
```

Search the page's scripts for `turnstile.render` or for `0x4AAAA`. Here the cdata option is spelled `cData`, but you send it to Peak Solutions as `cdata`.

## With the browser console [#with-the-browser-console]

On the page with the widget, open the developer tools (F12), go to **Console**, and run:

```js
[...document.querySelectorAll('.cf-turnstile')].map((el) => ({
  sitekey: el.dataset.sitekey,
  action: el.dataset.action,
  cdata: el.dataset.cdata,
}));
```

It lists every widget written in the HTML. If it returns an empty list, or a widget without a sitekey, the widget was rendered with JavaScript: use the **Network** tab instead.

## In the Network tab [#in-the-network-tab]

Every widget loads a frame from `challenges.cloudflare.com`, with the sitekey in its URL.

1. Open the developer tools (F12) and go to **Network**.
2. Reload the page and filter by `challenges.cloudflare.com`.
3. Find the request whose path contains `turnstile`. The part of the path that starts with `0x4` is the sitekey.

This works even when the widget is hidden or created by a script.

## In your code [#in-your-code]

If your code already loads the page, read the values out of the HTML before you solve:

```python tab="Python" tab-group="language"
import re

def find_turnstile(html: str) -> dict:
    """Returns the sitekey, action and cdata of the first Turnstile widget in the HTML."""
    widget = re.search(r'<[^>]*cf-turnstile[^>]*>', html)
    if not widget:
        return {}
    found = {}
    for field in ("sitekey", "action", "cdata"):
        match = re.search(rf'data-{field}=["']([^"']+)["']', widget.group(0))
        if match:
            found[field] = match.group(1)
    return found
```

```js tab="JavaScript" tab-group="language"
function findTurnstile(html) {
  const widget = html.match(/<[^>]*cf-turnstile[^>]*>/);
  if (!widget) return {};
  const found = {};
  for (const field of ["sitekey", "action", "cdata"]) {
    const match = widget[0].match(new RegExp(`data-${field}=["']([^"']+)["']`));
    if (match) found[field] = match[1];
  }
  return found;
}
```

```go tab="Go" tab-group="language"
import "regexp"

var widgetRe = regexp.MustCompile(`<[^>]*cf-turnstile[^>]*>`)

// findTurnstile returns the sitekey, action and cdata of the first Turnstile widget in the HTML.
func findTurnstile(html string) map[string]string {
	found := map[string]string{}
	widget := widgetRe.FindString(html)
	if widget == "" {
		return found
	}
	for _, field := range []string{"sitekey", "action", "cdata"} {
		re := regexp.MustCompile(`data-` + field + `=["']([^"']+)["']`)
		if m := re.FindStringSubmatch(widget); m != nil {
			found[field] = m[1]
		}
	}
	return found
}
```

Then pass them into the task with the page `url`. If a field isn't found, leave it out of the task. If no widget is found at all, it was rendered with JavaScript: look for the `turnstile.render()` call in the page's scripts instead.

## If the solve fails [#if-the-solve-fails]

| `error_code`      | What to check                                                                |
| ----------------- | ---------------------------------------------------------------------------- |
| `invalid_sitekey` | The sitekey is copied in full, and belongs to the `url` you sent.            |
| `invalid_action`  | The action matches the page exactly, or leave it out if the page has none.   |
| `invalid_cdata`   | The cdata matches the page exactly. Some sites change it on every page load. |

If the site changes `cdata` on every load, read it again from the page right before each solve.
