Skip to content
On this page
Home
>Task(Token)
>Cloudflare(turnstile)

Cloudflare: solving Turnstile

TIP

Create the task with the createTask method and get the result with the getTaskResult method.

DANGER

ℹ️turnstile type doesn't require proxy, so you just use AntiTurnstileTaskProxyLess

The Turnstile/Challenge verification code is another attempt to replace reCaptcha/hCaptcha. We automatically support these subtypes:

  • turnstile
    • Manually
    • Non-Interactive
    • InVisible
  • challenge
    • 5s challenge
    • Non-Interactive turnstile

There is no need to specify subtypes during your call. It is not necessary to provide your own custom User-Agent yet, we will ignore this parameter.

TypeNoteState
img.pngturnstilestable

The task type type is as follows

  • AntiTurnstileTaskProxyLess

Create Task

Create the task with the createTask.

In the process of using turnstile, we must input websiteURL and websiteKey, other parameters are optional.

Task Object Structure

PropertiesTypeRequiredDescription
typeStringRequiredAntiTurnstileTaskProxyLess
websiteURLStringRequiredThe address of the target page.
websiteKeyStringRequiredTurnstile website key.
metadataMap<String,String>RequiredTurnstile extra data . Turnstile Documentation
metadata.actonStringOptionalThe value of the data-action attribute of the Turnstile element if it exists.
metadata.cdataStringOptionalThe value of the data-cdata attribute of the Turnstile element if it exists.

Example Request

txt
POST https://api.capsolver.com/createTask
Host: api.capsolver.com
Content-Type: application/json
json
{
  "clientKey": "YOUR_API_KEY",
  "task": {
    "type": "AntiTurnstileTaskProxyLess",
    "websiteURL": "https://www.yourwebsite.com",
    "websiteKey": "0x4XXXXXXXXXXXXXXXXX",
    "metadata": {
       "action": "login",  //optional
       "cdata": "0000-1111-2222-3333-example-cdata"  //optional
    }
  }
}

Example Response

json
{
  "errorId": 0,
  "status": "idle",
  "taskId": "61138bb6-19fb-11ec-a9c8-0242ac110006"   // record taskId
}

Getting Result

Use the getTaskResult method to get the recognition results

Depending on the system load, you will get the results within the interval of 1s to 20s

Example Request

txt
POST https://api.capsolver.com/getTaskResult
Host: api.capsolver.com
Content-Type: application/json
json
{
  "clientKey": "YOUR_API_KEY",
  "taskId": "61138bb6-19fb-11ec-a9c8-0242ac110006"
}

Example Response

json
{
  "errorId": 0,
  "taskId": "61138bb6-19fb-11ec-a9c8-0242ac110006",
  "status": "ready",
  "errorCode": null,
  "errorDescription": null,
  "solution": {
    "token": "0.mF74FV8wEufAWOdvOak_xFaVy3lqIDel7SwNhw3GgpICSWwTjYfrQB8mRT1dAJJBEoP7N1sESdp6WH9cTS1T0catWLecG3ayNcjwxVtr3hWfS-dmcBGRTx4xYwI64sAVboYGpIyuDBeMIRC3W8dK35v1nDism9xa595Da5VlXKM7hk7pIXg69lodfiftasIkyD_KUGkxBwxvrmz7dBo10-Y5zvro9hD4QKRjOx7DYj9sumnkyYCDx0m4ImDIIkNswfVTWI2V22wlnpHdvMgdtKYgOIIAU28y9gtdrdDkpkH0GHcDyd15sxQGd9VjwhGZA_mpusUKMsEoGgst2rJ3zA.UWfZupqLlGvlATkPo3wdaw.38d55cd0163610d8ce8c42fcff7b62d8981495cc1afacbb2f14e5a23682a4e13",
    "type": "turnstile",
    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
  }
}

Use SDK Request

python
# pip install --upgrade capsolver
# export CAPSOLVER_API_KEY='...'

import capsolver

# capsolver.api_key = "..."
solution = capsolver.solve({
  "type": "AntiTurnstileTaskProxyLess",
  "websiteURL": "https://www.yourwebsite.com",
  "websiteKey": "0x4XXXXXXXXXXXXXXXXX",
  "metadata": {
	 "action": "login"  # optional
  }
})
go
package main

