Create a macOS VM from code and show it in your web app
Your backend creates the macOS instance with your API key and hands the browser only the instance's stream URL and a token. On the page, <RemoteControl /> from @limrun/ui streams the desktop and forwards the user's pointer and keyboard input. The Limrun console's stream page for macOS instances uses the same component.
To create an instance by hand from a terminal, see Run a macOS VM.
Before you start
- An API key, kept on your backend. Create one at console.limrun.com under Settings, API Keys. Never send it to a browser.
- A client for the macOS endpoints. The TypeScript SDK exposes them as
client.macosInstancesfrom@limrun/api0.77.1 or later, and the Go SDK asclient.MacosInstancesfrom v0.13.1. Thelim macoscommands needlim0.62.0 or later. The Python SDK has no macOS resource; call the REST endpoints under/v1/macos_instancesfrom Python. SDKs and REST API lists the install commands. @limrun/ui0.21.0 or later in your frontend, for the macOS desktop in<RemoteControl />. 0.22.0 and later draw it inside a MacBook Pro.
Create the instance on your backend
Create the instance and wait until it is ready, so its stream URL and tokens are in the response:
lim macos create --label user=user_42 --reuse-if-exists --inactivity-timeout 10m --jsonimport Limrun from '@limrun/api';
const lim = new Limrun(); // reads LIM_API_KEY from the environment
const mac = await lim.macosInstances.create({
wait: true,
reuseIfExists: true,
metadata: { labels: { user: 'user_42' } },
spec: {
jurisdiction: 'us', // optional; hard constraint on where the instance runs
inactivityTimeout: '10m', // omit to use the organization's macOS default
},
});
console.log(mac.metadata.id, mac.status.state);import (
"context"
"fmt"
limrun "github.com/limrun-inc/go-sdk"
"github.com/limrun-inc/go-sdk/packages/param"
)
client := limrun.NewClient() // reads LIM_API_KEY from the environment
ctx := context.Background()
mac, err := client.MacosInstances.New(ctx, limrun.MacosInstanceNewParams{
Wait: param.NewOpt(true),
ReuseIfExists: param.NewOpt(true),
Metadata: limrun.MacosInstanceNewParamsMetadata{
Labels: map[string]string{"user": "user_42"},
},
Spec: limrun.MacosInstanceNewParamsSpec{
Jurisdiction: "us",
InactivityTimeout: param.NewOpt("10m"),
},
})
if err != nil {
panic(err)
}
fmt.Println(mac.Metadata.ID, mac.Status.State)curl -X POST "https://api.limrun.com/v1/macos_instances?wait=true&reuseIfExists=true" \
-H "Authorization: Bearer $LIM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"metadata": { "labels": { "user": "user_42" } },
"spec": { "jurisdiction": "us", "inactivityTimeout": "10m" }
}'REST only: spec.size picks the virtual machine's size, named for its vCPUs and GiB of memory: 4c11g, 4c15g (the default), 6c23g, or 6c31g.
wait: true returns once the instance is ready. reuseIfExists: true returns an existing instance with exactly the same labels instead of creating another one, so a user who reloads the page gets the same virtual machine back; see Labels and reuse. Without inactivityTimeout and hardTimeout, the instance gets your organization's macOS defaults; see Set the timeouts.
The ready instance's status carries what the browser needs:
| Field | Use |
|---|---|
endpointWebSocketUrl | The desktop stream. Pass it as the url prop of <RemoteControl />. |
token | Control token for this instance. Keep it on your backend. |
streamToken | Opens the desktop stream and nothing else. Its holder can see and control the desktop, but cannot reach the instance API or change the instance record. Hand it to the browser so token stays on your backend. |
streamToken is returned only to callers that control the instance, and get and list return it only while the instance runs. For every credential and who should hold it, see Credentials.
Show the desktop in your web app
Install the UI package in your frontend:
npm install @limrun/uiRender <RemoteControl /> with the stream URL and status.streamToken from your backend. The user sees and controls the desktop, and status.token never reaches the browser:
import { RemoteControl } from '@limrun/ui';
type Session = { endpointWebSocketUrl: string; token: string };
export function MacDesktop({ session }: { session: Session }) {
return <RemoteControl url={session.endpointWebSocketUrl} token={session.token} />;
}The component detects a macOS instance from its stream URL and adapts to a desktop:
- From
@limrun/ui0.22.0, it draws the stream inside a MacBook Pro, with a switch to a 3D model.showFrame={false}shows the stream alone, the way 0.21 does. See macOS in a MacBook frame. - The pointer moves the macOS cursor as it hovers, and every mouse button reaches the virtual machine, including right-clicks.
- Mouse wheel and trackpad scrolling reach macOS as native scrolling.
- Keys reach macOS with their modifiers, so the virtual machine's own keyboard shortcuts work. Keys held down are released when the stream loses focus.
Inspect mode has no accessibility tree to show on a macOS instance. The other props, such as sessionId, autoReconnect, and onTerminated, are described in Embed a device.
List, get, and delete instances
List calls filter by state and labels. Get and delete take the instance ID, and deleting an instance discards everything on its disk:
lim macos list --label-selector user=user_42
lim macos get <instance-id>
lim macos delete <instance-id>for await (const inst of lim.macosInstances.list({ labelSelector: 'user=user_42', state: 'ready' })) {
console.log(inst.metadata.id);
}
const inst = await lim.macosInstances.get(id);
await lim.macosInstances.delete(id);page, err := client.MacosInstances.List(ctx, limrun.MacosInstanceListParams{
LabelSelector: param.NewOpt("user=user_42"),
State: param.NewOpt("ready"),
})
if err != nil {
panic(err)
}
for _, inst := range page.Items {
fmt.Println(inst.Metadata.ID)
}
inst, _ := client.MacosInstances.Get(ctx, id)
_ = client.MacosInstances.Delete(ctx, id)curl "https://api.limrun.com/v1/macos_instances?labelSelector=user%3Duser_42&state=ready" \
-H "Authorization: Bearer $LIM_API_KEY"
curl https://api.limrun.com/v1/macos_instances/<instance-id> -H "Authorization: Bearer $LIM_API_KEY"
curl -X DELETE https://api.limrun.com/v1/macos_instances/<instance-id> -H "Authorization: Bearer $LIM_API_KEY"CLI only: lim macos list shows only ready instances unless you pass --state or --all. The API returns every state when you omit state.
Change labels on a running instance
PATCH /v1/macos_instances/{id} replaces the labels or the delegated permissions of a live instance. Send the ETag from a get call in the If-Match header; a stale ETag returns 412. The TypeScript SDK exposes this as lim.macosInstances.update(id, { ifMatch, metadata }) and the Go SDK as client.MacosInstances.Update. The request body and the update rules are the same as for the other instance types; see Updating labels.
Work inside the instance from your backend
Your backend can also run shell commands in the virtual machine, sync folders into it, push and pull files, launch apps, drive its screen, and record it, with the instance's token. Create a guest client from the ready instance with lim.macosInstances.createClient({ instance }) in TypeScript or client.MacosInstances.CreateClient(instance) in Go; Run a macOS VM shows every call. To keep the virtual machine's disk for the user's next session, give the instance a disk snapshot key.
Next steps
Create macOS virtual machines from your backend with the REST API or an SDK, and show their desktops in your own web app.
Was this guide helpful?