NEWStandard createTask and in.php APIs. Change the address and you are connected.View API docs
GATESOLVER

AI and automation

How do I manage cookies and sessions in automation?

Treat a session as one bundle: cookies and local storage, plus the IP, user agent and locale that created them. Save and restore the bundle together, give each worker its own, and replace it when it expires or gets flagged.

The short answer

Sites recognize you by the state you carry. A session is the cookies and storage a site set, but also the context those were issued in: the IP address, the user agent, the language. Many sites, and especially bot protection cookies, check that this context stays the same. So manage sessions as units: one bundle per identity, stored and loaded as a whole, used by one worker at a time, and retired when it stops working.

What belongs in a session bundle

PartWhy it matters
Cookieslogin, preferences, clearance proofs
Local storage and IndexedDBmany apps keep auth tokens here, not in cookies
Proxy or sticky session idIP-bound cookies fail from another address
User agent and client hintsclearance cookies are tied to them
Locale, time zone, viewportconsistency signals for risk scoring
Created and last-used timesto retire sessions before they expire mid-job

Saving and restoring in Playwright

Playwright stores cookies and local storage in one call, which makes it convenient for bundles:

import json, pathlib
import playwright.sync_api as pws

slot = pathlib.Path("sessions/acct-07")
meta = json.loads((slot / "meta.json").read_text())

with pws.sync_playwright() as pw:
    nav = pw.chromium.launch(proxy={"server": meta["proxy"]})
    ctx = nav.new_context(
        storage_state=str(slot / "state.json") if (slot / "state.json").exists() else None,
        user_agent=meta["user_agent"], locale=meta["locale"], timezone_id=meta["tz"])
    page = ctx.new_page()
    page.goto("https://portal.example.com/reports")
    # ...work...
    ctx.storage_state(path=str(slot / "state.json"))
    nav.close()

The metadata file keeps proxy, user agent, locale and time zone next to the state, so the session is always reopened in the same context. Puppeteer users can do the same with cookie export plus a userDataDir, as described on saving cookies in Puppeteer.

Session pools for many workers

  • Lease, do not share. A worker checks a session out, uses it, and returns it. Two workers on one session double its rate and confuse the site.
  • Track health. Record per session how many requests succeeded, how many challenges appeared, and when it last worked.
  • Retire on signals. Redirects to login, repeated challenges or 403s mean the session is spent. Replace it instead of retrying.
  • Warm new sessions. Let a new session visit a few ordinary pages before heavy work, so it is not a stranger hitting deep URLs.

Detecting expiry

Sessions end without warning: a cookie expires, the server revokes a token, or the site logs out idle users. Check for it explicitly after loading state: request a page that only works when logged in, or look for a login form. Read cookie expires values when saving and refresh sessions a little before they run out. A session that expires halfway through a multi-step job is the most common source of half-finished data.

Clearance cookies from challenges

Cookies earned by passing a bot check are especially worth keeping, and especially strict about context. cf_clearance works only with the user agent and IP that obtained it; aws-waf-token and datadome behave similarly. When you get them through GateSolver, store them in the session bundle together with the user agent from the solution and the proxy you passed to the task. Then every request in that session can reuse them until they expire, instead of solving again. The article on reusing cf_clearance cookies details the rules.

HTTP clients and browsers together

A common hybrid is to obtain a session in a browser and use it from a fast HTTP client. Export cookies as a Cookie header or load them into the client's jar, copy the same user agent, and route through the same proxy. Keep writing new cookies the server sets back into the bundle, or the client and browser versions drift apart.

Security

Saved sessions are credentials. Store them encrypted or with restricted file permissions, keep them out of logs and repositories, and delete those you no longer need. Only keep sessions for accounts you are authorized to use.

Common questions

Are cookies enough to keep a login?

Not always. Many apps store tokens in local storage, so save storage state as well as cookies.

Can several workers share one session?

It is better not to. Lease each session to one worker at a time to keep its rate and behavior plausible.

Why does a restored session log me out?

It may have expired, or the site sees a different IP or user agent from the one that created it.

How do I keep a cf_clearance cookie working?

Use it with the same user agent and IP that obtained it, and replace it when it expires.

Where should I store saved sessions?

Somewhere private with restricted access, treated like passwords and kept out of version control.

Keep reading