import (
  "fmt"
  capsolver_go "github.com/capsolver/capsolver-go"
  "log"
)

func main() {
  // first you need to install sdk
  //go get github.com/capsolver/capsolver-go

  capSolver := capsolver_go.CapSolver{ApiKey: "..."}
  solution, err := capSolver.Solve(map[string]any{
    "type":       "AntiTurnstileTaskProxyLess",
    "websiteURL": "https://www.yourwebsite.com",
    "websiteKey": "0x4XXXXXXXXXXXXXXXXX",
    "metadata": map[string]string{
	  "action": "login"  // optional
    },
  })
  if err != nil {
    log.Fatal(err)
    return
  }
  fmt.Println(solution)
}

Sample Code

python
# pip install requests
import requests
import time

api_key = "YOUR_API_KEY"  # your api key of capsolver
site_key = "0x4XXXXXXXXXXXXXXXXX"  # site key of your target site
site_url = "https://www.yourwebsite.com"  # page url of your target site

def capsolver():
    payload = {
        "clientKey": api_key,
        "task": {
            "type": 'AntiTurnstileTaskProxyLess',
            "websiteKey": site_key,
            "websiteURL": site_url,
            "metadata": {
                "action": ""  # optional
            }
        }
    }
    res = requests.post("https://api.capsolver.com/createTask", json=payload)
    resp = res.json()
    task_id = resp.get("taskId")
    if not task_id:
        print("Failed to create task:", res.text)
        return
    print(f"Got taskId: {task_id} / Getting result...")

    while True:
        time.sleep(1)  # delay
        payload = {"clientKey": api_key, "taskId": task_id}
        res = requests.post("https://api.capsolver.com/getTaskResult", json=payload)
        resp = res.json()
        status = resp.get("status")
        if status == "ready":
            return resp.get("solution", {}).get('token')
        if status == "failed" or resp.get("errorId"):
            print("Solve failed! response:", res.text)
            return

token = capsolver()
print(token)
go
package main

import (
	"bytes"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"net/http"
	"time"
)

type capSolverResponse struct {
	ErrorId          int32          `json:"errorId"`
	ErrorCode        string         `json:"errorCode"`
	ErrorDescription string         `json:"errorDescription"`
	TaskId           string         `json:"taskId"`
	Status           string         `json:"status"`
	Solution         map[string]any `json:"solution"`
}

func capSolver(ctx context.Context, apiKey string, taskData map[string]any) (*capSolverResponse, error) {
	uri := "https://api.capsolver.com/createTask"
	res, err := request(ctx, uri, map[string]any{
		"clientKey": apiKey,
		"task":      taskData,
	})
	if err != nil {
		return nil, err
	}
	if res.ErrorId == 1 {
		return nil, errors.New(res.ErrorDescription)
	}

	uri = "https://api.capsolver.com/getTaskResult"
	for {
		select {
		case <-ctx.Done():
			return res, errors.New("solve timeout")
		case <-time.After(time.Second):
			break
		}
		res, err = request(ctx, uri, map[string]any{
			"clientKey": apiKey,
			"taskId":    res.TaskId,
		})
		if err != nil {
			return nil, err
		}
		if res.ErrorId == 1 {
			return nil, errors.New(res.ErrorDescription)
		}
		if res.Status == "ready" {
			return res, err
		}
	}
}

func request(ctx context.Context, uri string, payload interface{}) (*capSolverResponse, error) {
	payloadBytes, err := json.Marshal(payload)
	if err != nil {
		return nil, err
	}
	req, err := http.NewRequestWithContext(ctx, "POST", uri, bytes.NewReader(payloadBytes))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	responseData, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	capResponse := &capSolverResponse{}
	err = json.Unmarshal(responseData, capResponse)
	if err != nil {
		return nil, err
	}
	return capResponse, nil
}

func main() {
	apikey := "YOUR_API_KEY"
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*120)
	defer cancel()

	res, err := capSolver(ctx, apikey, map[string]any{
		"type":       "AntiTurnstileTaskProxyLess",
		"websiteURL": "https://www.yourwebsite.com",
		"websiteKey": "0x4XXXXXXXXXXXXXXXXX",
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(res.Solution["token"])
}