createSession()
Create a persistent session context for related requests. Within a session,browser, os, and proxy are fixed at creation time (unless you pass an explicit transport per request).
Signature
Options
Default browser fingerprint profile for all session requests.
Default operating system to emulate.
Default proxy URL for all session requests.
Default request timeout in milliseconds.
Default headers to include in every session request. Can be a
Headers object, plain object, or array of key-value pairs.Explicit session identifier. When omitted, a random ID is generated.
Accept invalid certificates for all session requests. Use only in development.
Session object
The returnedSession object has:
session.fetch(url, init?)
Make a request using the session context.session.websocket(url, options?)
Open a WebSocket that reuses the session transport and context. Use this after login or any other HTTP flow that sets cookies.session.websocket(...) accepts headers, protocols, and binaryType.
It does not accept browser, os, or proxy because those are owned by the session transport.
protocols values are validated for non-empty unique entries and sent in the Sec-WebSocket-Protocol handshake header.
session.getCookies(url)
Return cookies that would be sent to the given URL (RFC 6265 domain/path matching).Target URL. Only cookies whose domain and path match this URL are returned.
Record<string, string> — cookie name/value pairs.
session.setCookie(name, value, url)
Add a cookie to the session jar, scoped to the domain/path of the given URL.Cookie name.
Cookie value.
URL that determines the cookie’s domain and path scope.
session.clearCookies()
Clear all cookies from the session cookie jar.session.close()
Close the session and release resources. Always call this when done.Example
withSession()
Auto-disposing session helper that closes the session when the callback completes.Signature
Example
With options
Session vs. Ephemeral
| Feature | Ephemeral (default fetch) | Session |
|---|---|---|
| Cookies | Separate request context | Shared session context |
| Transport settings | Request scoped | Session scoped unless overridden by transport |
| Use case | Isolated requests | Multi-step flows |