Cookie Script API
The nt.cookies API lets you interact with cookie jars from pre-request and post-response scripts. All methods are async, so use await.
Methods
Section titled “Methods”Get All Cookies
Section titled “Get All Cookies”const cookies = await nt.cookies.getAll();for (const c of cookies) { console.log(c.name, '=', c.value, '(domain:', c.domain + ')');}Returns an array of all cookies across all jars.
Get Cookies for a URL
Section titled “Get Cookies for a URL”const cookies = await nt.cookies.getCookiesForUrl('https://api.example.com/users');const session = cookies.find(c => c.name === 'sessionId');if (session) { console.log('Session:', session.value);}Returns cookies that match the given URL by domain and path, the same matching logic used during auto-injection.
Set a Cookie
Section titled “Set a Cookie”await nt.cookies.setCookie({ name: 'sessionId', value: 'abc123', domain: 'api.example.com', path: '/', secure: true, httpOnly: true, sameSite: 'Lax'});Adds a new cookie or updates an existing one with the same name and domain.
Delete a Cookie
Section titled “Delete a Cookie”await nt.cookies.deleteCookie('api.example.com', 'sessionId');Removes a specific cookie by domain and name.
Clear All Cookies
Section titled “Clear All Cookies”await nt.cookies.clearAll();Removes every cookie from all jars.
Cookie Object Shape
Section titled “Cookie Object Shape”{ name: string // Cookie name value: string // Cookie value domain: string // Domain scope path: string // Path scope expires?: number // Unix timestamp (undefined = session cookie) httpOnly?: boolean // Default: false secure?: boolean // Default: false sameSite?: string // "Strict", "Lax", or "None"}Examples
Section titled “Examples”Extract and reuse a CSRF token
Section titled “Extract and reuse a CSRF token”// Post-response: save the CSRF token from Set-Cookieconst cookies = await nt.cookies.getCookiesForUrl(nt.request.url);const csrf = cookies.find(c => c.name === 'csrfToken');if (csrf) { nt.setVar('csrfToken', csrf.value);}Then in subsequent requests, use {{csrfToken}} in a header:
X-CSRF-Token: {{csrfToken}}Set up a session before a test run
Section titled “Set up a session before a test run”// Pre-request: inject a known session cookieawait nt.cookies.setCookie({ name: 'session', value: nt.getVar('TEST_SESSION_ID'), domain: 'api.example.com', path: '/',});Clean up after a test
Section titled “Clean up after a test”// Post-response: remove all cookies after the final requestawait nt.cookies.clearAll();console.log('Cookies cleared');Log all cookies for debugging
Section titled “Log all cookies for debugging”const all = await nt.cookies.getAll();console.log('Total cookies:', all.length);for (const c of all) { console.log(` ${c.domain}: ${c.name}=${c.value}`);}