Docs/Ports
#

Ports

A sandbox can declare several ports, and each one gets its own public URL:

yaml
ports:
  web: { port: 3000, access: public }
  api: { port: 3001, access: public }

Why several ports

Bundling web and API into one sandbox means a single claimed environment serves both UI flows (driving the browser against web's URL) and API sequences (calling api's URL directly) against the same underlying data — the workspace, projects and tasks a UI test just created through the browser are the same rows an API test can immediately query. There's no separate "API sandbox" to keep in sync with the "UI sandbox."

access

public gets a real, routable URL a test or a human can hit from outside the sandbox. private stays internal — reachable by other services in the sandbox, not from outside it.

{port} params

A params entry of the form {port: <name>} resolves to that port's own public URL, so the app (and its build/prepare steps) can be handed its own public origin rather than hardcoding it:

yaml
params:
  web_url: { port: web }
  api_url: { port: api }
env:
  PUBLIC_URL: ${params.web_url}
  API_URL: ${params.api_url}

Each {port} param gives the app its own public URL for exactly the port it names — web_url always resolves to web's URL, api_url to api's, and so on for however many ports a definition declares.

{ port: emulation } is also allowed when the definition declares an emulator under emulation:. It resolves to the emulator's public origin (the implicit emulation port), which is the issuer for the oidc plugin:

yaml
params:
  issuer_url: { port: emulation }

Without a declared emulator, emulation is an undeclared port and validation fails.

Each port and copy has its own origin

A public port's URL is https://<label>.sbx.<zone>: <label> is 32 hex characters, different for every port of every copy, and the suffix after it (sbx.runcofactor.com in production today, sbx.dev.cofactor-preview.app on dev) is Cofactor's and can change. A copy's web and api are therefore on different hosts, and nothing in web's host tells you api's. A claimed copy's URLs are in cofactor sandbox show <id>.

So the app must never hard-code a copy's origin, bake one into a built bundle, derive one port's host from another's, or detect a sandbox by its hostname pattern. An app that does breaks in every copy but the one it was built for, and a bundle with an empty or relative API base crashes on new URL(). Get origins from the definition. In order of preference:

  1. One origin. If the API can share the web host, have the web service serve it under a relative path (/api) by proxying to the API on its private port, and call that path. Nothing to configure.

  2. Placeholder origin, swapped when served. Use this when the API is on its own port. Build the bundle once with a fixed placeholder origin that cannot resolve (https://app-api.invalid). Serve the built files from a small static server that replaces the placeholder in every .js and .html response with the copy's API origin, which a {port} param puts in the service's env. The service needs reload_on_prepare: true so each copy's prepare restarts it with its own value:

    yaml
    ports:
      web: { port: 3000, access: public }
      api: { port: 3001, access: public }
    params:
      api_url: { port: api }
    env:
      API_URL: ${params.api_url}
    services:
      web:
        run: exec bun serve-web.ts dist 3000
        health: { http: 3000, path: / }
        after_build: true
        reload_on_prepare: true
    lifecycle:
      build:
        - name: web
          run: VITE_API_URL=https://app-api.invalid bunx vite build
    
    ts
    // serve-web.ts excerpt: inside the request handler
    const PLACEHOLDER = 'https://app-api.invalid';
    return new Response((await file.text()).replaceAll(PLACEHOLDER, process.env.API_URL!));
    

    The API must also take the web origin and its own (CORS, OAuth redirect URIs) from env, which reload_on_prepare refreshes the same way.

    Optional, to skip that restart on copies restored from a snapshot: make every port param live: true and read API_URL from /var/lib/cofactor/live-env.json on each request, falling back to process.env.API_URL before the first prepare (see definition). The API then reads its origins from the file per request too. Do this only if the app can re-read per request; see authentication.

  3. Read the web port's own origin from window.location.origin at runtime. That is only the web port's origin, never the API's.

Env reaches a service when the sandbox is prepared, after the image is built, so it cannot reach a bundle built by lifecycle.build (see env): that is why the build holds a placeholder and the server fills it in. If the web service cannot do 1 or 2, say so in the plan as an open question for the human; do not bake an origin in.

Limits

At most 8 ports total per sandbox — a platform limit. That count includes the implicit emulation port that gets added automatically the moment any plugin is declared under emulation: (see the emulation topic) — it isn't optional and isn't something you declare yourself, but it does count against the 8. emulation is also a reserved port name: you can't declare your own port called emulation. The same goes for the implicit object-storage port that object_storage adds (see the object-storage topic), and the implicit public sync port every sandbox has, which cofactor sandbox sync writes your working tree through (see the local topic): a definition can declare at most 7 ports of its own (fewer with emulation or object_storage), and sync and port 7099 are reserved. cofactor sandbox validate checks the reservations and the 8-port cap. The sync port is never shown as one of the sandbox's URLs.

Ports 80 and 443 are reserved for the emulator whenever emulation is on — a definition can't put its own web or api port there in that case.

To reach a port from your own machine, public or private, see cofactor sandbox forward in the playwright topic.

OAuth callbacks

Map the callback through the application's existing configuration, e.g. GOOGLE_REDIRECT_URI: ${params.api_url}/auth/callback. Use the port that serves the actual callback, not necessarily the web port. Services that read this environment variable at startup need reload_on_prepare: true. Read cofactor sandbox guide v2 authentication before using live: true or configuring Google emulation.

https://docs.runcofactor.com/reference/sandbox-emulation#callback-configuration-and-app-changes