BLOG
Virtual Browser API: How to Connect Code to Hosted Chromium
A virtual browser API lets you drive hosted Chromium via CDP. Learn how to connect Playwright, Selenium, and AI agents to remote sessions.
# Virtual Browser API: How to Connect Code to Hosted Chromium
A virtual browser API is the missing infrastructure layer between your code and a real, running browser instance. Instead of launching Chrome locally on a developer machine or a fragile CI runner, you connect to a hosted Chromium session over the network. This approach matters for AI agents that need persistent sessions, for teams that need to scale browser workloads, and for anyone who has ever fought with Chrome's resource usage in a Docker container.
The core idea is simple: the browser lives somewhere else, and your code talks to it through a standard protocol. For most modern tooling, that protocol is the Chrome DevTools Protocol (CDP). Playwright, Puppeteer, and Selenium can all connect to a remote browser via CDP, which means a virtual browser API can serve as a drop-in replacement for your local browser setup.
What Is a Virtual Browser API?
A virtual browser API is a service that provisions and manages browser instances on remote infrastructure. You make an API call to create a session, receive a connection endpoint (usually a WebSocket URL for CDP), and then drive that browser as if it were running locally.
The "virtual" part is key. You don't need to know where the browser runs, what hardware supports it, or how the underlying Chromium process is managed. You just need a stable endpoint and a standard protocol.
This differs from a traditional browser automation setup in several important ways:
- No local installation: You don't need Chrome, Chromium, or Firefox installed on your machine or CI runner.
- No process management: You don't need to handle crashes, restarts, or orphaned processes.
- Session persistence: The browser can stay alive between requests, which is critical for stateful workflows.
- Network location: The browser runs on infrastructure with better bandwidth and IP diversity than your laptop.
Why Teams Need a Virtual Browser API
The search queries around this topic reveal the real pain points. Developers ask about keeping browser sessions alive across cloud workers, giving AI agents browser access in production, and scaling Playwright workloads reliably. These are not theoretical concerns—they are the daily reality of browser automation at scale.
The Local Browser Problem
Running browsers locally works for small test suites. But it breaks down quickly:
- Resource contention: Each Chromium instance can consume 200-500 MB of RAM. Running ten parallel sessions on a single machine is impractical.
- Flaky infrastructure: CI runners are ephemeral. If a browser crashes mid-suite, you lose the session state.
- Network restrictions: Local browsers are subject to your network's IP reputation, which can trigger bot detection on many sites.
- Session loss: When a worker dies, the browser dies with it. Any cookies, local storage, or login state is gone.
The AI Agent Problem
AI agents that browse the web have a different set of requirements. They need to maintain context across multiple steps, handle authentication, and sometimes run for extended periods. A virtual browser API provides the session persistence and isolation that agents need.
Consider a typical agent workflow: the agent logs into a dashboard, navigates through several pages, extracts data, and performs an action. If the browser session dies between steps, the agent must start over. With a hosted session, the browser stays alive independently of the agent's execution environment.
How a Virtual Browser API Works
The typical flow for connecting to a virtual browser API looks like this:
- Create a session: Make an API request to provision a browser instance.
- Receive connection details: The API returns a WebSocket endpoint and session ID.
- Connect via CDP: Use Playwright, Puppeteer, or Selenium to connect to the endpoint.
- Drive the browser: Execute your automation logic as if the browser were local.
- Manage the session: Keep it alive, extend it, or terminate it as needed.
Here's a concrete example using Playwright and TypeScript:
import { chromium } from 'playwright';
// Create a session via the virtual browser API
const createSession = async () => {
const response = await fetch('https://api.remote-browser.dev/v1/sessions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.REMOTE_BROWSER_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
// Request a persistent session with a specific profile
persistent: true,
profileId: 'my-agent-profile',
// Optional: configure proxy or other browser settings
browserSettings: {
viewport: { width: 1280, height: 720 }
}
})
});
const session = await response.json();
return session;
};
// Connect to the remote browser via CDP
const connectToBrowser = async (cdpUrl: string) => {
const browser = await chromium.connectOverCDP(cdpUrl);
const context = browser.contexts()[0];
const page = context.pages()[0] || await context.newPage();
await page.goto('https://example.com');
console.log(await page.title());
// The browser stays alive after your script ends
// You can reconnect later with the same session ID
return browser;
};
// Main flow
const session = await createSession();
const browser = await connectToBrowser(session.cdpUrl);
// ... do work ...
// Don't close the browser if you need to reconnect laterThe key detail here is connectOverCDP. This Playwright method connects to an existing browser instance rather than launching a new one. That's what makes the virtual browser API work—you're attaching to a browser that someone else manages.
CDP: The Common Language
The Chrome DevTools Protocol is the foundation of most modern browser automation. It defines a set of commands and events that allow external tools to inspect, control, and debug Chromium-based browsers.
Playwright's connectOverCDP method is the most common way to connect to a remote browser. According to the Playwright documentation, this method connects to an existing browser instance that was started with the --remote-debugging-port flag.
The protocol itself is documented at the Chrome DevTools Protocol repository. It covers everything from page navigation to network interception to JavaScript execution.
Browser Support Considerations
One common question is whether CDP works with Firefox. The answer is nuanced. CDP is a Chrome-centric protocol. While Firefox has implemented some CDP support, it's incomplete and not recommended for production use. Playwright's own documentation notes that connectOverCDP is primarily for Chromium-based browsers.
If you need Firefox support, you have two options:
- Use the WebDriver BiDi protocol, which Playwright supports for Firefox.
- Use a virtual browser API that abstracts the protocol difference and exposes a consistent interface.
For most production workloads, Chromium is the safer choice. It has the most complete CDP implementation, the best tooling support, and the largest ecosystem of extensions and debugging tools.
Comparing Virtual Browser API Options
When evaluating a virtual browser API, you need to consider several factors. The table below compares the key criteria:
| Criterion | Local Browser | Basic Cloud VM | Virtual Browser API |
|---|---|---|---|
| Setup time | Minutes (install Chrome) | Hours (provision VM, install deps) | Seconds (API call) |
| Session persistence | Lost on machine shutdown | Survives VM restarts | Survives worker restarts |
| Scaling | Limited by local resources | Manual VM provisioning | Automatic session provisioning |
| IP diversity | Single local IP | Depends on VM provider | Configurable per session |
| Resource isolation | Shared with other processes | VM-level isolation | Per-session isolation |
| Protocol support | Full CDP locally | Full CDP on VM | CDP over WebSocket |
| Cost model | Hardware + electricity | VM hourly rate | Per browser-hour |
The trade-off is clear: a virtual browser API trades a bit of control for significant operational simplicity. You don't manage the underlying infrastructure, but you also don't have to.
Production Criteria for a Virtual Browser API
Not all virtual browser APIs are created equal. When evaluating options, consider these production criteria:
Session Persistence
Can you keep a browser session alive across multiple requests or worker instances? This is critical for AI agents that need to maintain state. Look for APIs that let you create persistent sessions with stable session IDs.
Profile Management
Can you save and restore browser profiles? This includes cookies, local storage, and other session data. A good virtual browser API should let you associate a profile with a session, so your agent can log in once and reuse that authentication state.
Connection Stability
How does the API handle network interruptions? A robust implementation should allow reconnection to the same session without losing state. This is where CDP's design shines—you can disconnect and reconnect as long as the browser process stays alive.
Resource Controls
Can you limit CPU, memory, or concurrent sessions? This matters for cost control and for preventing a runaway agent from consuming excessive resources. Look for APIs that provide usage controls and session timeouts.
Observability
Can you see what the browser is doing in real time? A live viewer or session recording is invaluable for debugging AI agent behavior. You need to see the page as the agent sees it, not just read logs.
Use Cases for a Virtual Browser API
The practical applications of a virtual browser API span several domains:
AI Web Agents
AI agents that browse the web need a reliable browser runtime. A virtual browser API provides the session persistence and isolation that agents require. The agent can run in one environment (a serverless function, a container, a VM) while the browser runs in another.
This separation is powerful. If the agent's execution environment crashes, the browser session survives. The agent can restart and reconnect to the same session, picking up where it left off.
Browser Automation at Scale
Teams running Playwright or Selenium test suites need to scale horizontally. A virtual browser API lets you provision numerous browser sessions on demand, without managing a browser farm.
The key advantage is elasticity. You can spin up 50 sessions for a test run and tear them down when you're done. You only pay for the browser-hours you actually use.
Web Scraping and Data Collection
Scraping requires careful session management and IP diversity. A virtual browser API with configurable proxy settings lets you route traffic through different IPs while maintaining consistent browser state.
Remote Debugging and Support
Sometimes you need to see what a user or agent is experiencing. A virtual browser API with a live viewer lets you watch browser sessions in real time, which is invaluable for debugging and support.
Getting Started with Remote Browser
Remote Browser provides a virtual browser API designed for AI agents and browser automation workloads. It offers hosted Chromium sessions with CDP access, persistent profiles, and Playwright/Puppeteer/Selenium compatibility.
The workflow is straightforward:
- Create an account and get an API key.
- Provision a session via the API or dashboard.
- Connect using Playwright's
connectOverCDPor your preferred tool. - Run your workload with the confidence that the browser is managed and monitored.
For detailed implementation guidance, see our documentation or explore how remote browsers work for AI agents. If you're evaluating costs, check our pricing page for current rates.
Practical Considerations
Before you integrate a virtual browser API, consider these practical points:
Latency
Every CDP command travels over the network. While the overhead is usually minimal (single-digit milliseconds on a good connection), it's not zero. For most automation workloads, this is irrelevant. For pixel-perfect performance testing, it might matter.
Security
Your automation code will send sensitive data—cookies, tokens, form data—over the connection. Ensure your virtual browser API provider uses TLS encryption and offers session isolation. You should also rotate API keys regularly and use short-lived session tokens where possible.
Cost Modeling
Virtual browser APIs typically charge per browser-hour. This is predictable and scales with usage. The key is to design your workloads to be efficient: reuse sessions where possible, close idle sessions, and avoid keeping browsers alive unnecessarily.
Fallback Strategy
Even the best virtual browser API can have outages. Design your automation with retry logic and graceful degradation. If a session becomes unavailable, your code should be able to create a new session and recover state from a persistent profile.
Conclusion
A virtual browser API is the practical answer to a fundamental problem: how to give your code—whether it's a test suite, a scraper, or an AI agent—reliable access to a real browser without the operational burden of managing browser infrastructure.
The technology is mature. CDP is a well-documented protocol, Playwright and Selenium have robust remote connection support, and hosted browser services have proven their reliability in production. The remaining challenge is choosing the right provider and designing your workloads to take advantage of session persistence and isolation.
For AI agents especially, the virtual browser API is not a nice-to-have—it's the difference between a demo that works on your laptop and a system that works in production. The browser needs to be as reliable as the database or the API server. A virtual browser API makes that possible.
If you're building browser automation or AI web agents, evaluate how a virtual browser API fits into your architecture. The shift from local to hosted browsers is not just about convenience—it's about building systems that can actually run unattended in production.