> ## 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.

# Keep a login

> Keep separate logins in one browser and reuse them later

Keep more than one login in the same browser, and reuse a login in a later runtime. A browser starts with one cookie jar. An isolated context is a second jar. Export that jar after a login and import it when you need the session again.

## Isolate a session

`create_context` returns `{context_id}`. Targets opened with that id live in
the isolated jar — their cookies never touch the default session or other
contexts. `contexts` lists every live context with its `target_count`;
the default context always exists and cannot be deleted.

`proxy` attaches a proxy server URL to the context. `storage_state` seeds
the context's jars from an exported state object.

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

client = GravixLayer()
sandbox = client.runtime.create(template="browser-small")
browser = sandbox.browser.open()
bid = browser["browser_id"]

# An isolated session — separate cookies from the default context
ctx = sandbox.browser.create_context(bid)
cid = ctx["context_id"]

tab = sandbox.browser.targets(bid, url="https://example.com", context_id=cid)
print(tab["target_id"])

print([c["context_id"] for c in sandbox.browser.contexts(bid)["contexts"]])
sandbox.browser.delete_context(bid, cid)   # jars go with it
```

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

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

// An isolated session — separate cookies from the default context
const ctx = await sandbox.browser.contextCreate(bid);
const tab = await sandbox.browser.targets(bid, {
  url: 'https://example.com',
  contextId: ctx.contextId,
});
console.log(tab.targetId, tab.targets[0]?.contextId);

console.log((await sandbox.browser.contexts(bid)).map((c) => c.contextId));
await sandbox.browser.contextDelete(bid, ctx.contextId); // jars go with it
```

```bash CLI theme={null}
CID=$(gravixlayer runtime browser context-create "$RT" --browser b1 \
  --output json | jq -r .context_id)

gravixlayer runtime browser targets "$RT" --browser b1 \
  --url https://example.com --context "$CID"

gravixlayer runtime browser contexts "$RT" --browser b1
gravixlayer runtime browser context-delete "$RT" --browser b1 "$CID"
```

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

CID=$(curl -sS -X POST "$API/agents/runtime/$RT/browser/b1/context" \
  -H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}' | jq -r .context_id)

curl -sS -X POST "$API/agents/runtime/$RT/browser/b1/targets" \
  -H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"url\":\"https://example.com\",\"context_id\":\"$CID\"}"

curl -sS "$API/agents/runtime/$RT/browser/b1/contexts" \
  -H "Authorization: Bearer $GRAVIXLAYER_API_KEY"

curl -sS -X DELETE "$API/agents/runtime/$RT/browser/b1/context/$CID" \
  -H "Authorization: Bearer $GRAVIXLAYER_API_KEY"
```

## Reuse a login

`export_storage` returns the context's cookies and origin storage in the
Playwright-compatible `{cookies, origins}` shape; `import_storage` applies
a previously exported state. An empty `context_id` (the default) addresses
the default context.

This is how sessions persist: export after a login flow, keep the state,
and import it into a later runtime — or seed a fresh context with it
directly via `create_context`.

```python Python theme={null}
# Capture the logged-in session from the default context
state = sandbox.browser.export_storage(bid)

# Later — restore it into an isolated context
ctx = sandbox.browser.create_context(bid, storage_state=state["storage_state"])

# Or into the default context of another runtime
sandbox2.browser.import_storage(bid2, state["storage_state"])
```

```typescript TypeScript theme={null}
// Capture the logged-in session from the default context
const state = await sandbox.browser.storageExport(bid);

// Later — restore it into an isolated context
const ctx = await sandbox.browser.contextCreate(bid, { storageState: state });

// Or into the default context of another runtime
await sandbox2.browser.storageImport(bid2, state);
```

```bash CLI theme={null}
# Capture the logged-in session from the default context
gravixlayer runtime browser storage-export "$RT" --browser b1 --out state.json

# Later — restore it into an isolated context
gravixlayer runtime browser context-create "$RT" --browser b1 \
  --storage-state "$(cat state.json)"

# Or into the default context of another runtime
gravixlayer runtime browser storage-import "$RT2" --browser b1 --state-file state.json
```

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

# Capture the logged-in session from the default context
curl -sS -X POST "$API/agents/runtime/$RT/browser/b1/storage/export" \
  -H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}' | jq .storage_state > state.json

# Restore it into a new isolated context
curl -sS -X POST "$API/agents/runtime/$RT/browser/b1/context" \
  -H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"storage_state\":$(cat state.json)}"

# Or import into another runtime's default context
curl -sS -X POST "$API/agents/runtime/$RT2/browser/b1/storage/import" \
  -H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"storage_state\":$(cat state.json)}"
```

The exported object is JSON-serializable — store it like any other
credential: it contains session cookies, so treat it with the same care as
the password that produced it.

## Endpoints

| Method | Path | Description |
| - | - | - |
| POST | `/v1/agents/runtime/{runtime_id}/browser/{browser_id}/context` | Open an isolated context |
| GET | `/v1/agents/runtime/{runtime_id}/browser/{browser_id}/contexts` | List contexts |
| DELETE | `/v1/agents/runtime/{runtime_id}/browser/{browser_id}/context/{context_id}` | Close a context |
| POST | `/v1/agents/runtime/{runtime_id}/browser/{browser_id}/storage/export` | Export cookies + storage |
| POST | `/v1/agents/runtime/{runtime_id}/browser/{browser_id}/storage/import` | Import cookies + storage |


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