---
title: "Create and embed from code"
description: "Create macOS virtual machines from your backend with the REST API or an SDK, and show their desktops in your own web app."
canonical_url: "https://docs.limrun.com/docs/macos/create-and-embed"
markdown_url: "https://docs.limrun.com/docs/macos/create-and-embed.md"
last_updated: "2026-12-02"
x_farming_labs_generated_preamble: true
---

# Create and embed from code
URL: /docs/macos/create-and-embed
LLM index: /llms.txt
Description: Create macOS virtual machines from your backend with the REST API or an SDK, and show their desktops in your own web app.

# 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](/docs/macos/run-vm).

## Before you start

- **An API key, kept on your backend.** Create one at [console.limrun.com](https://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.macosInstances` from `@limrun/api` 0.77.1 or later, and the Go SDK as `client.MacosInstances` from v0.13.1. The `lim macos` commands need `lim` 0.62.0 or later. The Python SDK has no macOS resource; call the REST endpoints under `/v1/macos_instances` from Python. [SDKs and REST API](/docs/reference/sdk#install) lists the install commands.
- **`@limrun/ui` 0.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:

<CodeGroup labels={["CLI","TypeScript","Go","cURL"]}>
```bash
lim macos create --label user=user_42 --reuse-if-exists --inactivity-timeout 10m --json
```

```ts
import 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);
```

```go
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)
```

```bash
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" }
  }'
```
</CodeGroup>

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](/docs/concepts#labels-and-reuse). Without `inactivityTimeout` and `hardTimeout`, the instance gets your organization's macOS defaults; see [Set the timeouts](/docs/macos/run-vm#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](/docs/concepts#credentials).

## Show the desktop in your web app

Install the UI package in your frontend:

```bash
npm install @limrun/ui
```

Render `<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:

```tsx title="frontend/MacDesktop.tsx"
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/ui` 0.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](/docs/platform/embed-simulator#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](/docs/platform/embed-simulator#component-props).

## 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:

<CodeGroup labels={["CLI","TypeScript","Go","cURL"]}>
```bash
lim macos list --label-selector user=user_42
lim macos get <instance-id>
lim macos delete <instance-id>
```

```ts
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);
```

```go
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)
```

```bash
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"
```
</CodeGroup>

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](/docs/reference/instances#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](/docs/macos/run-vm#connect-a-client) shows every call. To keep the virtual machine's disk for the user's next session, give the instance a [disk snapshot](/docs/macos/snapshots) key.

## Next steps

<Columns cols={2}>
  <Card title="Embed a device" icon="monitor-play" href="/docs/platform/embed-simulator">
    Every `<RemoteControl />` prop, reconnects, and a full backend and frontend example.
  </Card>
  <Card title="SDKs and REST API" icon="book-open" href="/docs/reference/sdk">
    Authentication, errors, pagination, and the other instance types.
  </Card>
</Columns>

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
Docs-scoped sitemap: [/docs/sitemap.md](/docs/sitemap.md).
Well-known sitemap: [/.well-known/sitemap.md](/.well-known/sitemap.md).
