By default, each fetch() call runs in ephemeral mode. This is a good fit for one-off requests.For multi-step flows (like login sequences), use sessions to persist state.
import { createSession } from 'wreq-js';const session = await createSession({ browser: 'chrome_142' });// All requests share the same cookie jarawait session.fetch('https://example.com/login', { method: 'POST', body: new URLSearchParams({ user: 'name', pass: 'secret' }),});// Cookies from login are automatically includedconst dashboard = await session.fetch('https://example.com/dashboard');console.log(await dashboard.text());// Always close when doneawait session.close();
Within a session, browser, os, and proxy are fixed at creation time unless you pass an explicit transport for that request.You can still override per-request values like timeout, headers, redirect, and body:
const session = await createSession({ browser: 'chrome_142', timeout: 30_000,});// Override timeout for a single requestawait session.fetch('https://example.com', { timeout: 5_000, headers: { 'accept': 'text/html,application/xhtml+xml', },});
Use setCookie(name, value, url) to inject a cookie into the session jar:
session.setCookie('auth_token', 'my-token', 'https://example.com');// Subsequent requests to example.com will include the cookieconst resp = await session.fetch('https://example.com/api');
Cookie jar: cookies are not shared between sessions
Session identifier and defaults
If you want separate cookie jars but shared transport settings (for example, multiple sessions through the same proxy), use a shared Transport per request.
// These sessions are completely isolatedconst session1 = await createSession({ browser: 'chrome_142' });const session2 = await createSession({ browser: 'firefox_139' });// Cookies set in session1 do not affect session2await session1.fetch('https://example.com/set-cookie');await session2.fetch('https://example.com/check-cookie'); // No cookie present