# getTaskResult

> POST /getTaskResult returns a task's status and, once it's ready, its solution.

Source: https://docs.peak.fo/docs/compat/get-task-result

`POST https://api.peak.fo/getTaskResult`

Returns whether a task from [createTask](/docs/compat/create-task) is still being solved, and its solution once it's ready. Poll it every 3 to 5 seconds.

## Request [#request]

| Field       | Type               | Required | Description                                                          |
| ----------- | ------------------ | -------- | -------------------------------------------------------------------- |
| `clientKey` | `string`           | Yes      | Your API key. Any key on the account that created the task works.     |
| `taskId`    | `number`, `string` | Yes      | The `taskId` from `createTask`. A number may also be sent as a string. |

```bash title="Terminal"
curl https://api.peak.fo/getTaskResult \
  -H "Content-Type: application/json" \
  -d '{"clientKey": "pk_your_api_key", "taskId": 4826153907261}'
```

## Response [#response]

| Field        | Type     | Description                                                                 |
| ------------ | -------- | --------------------------------------------------------------------------- |
| `errorId`    | `number` | `0` if the request worked.                                                  |
| `status`     | `string` | `processing` while solving, `ready` when the solution is in.                |
| `solution`   | `object` | Only when `ready`. Its fields are below.                                    |
| `cost`       | `string` | Only when `ready`. What the solve was charged, in USD, as a string.        |
| `createTime` | `number` | Only when `ready`. When the task was created, as a Unix time in seconds.    |
| `endTime`    | `number` | Only when `ready`. When the solve finished, as a Unix time in seconds.      |
| `solveCount` | `number` | Only when `ready`. Always `1`.                                              |
| `taskId`     | `string` | Only when `ready`, for CapSolver tasks. The task's id.                      |

### Solution [#solution]

| Task                                       | `solution` fields                                                                                    |
| ------------------------------------------ | ---------------------------------------------------------------------------------------------------- |
| Turnstile                                  | `token`: the Turnstile token.                                                                        |
| 5-second challenge, CapMonster code        | `cf_clearance`: the cookie value. `userAgent`: the user-agent the cookie is bound to.                |
| 5-second challenge, CapSolver code         | `cookies`: every cookie set, including `cf_clearance`. `token`: the `cf_clearance` value. `userAgent`. |

Send the cookie with the same `userAgent`, through the same proxy you gave the task. Cloudflare checks both.

```json tab="Processing"
{
  "errorId": 0,
  "status": "processing"
}
```

```json tab="Ready (Turnstile)"
{
  "errorId": 0,
  "status": "ready",
  "solution": {
    "token": "0.AgAAAB..."
  },
  "cost": "0.0009",
  "createTime": 1795094012,
  "endTime": 1795094019,
  "solveCount": 1
}
```

```json tab="Ready (cf_clearance)"
{
  "errorId": 0,
  "status": "ready",
  "solution": {
    "cf_clearance": "Xk3o9...",
    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"
  },
  "cost": "0.001",
  "createTime": 1795094012,
  "endTime": 1795094031,
  "solveCount": 1
}
```

```json tab="Failed"
{
  "errorId": 12,
  "errorCode": "ERROR_CAPTCHA_UNSOLVABLE",
  "errorDescription": "Challenge was flagged - try using a different proxy with a cleaner IP reputation"
}
```

A failed solve isn't charged. `errorDescription` says why it failed, in the same words as the native API's `error`. See [Error codes](/docs/compat/errors) for how failures map to `errorCode`.

## Task not found [#task-not-found]

`ERROR_NO_SUCH_CAPCHA_ID` (CapSolver code: `ERROR_TASKID_INVALID`) means one of these:

- The `taskId` is wrong.
- The task finished more than 5 minutes ago and was removed.
- Peak Solutions restarted since you created it.
- It belongs to another account.

Create a new task. If the solve had already finished, it was charged.

## A complete loop [#a-complete-loop]

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

import requests

API = "https://api.peak.fo"
API_KEY = "pk_your_api_key"


def solve_turnstile(url: str, sitekey: str) -> str:
    """Creates a Turnstile task and polls until it has a token."""
    created = requests.post(
        f"{API}/createTask",
        json={
            "clientKey": API_KEY,
            "task": {"type": "TurnstileTaskProxyless", "websiteURL": url, "websiteKey": sitekey},
        },
        timeout=30,
    ).json()
    if created["errorId"] != 0:
        raise RuntimeError(f"{created['errorCode']}: {created['errorDescription']}")

    for _ in range(40):
        time.sleep(3)
        result = requests.post(
            f"{API}/getTaskResult",
            json={"clientKey": API_KEY, "taskId": created["taskId"]},
            timeout=30,
        ).json()
        if result["errorId"] != 0:
            raise RuntimeError(f"{result['errorCode']}: {result['errorDescription']}")
        if result["status"] == "ready":
            return result["solution"]["token"]
    raise TimeoutError("no result after 2 minutes")


if __name__ == "__main__":
    print(solve_turnstile("https://example.com/login", "0x4AAAAAAA..."))
```

```js tab="JavaScript" tab-group="language"
const API = "https://api.peak.fo";
const API_KEY = "pk_your_api_key";

async function call(method, body) {
  const resp = await fetch(`${API}/${method}`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ clientKey: API_KEY, ...body }),
  });
  const data = await resp.json();
  if (data.errorId !== 0) throw new Error(`${data.errorCode}: ${data.errorDescription}`);
  return data;
}

// Creates a Turnstile task and polls until it has a token.
async function solveTurnstile(url, sitekey) {
  const { taskId } = await call("createTask", {
    task: { type: "TurnstileTaskProxyless", websiteURL: url, websiteKey: sitekey },
  });
  for (let i = 0; i < 40; i++) {
    await new Promise((r) => setTimeout(r, 3000));
    const result = await call("getTaskResult", { taskId });
    if (result.status === "ready") return result.solution.token;
  }
  throw new Error("no result after 2 minutes");
}

console.log(await solveTurnstile("https://example.com/login", "0x4AAAAAAA..."));
```
