Skip to content
James Ross
articlePublished Updated 9 min read

Turning a browser-only AI tool into an HTTP API

By James Ross

A browser-only tool becomes an HTTP API by wrapping it in a service that owns the browser, turns every request into a queued job, and returns a job id instead of holding the connection open. The wrapper has to solve four things the browser does not: a queue with real deadlines, a confirmation gate before anything paid runs, retries that do not double-charge, and access that does not require exposing a browser to the public internet.

A browser window sealed inside a service box with a job queue conveyor, showing a browser tool HTTP API.

This sits beside the company AI workspace rollout as the other half of the same abstraction: one gateway hides a command-line tool behind a chat-completions API, and this bridge hides a browser behind a job API.

Google Flow has no public developer API. That single fact is why the bridge exists. The generation work is real and useful, the interface is a web page, and every caller that wants an image or a clip should not have to know that.

Why a browser-only tool needs an HTTP bridge

The tool in question is a heavily extended fork of an existing MCP server: a process that speaks the Model Context Protocol over stdio and exposes tools such as generate, download and queue status. That is the right shape for an assistant driving it interactively. It is the wrong shape for anything else. A stdio server is tied to one client process, cannot be called from a web app, and dies with its parent.

An HTTP bridge adds the things a service needs. It gives the browser one owner instead of one per caller. It holds a persistent browser profile so the signed-in session survives restarts, which is a reliability property rather than a trick: a fresh profile means a fresh login on every deploy. It turns long renders into jobs. And it becomes the single place where the paid actions are gated.

The MCP surface stays: the bridge calls the same tool handlers, so there is one implementation of “generate an image” and two front doors. A fix lands in both.

The API shape: accept, then poll

The shape that works is a create call, a confirm step and a status poll. A generate call validates its arguments, creates a job, and returns 202 Accepted with a job id. A status call returns the job’s state, its progress and, when it finishes, the result. Polling every few seconds is the intended use.

202 Accepted is the right code because the work has been accepted and has not happened yet (RFC 9110). Returning 200 with a half-finished result, or holding the connection for four minutes, are both worse: the first lies, and the second gets cut by every proxy between here and the caller.

In outline:

POST /generate
{ "prompt": "studio product shot on a neutral background",
  "count": 2, "ratio": "1:1", "confirm": false }

202 Accepted
{ "status": "ready_for_confirmation", "jobId": "j_8f21",
  "staged": { "prompt_chars": 64, "references": 0 } }

POST /jobs/j_8f21/confirm
202 Accepted
{ "status": "running", "jobId": "j_8f21", "progress": 25 }

GET /jobs/j_8f21
{ "status": "completed", "progress": 100,
  "files": [ { "name": "take-1.png", "bytes": 740000 } ] }

Progress is coarse on purpose: staged, submitted, media detected, downloaded. A precise percentage would be a guess, and callers build timeouts on it.

The async path exists because of a hard external limit. MCP clients typically cut a tool call at around 120 seconds, and a real render takes longer. The bridge cannot change the client’s timeout, so it returns a job id immediately and lets the caller poll. The MCP server does the same with its own async flag.

The job queue and three deadlines

Three deadlines have to be ordered correctly, or the system fails in a way that looks like a slow render.

Deadline Typical value What it bounds
Client tool timeout about 120 s How long an MCP client waits for a tool call
Render ceiling 600 s video, 300 s image How long the browser waits for the UI to finish
Job timeout 720 s The whole job, including download and settling

The rule is client timeout < render ceiling < job timeout. The render must outlive a single blocking tool call, which is why async is the supported shape for media. The job must outlive the render it is waiting on, which is why the job timeout carries two minutes of headroom over the render ceiling for upload settling and download.

The queue runs one active job at a time, with a history of completed jobs. One browser profile cannot usefully run two generations at once, and a queue of one makes failure legible: a stuck job is visibly stuck.

The paid-action gate

The paid-action gate is the most important safety property in the system. Every generation consumes credits from a subscription, and a wrapper that fires on every request will eventually spend a batch on a typo.

The gate is a two-phase default. With confirmation off, a call stages the prompt, types it into the composer, applies the model and ratio, captures a verification screenshot, and stops. It returns ready_for_confirmation and spends nothing. Only a second call with confirmation on actually clicks generate, approves the credit dialog, waits for the render and downloads the result.

That gives a caller a cheap way to check three things before spending: that the prompt fits, that the reference images attached, and that the UI is in the mode the caller thinks it is in. The staging response reports the settings that were actually applied, not the ones that were requested, because those diverged in an earlier version and the difference was invisible.

The gate also covers downloads: a 4K upscale costs credits, so it sits behind the same confirmation.

Retries and idempotency

Retries are safe before the paid click and dangerous after it. Before confirmation, a failed attachment or a missing composer costs nothing, so a retry is free and often works. After confirmation, a retry can spend the credits twice.

