|
| 1 | +--- |
| 2 | +title: Lightpanda |
| 3 | +description: Connect to the platform-hosted Lightpanda browser through CDP |
| 4 | +sidebar: |
| 5 | + order: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +Lightpanda is a lightweight browser backend exposed by CoreClaw through the Chrome DevTools Protocol (CDP). Use it when your Worker needs browser-level navigation, JavaScript execution, or page rendering without packaging or launching a browser locally. |
| 9 | + |
| 10 | +## Positioning |
| 11 | + |
| 12 | +Lightpanda is **not** a browser automation framework. It is a platform-hosted browser endpoint. |
| 13 | + |
| 14 | +You still write automation logic with a client library such as Playwright. The difference is that Playwright connects to the Lightpanda CDP endpoint instead of starting a local browser process. |
| 15 | + |
| 16 | +## Environment Variables |
| 17 | + |
| 18 | +| Variable | Description | |
| 19 | +| --- | --- | |
| 20 | +| `LightpandaDomain` | Platform-injected Lightpanda CDP domain or endpoint | |
| 21 | +| `PROXY_AUTH` | Platform-injected credentials in `username:password` format | |
| 22 | + |
| 23 | +:::danger[Important] |
| 24 | +- Never hardcode `PROXY_AUTH` or Lightpanda credentials in Worker code. |
| 25 | +- Read `LightpandaDomain` from the runtime environment. |
| 26 | +- Pass `PROXY_AUTH` as a Basic `Authorization` header when connecting to Lightpanda. |
| 27 | +::: |
| 28 | + |
| 29 | +## Endpoint and Authentication Rules |
| 30 | + |
| 31 | +Apply these rules before calling `connect_over_cdp`: |
| 32 | + |
| 33 | +- If `LightpandaDomain` is a bare domain, normalize it to `ws://<domain>/devtools/browser/new`. |
| 34 | +- If `LightpandaDomain` is already a full `ws://`, `wss://`, `http://`, or `https://` CDP endpoint, use it as provided. |
| 35 | +- Authentication uses the HTTP `Authorization` header with the Basic scheme. Build the header from `PROXY_AUTH` in `username:password` format. |
| 36 | + |
| 37 | +## Recommended Playwright Connection |
| 38 | + |
| 39 | +Use this version when `LightpandaDomain` may be injected as either a bare domain or a full CDP endpoint. |
| 40 | + |
| 41 | +```python |
| 42 | +import asyncio |
| 43 | +import base64 |
| 44 | +import os |
| 45 | + |
| 46 | +from playwright.async_api import async_playwright |
| 47 | + |
| 48 | + |
| 49 | +def basic_auth_header(auth: str) -> str: |
| 50 | + token = base64.b64encode(auth.encode("utf-8")).decode("ascii") |
| 51 | + return f"Basic {token}" |
| 52 | + |
| 53 | + |
| 54 | +def lightpanda_cdp_endpoint(value: str) -> str: |
| 55 | + endpoint = value.rstrip("/") |
| 56 | + if endpoint.startswith(("ws://", "wss://", "http://", "https://")): |
| 57 | + return endpoint |
| 58 | + return f"ws://{endpoint}/devtools/browser/new" |
| 59 | + |
| 60 | + |
| 61 | +async def main() -> None: |
| 62 | + auth = os.environ["PROXY_AUTH"] |
| 63 | + cdp_endpoint = lightpanda_cdp_endpoint(os.environ["LightpandaDomain"]) |
| 64 | + |
| 65 | + async with async_playwright() as playwright: |
| 66 | + browser = await playwright.chromium.connect_over_cdp( |
| 67 | + cdp_endpoint, |
| 68 | + headers={"Authorization": basic_auth_header(auth)}, |
| 69 | + timeout=60000, |
| 70 | + ) |
| 71 | + try: |
| 72 | + page = await browser.new_page() |
| 73 | + await page.goto( |
| 74 | + "https://ipinfo.io/ip", |
| 75 | + wait_until="domcontentloaded", |
| 76 | + timeout=60000, |
| 77 | + ) |
| 78 | + print((await page.text_content("body") or "").strip()) |
| 79 | + finally: |
| 80 | + await browser.close() |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + asyncio.run(main()) |
| 85 | +``` |
| 86 | + |
| 87 | +## HTTP CDP Endpoint |
| 88 | + |
| 89 | +The recommended helper above already supports full HTTP CDP endpoints. If you know `LightpandaDomain` is always injected as a full HTTP endpoint, this shorter form is equivalent: |
| 90 | + |
| 91 | +```python |
| 92 | +import asyncio |
| 93 | +import base64 |
| 94 | +import os |
| 95 | + |
| 96 | +from playwright.async_api import async_playwright |
| 97 | + |
| 98 | + |
| 99 | +def basic_auth_header(auth: str) -> str: |
| 100 | + token = base64.b64encode(auth.encode("utf-8")).decode("ascii") |
| 101 | + return f"Basic {token}" |
| 102 | + |
| 103 | + |
| 104 | +async def main() -> None: |
| 105 | + auth = os.environ["PROXY_AUTH"] |
| 106 | + cdp_endpoint = os.environ["LightpandaDomain"] |
| 107 | + |
| 108 | + async with async_playwright() as playwright: |
| 109 | + browser = await playwright.chromium.connect_over_cdp( |
| 110 | + cdp_endpoint, |
| 111 | + headers={"Authorization": basic_auth_header(auth)}, |
| 112 | + timeout=60000, |
| 113 | + ) |
| 114 | + try: |
| 115 | + page = await browser.new_page() |
| 116 | + await page.goto("https://ipinfo.io/ip", wait_until="domcontentloaded") |
| 117 | + print((await page.text_content("body") or "").strip()) |
| 118 | + finally: |
| 119 | + await browser.close() |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + asyncio.run(main()) |
| 124 | +``` |
| 125 | + |
| 126 | +## Complete Worker Example with CoreSDK |
| 127 | + |
| 128 | +Use this structure in `main.py` when packaging Lightpanda automation as a CoreClaw Worker. The SDK handles input parameters, logs, table headers, and result delivery back to the platform. |
| 129 | + |
| 130 | +```python |
| 131 | +import asyncio |
| 132 | +import base64 |
| 133 | +import os |
| 134 | + |
| 135 | +from playwright.async_api import async_playwright |
| 136 | +from sdk import CoreSDK |
| 137 | + |
| 138 | + |
| 139 | +def basic_auth_header(auth: str) -> str: |
| 140 | + token = base64.b64encode(auth.encode("utf-8")).decode("ascii") |
| 141 | + return f"Basic {token}" |
| 142 | + |
| 143 | + |
| 144 | +def lightpanda_cdp_endpoint(value: str) -> str: |
| 145 | + endpoint = value.rstrip("/") |
| 146 | + if endpoint.startswith(("ws://", "wss://", "http://", "https://")): |
| 147 | + return endpoint |
| 148 | + return f"ws://{endpoint}/devtools/browser/new" |
| 149 | + |
| 150 | + |
| 151 | +async def run() -> None: |
| 152 | + CoreSDK.Log.info("Starting Lightpanda Worker...") |
| 153 | + |
| 154 | + headers = [ |
| 155 | + {"label": "url", "key": "url", "format": "text"}, |
| 156 | + {"label": "ip", "key": "ip", "format": "text"}, |
| 157 | + {"label": "html", "key": "html", "format": "text"}, |
| 158 | + {"label": "resp_status", "key": "resp_status", "format": "text"}, |
| 159 | + ] |
| 160 | + CoreSDK.Result.set_table_header(headers) |
| 161 | + |
| 162 | + input_json = CoreSDK.Parameter.get_input_json_dict() |
| 163 | + url = input_json.get("url") or "https://ipinfo.io/ip" |
| 164 | + |
| 165 | + auth = os.environ["PROXY_AUTH"] |
| 166 | + cdp_endpoint = lightpanda_cdp_endpoint(os.environ["LightpandaDomain"]) |
| 167 | + |
| 168 | + result = { |
| 169 | + "url": url, |
| 170 | + "ip": "", |
| 171 | + "html": "", |
| 172 | + "resp_status": "200", |
| 173 | + } |
| 174 | + |
| 175 | + browser = None |
| 176 | + try: |
| 177 | + async with async_playwright() as playwright: |
| 178 | + CoreSDK.Log.info("Connecting to Lightpanda...") |
| 179 | + browser = await playwright.chromium.connect_over_cdp( |
| 180 | + cdp_endpoint, |
| 181 | + headers={"Authorization": basic_auth_header(auth)}, |
| 182 | + timeout=60000, |
| 183 | + ) |
| 184 | + |
| 185 | + page = await browser.new_page() |
| 186 | + await page.goto(url, wait_until="domcontentloaded", timeout=60000) |
| 187 | + |
| 188 | + result["html"] = await page.content() |
| 189 | + result["ip"] = (await page.text_content("body") or "").strip() |
| 190 | + CoreSDK.Log.info("Lightpanda page loaded successfully") |
| 191 | + except Exception as exc: |
| 192 | + result["resp_status"] = "500" |
| 193 | + result["html"] = str(exc) |
| 194 | + CoreSDK.Log.error(f"Lightpanda run failed: {exc}") |
| 195 | + finally: |
| 196 | + if browser: |
| 197 | + await browser.close() |
| 198 | + CoreSDK.Result.push_data(result) |
| 199 | + |
| 200 | + |
| 201 | +if __name__ == "__main__": |
| 202 | + asyncio.run(run()) |
| 203 | +``` |
| 204 | + |
| 205 | +## Best Practices |
| 206 | + |
| 207 | +- Use Lightpanda for CDP-based browser automation where a lightweight hosted browser is sufficient. |
| 208 | +- Keep page-level logic in Playwright selectors and navigation APIs. |
| 209 | +- Set explicit timeouts for browser connection and page navigation. |
| 210 | +- In Worker code, use `CoreSDK.Parameter` for inputs, `CoreSDK.Log` for progress, and `CoreSDK.Result` for output. |
| 211 | +- Close the remote browser in a `finally` block. |
| 212 | +- Do not configure SOCKS5 proxy manually for browser pages; the remote browser handles outbound access. |
0 commit comments