Skip to main content

Modules

Fetchyr

Logins and authenticated browsing — login sessions from your own account, login flows, MFA detection, and form detect and fill

8 min read


Fetchyr manages authenticated browser sessions for sites requiring login or MFA, and detects and fills forms. Use it when you need to scrape pages behind a login wall.

See the Authenticated Scraping guide for a complete walkthrough.

store_credentials / authenticate

Credentials are stored once, encrypted on the server, under a name. authenticate then logs in with them and returns a session artifact you pass to later calls.

client.fetchyr.store_credentials("acme", username="user@example.com", password="secret")

session = client.fetchyr.authenticate(
    credential_name="acme",
    target_url="https://example.com/login",
)
print(session.success, session.session_id, session.artifact_id)
await client.fetchyr.storeCredentials('acme', { username: 'user@example.com', password: 'secret' });
const session = await client.fetchyr.authenticate('acme', 'https://example.com/login');

authenticate() parameters

| Parameter | Type | Required | Description | |-----------|------|----------|-------------| | credential_name | string | Yes | Name the credentials were stored under | | target_url | string | Yes | Login page (or a page behind the login) |

The result carries success, session_id, artifact_id (the session artifact), target_domain, auth_method and error.

Fetch with the session

Authenticated pages are fetched with evadr.fetch, passing the session artifact:

result = client.evadr.fetch(
    "https://example.com/dashboard",
    session_artifact_id=session.artifact_id,
)
print(result.html)

Log in with your own account

A login session is a session made from your own account on a site. Start one with POST /api/v1/organizations/{org_id}/fetchyr/login-sessions, then pass its session_artifact_id to evadr.fetch, webgrph.crawl or kolektr.page. From 0.7.0 the SDKs call these three routes with fetchyr.start_login_session, fetchyr.get_session and fetchyr.resume_session (startLoginSession, getSession, resumeSession in TypeScript and Java; StartLoginSession, GetSession, ResumeSession in Go).

| Field | Type | Required | Description | |-------|------|----------|-------------| | target_url | string | Yes | A page of the site that needs the login (http or https) | | method | string | No | interactive (default) or cookies | | cookies | list | For cookies | Your browser's cookies for the site: name, value, and optionally domain, path, expires or expirationDate (epoch seconds), secure, httpOnly, sameSite | | user_agent | string | For cookies | Your browser's navigator.userAgent (512 characters at most). The session presents it | | country | string | No | Two-letter country the session's exit is in (where the account usually logs in from) |

method: "interactive" answers 202. KLOAKD opens the site's login in a browser and asks you for what it needs:

curl -X POST "https://api.kloakd.dev/api/v1/organizations/$ORG_ID/fetchyr/login-sessions" \
  -H "Authorization: Bearer $KLOAKD_API_KEY" -H "Content-Type: application/json" \
  -d '{"target_url": "https://example.com/account"}'
{
  "method": "interactive",
  "status": "waiting_for_login",
  "target_url": "https://example.com/account",
  "discovery_id": "…",
  "session_artifact_id": null,
  "next": {
    "status": "GET /api/v1/organizations/{org_id}/fetchyr/auth-chain/{discovery_id}/status",
    "credentials": "POST /api/v1/organizations/{org_id}/fetchyr/auth-chain/{discovery_id}/resume-credentials",
    "mfa": "POST /api/v1/organizations/{org_id}/fetchyr/auth-chain/{discovery_id}/resume-mfa"
  },
  "cookies_kept": null,
  "cookies_dropped": [],
  "protections": { "min_interval_s": 8.0, "interval_jitter_s": 7.0, "max_requests_per_hour": 120, "max_concurrent": 1, "max_wait_s": 180.0, "max_attempts_per_page": 2 },
  "message": "The login is starting in a browser. Poll the status; when it waits for credentials, send your username and password."
}

Poll next.status and answer its pauses exactly as for an auth-chain discovery; when it is completed with success, it names the session_artifact_id.

method: "cookies" answers 201 with a session that is ready at once. Send the cookies of a session you already have in your own browser, with that browser's user agent. Only the site's own cookies are kept: cookies_kept counts them and cookies_dropped names the rest.

curl -X POST "https://api.kloakd.dev/api/v1/organizations/$ORG_ID/fetchyr/login-sessions" \
  -H "Authorization: Bearer $KLOAKD_API_KEY" -H "Content-Type: application/json" \
  -d '{"target_url": "https://example.com/account", "method": "cookies",
       "cookies": [{"name": "sid", "value": "…", "domain": ".example.com"}],
       "user_agent": "Mozilla/5.0 …"}'

