|
| 1 | +--- |
| 2 | +title: "Profiles" |
| 3 | +description: "Persist and reuse browser session state (cookies, local storage) across sessions" |
| 4 | +--- |
| 5 | + |
| 6 | +Kernel Profiles let you capture browser state created during a session — cookies and local storage — and reuse it in later sessions. This is useful for persisting login state or other site preferences across runs. |
| 7 | + |
| 8 | +## 1. Create a profile |
| 9 | + |
| 10 | +The first step in using profiles is to create one, optionally giving it a meaningful `name` that is unique within your organization. |
| 11 | + |
| 12 | +<CodeGroup> |
| 13 | + |
| 14 | +```typescript Typescript/Javascript |
| 15 | +import { ConflictError, Kernel } from "@onkernel/sdk"; |
| 16 | + |
| 17 | +const kernel = new Kernel(); |
| 18 | + |
| 19 | +try { |
| 20 | + await kernel.profiles.create({ name: "profiles-demo" }); |
| 21 | +} catch (err) { |
| 22 | + if (err instanceof ConflictError) { |
| 23 | + // Profile already exists; continue |
| 24 | + } else { |
| 25 | + throw err; |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +```python Python |
| 31 | +from kernel import AsyncKernel, ConflictError |
| 32 | + |
| 33 | +kernel = AsyncKernel() |
| 34 | + |
| 35 | +try: |
| 36 | + await kernel.profiles.create(name="profiles-demo") |
| 37 | +except ConflictError: |
| 38 | + pass |
| 39 | +``` |
| 40 | + |
| 41 | +</CodeGroup> |
| 42 | + |
| 43 | +## 2. Start a browser session using the profile and save changes |
| 44 | + |
| 45 | +After creating the profile, you can reference it by it's `name` or `id` when creating a browser. |
| 46 | +Set `save_changes` to true to persist any state created during this session back into the profile when the browser is closed. |
| 47 | + |
| 48 | +<CodeGroup> |
| 49 | + |
| 50 | +```typescript Typescript/Javascript |
| 51 | +const kernelBrowser = await kernel.browsers.create({ |
| 52 | + profile: { |
| 53 | + name: "profiles-demo", |
| 54 | + // or |
| 55 | + // id: profile.id, |
| 56 | + save_changes: true, |
| 57 | + }, |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +```python Python |
| 62 | +kernel_browser = await kernel.browsers.create( |
| 63 | + profile={ |
| 64 | + "name": "profiles-demo", |
| 65 | + // or |
| 66 | + // "id": profile.id, |
| 67 | + "save_changes": True, |
| 68 | + } |
| 69 | +) |
| 70 | +``` |
| 71 | + |
| 72 | +</CodeGroup> |
| 73 | + |
| 74 | +## 3. Use the browser, then close it to persist the state |
| 75 | + |
| 76 | +After using a browser with `save_changes: true`, closing the browser will save cookies and local storage into the profile. |
| 77 | + |
| 78 | +<CodeGroup> |
| 79 | + |
| 80 | +```typescript Typescript/Javascript |
| 81 | +console.log("Live view:", kernelBrowser.browser_live_view_url); |
| 82 | +// Navigate and create login state ... |
| 83 | +await kernel.browsers.deleteByID(kernelBrowser.session_id); |
| 84 | +``` |
| 85 | + |
| 86 | +```python Python |
| 87 | +print("Live view:", kernel_browser.browser_live_view_url) |
| 88 | +# Navigate and create login state ... |
| 89 | +await kernel.browsers.delete_by_id(kernel_browser.session_id) |
| 90 | +``` |
| 91 | + |
| 92 | +</CodeGroup> |
| 93 | + |
| 94 | +## 4. Start a new session with the saved profile (read-only) |
| 95 | + |
| 96 | +Create another browser using the same profile name. Omitting `save_changes` leaves the stored profile untouched. |
| 97 | + |
| 98 | +<CodeGroup> |
| 99 | + |
| 100 | +```typescript Typescript/Javascript |
| 101 | +const kernelBrowser2 = await kernel.browsers.create({ |
| 102 | + profile: { name: "profiles-demo" }, |
| 103 | +}); |
| 104 | +console.log("Live view:", kernelBrowser2.browser_live_view_url); |
| 105 | +``` |
| 106 | + |
| 107 | +```python Python |
| 108 | +kernel_browser2 = await kernel.browsers.create( |
| 109 | + profile={"name": "profiles-demo"} |
| 110 | +) |
| 111 | +print("Live view:", kernel_browser2.browser_live_view_url) |
| 112 | +``` |
| 113 | + |
| 114 | +</CodeGroup> |
| 115 | + |
| 116 | +### Notes |
| 117 | + |
| 118 | +- A profile's `name` must be unique within your organization. |
| 119 | +- Profiles store cookies and local storage. Start the session with `save_changes: true` to write changes back when the browser is closed. |
| 120 | +- To keep a profile immutable for a run, omit `save_changes` (default) when creating the browser. |
| 121 | +- You can spin up multiple browsers in parallel using the same profile. |
0 commit comments