# Solving AWS WAF with Python

Most captcha task types are a two-step client. You create a task, store the `taskId`, and poll until the worker is done. `AntiAwsWafTask` is the type Kaptra documents as synchronous. `/createTask` returns `status=ready` and the token in the same response. If your wrapper always polls, this is the call that should skip that branch.

This is a practical walkthrough of that call. It is not the guide for the AWS WAF image grid. That type is `AwsWafClassification`, it is asynchronous, and it returns tile indices instead of a token.

## The task in one glance

- Type: `AntiAwsWafTask`
- Required: `websiteURL`, the public URL of the challenged page
- Optional: `referrer`, which defaults from `websiteURL`
- Optional, as a set: `awsKey`, `awsIv`, `awsContext`. All three, or none
- Not a parameter: `proxy`
- Execution: synchronous. `/createTask` returns the result. Do not poll
- Result: `solution.token`, shaped like `aws-waf-token=...`
- Cost and speed: $0.60 per 1,000 successful tasks, average under 3 seconds, billed per success only

## Why there is no poll loop

The introduction is explicit. Some types, today `AntiAwsWafTask`, return `status=ready` from `/createTask`. Everything else returns `status=processing` and a `taskId`, and you poll `/getTaskResult` every 1 to 2 seconds, at most 120 times per task.

A shared client can keep the poll branch for the other types. For this type, the branch is dead code. There is no `taskId` to query, and querying one you invented will not make the call more correct.

The HTTP status is still always 200. Read `errorId` before you read `solution`. It is 0 or 1, and the reason is `errorCode`.

## Working Python implementation

```python
import requests
BASE_URL = "https://api.kaptra.ai"
API_KEY = "YOUR_KAPTRA_API_KEY"
def solve_aws_waf(website_url: str) -> dict:
    # synchronous: createTask returns the result, no polling
    resp = requests.post(
        f"{BASE_URL}/createTask",
        json={
            "clientKey": API_KEY,
            "task": {
                "type": "AntiAwsWafTask",
                "websiteURL": website_url,
            },
        },
    ).json()
    if resp.get("errorId"):
        raise RuntimeError(f"createTask failed: {resp.get('errorCode')}")
    if resp.get("status") != "ready":
        raise RuntimeError(f"unexpected status: {resp.get('status')}")
    return resp["solution"]  # {"token": "aws-waf-token=..."}
if __name__ == "__main__":
    page = "https://www.example.com/page"
    solution = solve_aws_waf(page)
    token = solution["token"]
    print(token)
    # retry the page with the token as a cookie
    value = token.split("=", 1)[1]
    session = requests.Session()
    session.cookies.set("aws-waf-token", value)
    page_resp = session.get(page)
    print(page_resp.status_code)
```

The retry is the second half of the function, not a later session. Split the token on the first `=`, set a cookie named `aws-waf-token`, and request the same page again. The documented solution has no expiry field and no user agent, so there is nothing else to copy across.

## The triple, and the grid that is not this task

`awsKey`, `awsIv`, and `awsContext` are documented as all three or none. The minimal request in the docs sends `websiteURL` only. Start there. Add the triple only when the challenge page has already given you all three values. A partial set is `ERROR_BAD_REQUEST`, not a more precise solve.

If the page is a 3 by 3 image grid instead of a token challenge, switch types. `AwsWafClassification` takes `images` (nine base64 tiles, in order) and `question` (`aws:grid:bag`, or a bare `bag`). It is async, so that one does poll. The solution is `objects`, indices 0 through 8, row-major, top-left first. Supported targets are `bag`, `bed`, `bucket`, `chair`, `clock`, `curtain`, and `hat`. A target outside that list returns `ERROR_CAPTCHA_UNSOLVABLE` and is not charged.

## Cost, and the errors worth handling

The pricing page lists this type at under 3 seconds and $0.60 per 1,000 successful solves, the same rate as the grid type. Failed and timed-out tasks cost nothing. There is no monthly fee, and top-up credit does not expire. New accounts start with $10 in test credit, which is 16,666 successful solves at this rate.

On `ERROR_CAPTCHA_UNSOLVABLE`, do not retry the same parameters in a loop. The error dictionary says to inspect the site and the fields, then create a new task. `ERROR_TASK_NOT_SUPPORTED` means the type string is wrong or the type is disabled. `ERROR_ZERO_BALANCE` means stop and top up. `ERROR_BAD_REQUEST` means a field is missing or malformed, which is the partial-triple case above.

## Related, and not the same call

- Site: [kaptra.ai](https://kaptra.ai/)
- AntiAwsWafTask docs: [kaptra.ai/docs](https://kaptra.ai/docs/task-types/anti-aws-waf)
- A widget that does poll: [How to solve Cloudflare Turnstile in Python](https://medium.com/@kaptra/how-to-solve-cloudflare-turnstile-in-python-for-automated-testing-a7b43fc90d32)
- Cookies plus your own proxy: [How to solve the Cloudflare 5-second challenge in Python](https://medium.com/@kaptra/how-to-solve-the-cloudflare-5-second-challenge-in-python-for-automated-testing-27193c859c72)
- A token with a two-minute clock: [How to solve reCAPTCHA v2 in Python](https://medium.com/@kaptra/how-to-solve-recaptcha-v2-in-python-for-automated-testing-121b611f427d)