The response has status: "ready", the session_artifact_id and next.status (the session's status route).

| Status | When | |--------|------| | 422 | target_url is not an allowed http/https address; method=cookies without cookies or without user_agent; or none of the cookies is the site's | | 502 | The login could not be started, or the session could not be stored | | 503 | No browser could take the interactive login just now |

How a session is protected

A login session belongs to your organisation only and lives 8 hours. So that the site sees one person, it is used with one browser identity and one exit for its life, one request at a time, at a human pace: by default at least 8 seconds between requests plus up to 7 seconds of jitter, and 120 requests an hour at most. A request waits up to 180 seconds for its turn. The response's protections object gives the values in force.

When the site signs the account out, rate-limits it or challenges it, the session is paused, not retried. A fetch made with it then reports the pause in its session field (fetch()).

GET /api/v1/organizations/{org_id}/fetchyr/sessions/{artifact_id} returns the session's state, never its cookie values:

| Field | Description | |-------|-------------| | status | active, paused, EXPIRED or INVALIDATED | | target_domain, auth_method | The site, and how the session was made | | created_at, expires_at, last_used_at | Times | | paused_at, pause_reason, pause_message | When paused: logged_out, rate_limited or challenged, and what to do | | identity | What the session presents to the site | | requests_this_hour | Requests counted against the hourly cap | | protections | The pacing values above |

POST /api/v1/organizations/{org_id}/fetchyr/sessions/{artifact_id}/resume lifts a pause. Check the account in your own browser first: a session paused for a rate limit or a challenge is paused again at the next sign. It answers 409 when the session is not paused, and 409 for a logged_out session, which cannot be resumed: log in again.

Log in with an auth-chain discovery

When a login needs input along the way (credentials, an MFA code), use an auth-chain discovery. KLOAKD drives a browser to the target page, pauses when it needs something from you, and ends with a session artifact.

import time

d = client.fetchyr.discover_auth_chain({"target_url": "https://example.com/dashboard"})
discovery_id = d["discovery_id"]

while True:
    st = client.fetchyr.get_auth_chain_status(discovery_id)
    if st["completed"]:
        break
    if st["waiting_for_intervention"]:
        if st["intervention_type"] == "credentials":
            client.fetchyr.resume_auth_chain_credentials(
                discovery_id, {"username": "user@example.com", "password": "secret"},
            )
        elif st["intervention_type"] == "mfa":
            client.fetchyr.resume_auth_chain_mfa(discovery_id, {"mfa_code": "123456"})
    time.sleep(2)

if st["success"]:
    session_artifact_id = st["session_artifact_id"]
else:
    print("Login failed:", st["error"])
  • POST …/fetchyr/auth-chain/discover ({"target_url": …}) answers 202 with discovery_id.
  • GET …/fetchyr/auth-chain/{discovery_id}/status returns completed, success, session_artifact_id (when it succeeded), steps_count, duration_seconds, waiting_for_intervention, intervention_type (credentials, mfa, captcha or generic) and error.
  • POST …/auth-chain/{discovery_id}/resume-credentials takes username, password and remember (true stores them, encrypted, for later runs). POST …/auth-chain/{discovery_id}/resume-mfa takes mfa_code.
  • A pause waits up to 10 minutes for your answer.

A fetch or extraction made without a session that lands on a login form starts one of these for you: the API's response has auth_required: true, its discovery_id, and login_options, which lists both ways to log in with your own account. The SDK results carry auth_required / authRequired and discovery_id / discoveryId; login_options is in the raw response.

detect_forms / fill_form

forms = client.fetchyr.detect_forms("https://example.com/checkout")
print(forms.total_forms, forms.login_form_detected)
for field in forms.forms:
    print(field)

client.fetchyr.fill_form(
    "https://example.com/checkout",
    form_data={"email": "user@example.com"},  # field name → value
    session_artifact_id=session.artifact_id,
)

detect_forms reads the page with a plain fetch and does not use a session_artifact_id, so it sees what a logged-out visitor sees. fill_form does use the session.

When no crawler ran the request

authenticate, detect_forms and fill_form run in a browser on one of KLOAKD's crawlers. When the task did not run to its end there, the API answers 503 with an object in detail:

{
  "detail": {
    "error": "crawler_task_not_run",
    "reason": "no_crawler_free",
    "message": "No crawler was free to run this request; try again shortly"
  }
}

| reason | message | |----------|-----------| | no_crawler_free | No crawler was free to run this request; try again shortly | | episode_overran | The request ran past its time limit on the crawler | | episode_task_failed | The request failed on the crawler | | not_run | The request did not run on a crawler |

Read detail.message for the text, not detail itself. The SDKs do not retry it (these are POST calls). From the SDK release after 0.7.0 it is CrawlerTaskNotRunError (Java: CrawlerTaskNotRunException), a kind of ApiError with reason (getReason() in Java, Reason in Go) and the message; before that it is an ApiError (503) whose code is crawler_task_not_run.

detect_mfa

mfa = client.fetchyr.detect_mfa("https://example.com/mfa")
print(mfa.mfa_detected, mfa.mfa_type, mfa.message)

detect_mfa checks a page for an MFA prompt (mfa_type is totp, sms, email or push). It reads the page with a plain fetch (no session), and it starts no challenge: challenge_id is always null. To answer an MFA prompt during a login, use an auth-chain discovery and resume_auth_chain_mfa.

submit_mfa (POST …/fetchyr/mfa-queue/{challenge_id}/resolve) only marks a queued challenge as resolved; nothing waits for it, and it continues no login.

Workflows

Workflows are not offered yet: a run performs only its navigate steps, not the fill, click or submit steps a login needs. To log in, use an auth-chain discovery.

check_duplicate

result = client.fetchyr.check_duplicate("https://example.com/p/1")
print(result.is_duplicate, result.original_url, result.similarity_score)

Long requests

authenticate, detect_forms, fill_form and detect_mfa can take a while. If one is still running after about 85 seconds the API answers 202 with a job_id and a poll_url (/api/v1/organizations/{org_id}/handoff/{job_id}); poll it until status is completed (the response is in result) or failed. See Long fetches.

Next steps

Was this page helpful?