So the bridge classifies rather than retries blindly:

  • Validation errors return 400 and never spawn a browser. A missing prompt, an unsupported ratio, or a reference image whose bytes do not match its declared type is the caller’s problem.
  • Queue and capacity errors return 429 with Retry-After (RFC 6585). The caller decides when to come back.
  • Browser or UI errors return 502 with the page state that caused them, and the job is marked failed. The caller can re-stage, which is safe, or re-confirm, which is not automatic.
  • Timeout errors return 504 after the browser has been returned to a clean state, not left mid-render.

The confirm call carries the job id, so a duplicate confirm for a job that already ran returns the existing result rather than starting a second generation. That is the idempotency key the whole design hangs on.

Private-network access instead of public ports

A browser-automation host is a bad thing to expose to the internet. It holds a signed-in browser profile, it can spend money, and its control surface is a small HTTP API with a bearer token.

The four bridges run on one GPU host and are reached over a private network. There is no public hostname, no inbound port and no TLS terminator in front of them; callers on the private network address them directly, and everything else cannot see them. That is a simpler security model than an authenticated public endpoint, and it removes a whole class of problems: scanning, credential stuffing and accidental discovery.

The trade-off is that only callers on that network can use them, which is the point for an internal pipeline. If a public surface is genuinely needed, the right answer is a thin authenticated API that forwards to the private bridge, not a hole in the network.

MCP stdio vs local HTTP bridge vs hosted API

Approach Who can call it Long jobs Paid-action control Exposure When it fits
MCP server over stdio One local client process Blocks the tool call Per-call confirmation flag None, it is a child process An assistant working interactively
Local HTTP bridge Any service on the private network Queued with a job id Staged then confirmed Private network only Unattended batches, internal pipelines
Hosted vendor API Anything with a key Usually asynchronous Billing-side controls Public endpoint The vendor offers the capability and the terms fit

Which should you choose? Keep the stdio server for interactive work, because that is what MCP clients are built for. Add the HTTP bridge when a scheduler, a web app or another service needs the same capability without a human in the loop. A hosted API would be simpler than either, but it does not exist for this tool, which is the reason the other two exist.

What broke and what we changed

Success was reported on tiles that were still rendering. A four-take job completed in 51 seconds and returned three files. The debug screenshot taken seconds earlier showed all four tiles at 32%. The harvester had walked back up the grid and picked up the reference stills, which arrived as JPEGs in the videos output directory, and the job still said success with credits consumed.

The fix was to gate completion on a real finished-tile signal rather than on tile presence, and to refuse a tile whose detected media type does not match the requested one. The refusal reads as data: Tile at index 4 was detected as "image", but expected "video". Candidate refused. A download error can no longer report success, and an extension whitelist keeps non-video files out of the video folder.

A fix nearly deleted a real take. Once refusals became fatal, the reference-matching rule mattered. Matching a reference by filename stem would have deleted a legitimate file whose name happened to contain the reference name, so the check now uses the full basename with extension, and a regression test pins that case.

The status tool could kill the job it was watching. The queue-timeout check ran inside the status read, so polling a long render could declare it dead. Reads are now non-mutating: status reports isExpired and timedOut and leaves the transition to the code that owns the job.

A screenshot reported a save that never happened. The tool returned { "screenshot": null, "message": "Screenshot saved." } twice, with no file on disk. The write is now read back, and the tool returns a real path and size or a real error.

The first call into a new project always failed. Attachments returned zero of three, the prompt input was missing, and one job hung at 50%. The same prompt into an existing project worked. The fix was a readiness gate: wait for the composer’s mode controls to exist before switching modes or typing, and report that readiness on the health route. Until then, the operating rule was one project per campaign rather than one per run.

Frequently asked questions

Can you use Google Flow through an API?

No. Google Flow has no public developer API, so programmatic use has to drive the web interface through a browser. That is why a bridge exists: it owns one browser, keeps the session signed in, and exposes a small HTTP API.

Why return a job id instead of waiting for the image?

Because renders routinely take longer than a client will wait. A measured single-take video render took about 227 seconds, while MCP clients typically cut a tool call at around 120 seconds. Returning a job id immediately and polling avoids a timeout that looks like a failure but leaves a real render running.

How do you stop a wrapper spending credits by accident?

By making the paid action a second step. The default call stages the prompt and returns ready_for_confirmation without clicking generate, so the caller can verify the prompt, the attachments and the mode for free. Only an explicit confirm spends credits, and a duplicate confirm returns the existing job rather than starting another.

What happens if the browser crashes mid-job?

The job fails with the page state attached, and the browser is returned to a clean state rather than left mid-render. Retrying is safe before the confirm step because nothing has been spent; after a confirm, the caller should re-stage rather than blindly re-run, since the credits are already gone.

Is it safe to expose the bridge on the internet?

No, and it does not need to be. The bridges run on a private network with no public hostname or inbound port. A browser profile that is signed in and able to spend money is a high-value target, and a private network is a simpler control than an authenticated public endpoint.