> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gravixlayer.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Work the cookie jar

> Read, set, and clear cookies without exporting a whole storage state

Read the browser's cookie jar directly, seed a session cookie, or clear a context's jar — lighter than exporting a full [storage state](https://docs.gravixlayer.ai/documentation/agentruntime/browser-use/contexts.md) when all you need is the cookies.

CLI and cURL examples use `$RT` and browser `b1` ([Sessions](https://docs.gravixlayer.ai/documentation/agentruntime/browser-use/browser.md)).

## Read and set cookies

`cookies` returns `{cookies, count}` — each entry `{name, value, domain, path, expires, secure, httpOnly, sameSite}` — for the default context, one `context_id`'s jar, or cookies visible to one `url`. `set_cookie` writes one cookie; scope it with `url` (preferred — derives domain/path) or a `context_id`, and pass `attrs` for the rest — `domain`, `path`, `expires`, `secure`, `httpOnly`, `sameSite` — all string values, e.g. `{"secure": "true", "sameSite": "Lax"}`.

```python Python theme={null}
from gravixlayer import GravixLayer

client = GravixLayer()  # reads GRAVIXLAYER_API_KEY
sandbox = client.runtime.create(template="browser-small")
bid = sandbox.browser.open(url="https://example.com")["browser_id"]

jar = sandbox.browser.cookies(bid)
print([(c["name"], c["domain"]) for c in jar["cookies"]])

sandbox.browser.set_cookie(
    bid, "session", "tok_123",
    url="https://example.com",
    attrs={"secure": "true", "httpOnly": "true", "sameSite": "Lax"},
)

sandbox.browser.delete_cookie(bid, "session", url="https://example.com")
sandbox.browser.clear_cookies(bid)  # the whole jar, default context
sandbox.kill()
```

```typescript TypeScript theme={null}
import { GravixLayer } from 'gravixlayer';

const client = new GravixLayer();
const sandbox = await client.runtime.create({ template: 'browser-small' });
const bid = (await sandbox.browser.open({ url: 'https://example.com' })).browserId;

const jar = await sandbox.browser.cookies(bid);
console.log((jar.cookies as { name: string }[]).map((c) => c.name));

await sandbox.browser.setCookie(bid, 'session', 'tok_123', {
  url: 'https://example.com',
  attrs: { secure: 'true', httpOnly: 'true', sameSite: 'Lax' },
});

await sandbox.browser.deleteCookie(bid, 'session', { url: 'https://example.com' });
await sandbox.browser.clearCookies(bid); // the whole jar, default context
await sandbox.kill();
```

```bash CLI theme={null}
gravixlayer runtime browser cookies "$RT" --browser b1

gravixlayer runtime browser cookie-set "$RT" --browser b1 session tok_123 \
  --url https://example.com \
  --attrs '{"secure":"true","httpOnly":"true","sameSite":"Lax"}'

gravixlayer runtime browser cookie-delete "$RT" --browser b1 session \
  --url https://example.com
gravixlayer runtime browser cookie-clear "$RT" --browser b1
```

```bash cURL theme={null}
API="https://api.gravixlayer.ai/v1"

curl -sS -X POST "$API/agents/runtime/$RT/browser/b1/cookies" \
  -H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
  -H "Content-Type: application/json" -d '{}'

curl -sS -X POST "$API/agents/runtime/$RT/browser/b1/cookies" \
  -H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"op":"set","name":"session","value":"tok_123","url":"https://example.com","attrs":{"secure":"true","httpOnly":"true"}}'

curl -sS -X POST "$API/agents/runtime/$RT/browser/b1/cookies" \
  -H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
  -H "Content-Type: application/json" -d '{"op":"clear"}'
```

> [!NOTE]
>
> Cookies inside an isolated [context](https://docs.gravixlayer.ai/documentation/agentruntime/browser-use/contexts.md) never appear in the default jar — pass its `context_id` to work that jar instead. To carry a whole session (cookies plus origins) across runtimes, export a [storage state](https://docs.gravixlayer.ai/documentation/agentruntime/browser-use/contexts.md); to carry the *entire* browser state, export a [profile](https://docs.gravixlayer.ai/documentation/agentruntime/browser-use/profiles.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.