> ## Documentation Index
> Fetch the complete documentation index at: https://docs.microsandbox.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Services

> Give a sandbox port a public URL

Cloud-only (Available only on microsandbox cloud; on the local backend, every Service call and msb service command fails with an unsupported error (`ErrCloudOnly` in Go).)

A Service gives a sandbox port a public HTTPS URL. Requests to the URL go to the Service's target: one sandbox and port. The URL is `https://<slug>.<domain>` and stays the same when you change the target.

## Create a Service

Select a remote destination with `MSB_BACKEND=remote` or a remote profile. With the local destination selected, Service lookup, list, and creation return an unsupported error before sending a provider request, even when `MSB_API_KEY` is set.

Start the target sandbox, then create the Service:

```typescript TypeScript theme={null}
import { Service } from "microsandbox";

const svc = await Service.builder("my-app").target("my-sandbox", 8080).create();
console.log(svc.url);
```

```rust Rust theme={null}
use microsandbox::Service;

let svc = Service::builder("my-app")
    .target("my-sandbox", 8080)
    .create()
    .await?;
println!("{}", svc.url());
```

```python Python theme={null}
from microsandbox import Service, ServiceTarget

svc = await Service.create("my-app", target=ServiceTarget("my-sandbox", 8080))
print(svc.url)
```

```go Go theme={null}
svc, err := m.CreateService(ctx, "my-app", m.ServiceTarget{
    Sandbox: "my-sandbox",
    Port:    8080,
})
if err != nil {
    return err
}
fmt.Println(svc.URL())
```

```ruby Ruby theme={null}
target = Microsandbox::ServiceTarget.new("my-sandbox", 8080)
svc = Microsandbox::Service.create("my-app", target: target)
puts svc.url
```

```bash CLI theme={null}
msb service create my-app --target my-sandbox:8080
```

Slugs contain 3–63 lowercase letters, digits, and single hyphens. They cannot start or end with a hyphen and are not normalized. If a slug is unavailable, choose another. Repeating a create with the same slug and target returns the existing Service while it is enabled.

Use the returned URL as-is. Service URLs are public, so add authentication in your application when needed.

The URL usually works when create, enable, or a target change returns; sometimes it takes a few more seconds. A URL requested shortly before you created the Service or set its target can return [`404 not_found`](#error-responses) for a short time.

## Move, disable, or enable a Service

A Service is `enabled`, `disabled`, or `detached` (it has no target). Disabling keeps the target. Enabling the Service, or setting a target, turns it back on; the target sandbox must be running.

```typescript TypeScript theme={null}
let svc = await Service.get("my-app");
svc = await svc.update({ target: { sandbox: "new-sandbox", port: 8080 } });
svc = await svc.disable();
svc = await svc.enable();
```

```rust Rust theme={null}
let svc = Service::get("my-app").await?;
let svc = svc.update(|u| u.target("new-sandbox", 8080)).await?;
let svc = svc.disable().await?;
let svc = svc.enable().await?;
```

```python Python theme={null}
svc = await Service.get("my-app")
svc = await svc.update(target=ServiceTarget("new-sandbox", 8080))
svc = await svc.disable()
svc = await svc.enable()
```

```go Go theme={null}
svc, err := m.GetService(ctx, "my-app")
if err != nil {
    return err
}
if svc, err = svc.Update(ctx, m.ServiceTarget{Sandbox: "new-sandbox", Port: 8080}); err != nil {
    return err
}
if svc, err = svc.Disable(ctx); err != nil {
    return err
}
if svc, err = svc.Enable(ctx); err != nil {
    return err
}
```

```ruby Ruby theme={null}
svc = Microsandbox::Service.get("my-app")
svc = svc.update(target: Microsandbox::ServiceTarget.new("new-sandbox", 8080))
svc = svc.disable
svc = svc.enable
```

```bash CLI theme={null}
msb service update my-app --target new-sandbox:8080
msb service disable my-app
msb service enable my-app
```

Each call returns the updated Service. Enabling and disabling both need a target: on a detached Service, both fail with the [`service_detached` reason](https://docs.microsandbox.dev/sdk/errors#service-changes), so set a target instead.

If the target sandbox stops, the Service keeps its target and requests return `503 sandbox_not_running` until the sandbox runs again. If the target sandbox is removed, the Service becomes detached and keeps its slug. After a few seconds, requests return `503 no_target` or `404 not_found` until you set a new target.

## Handle requests

In the sandbox, your application must listen on the target port at `0.0.0.0` or `127.0.0.1`. Services support HTTP, WebSocket, and server-sent events (SSE). Set your own timeouts for slow responses, and keep long-lived connections active. Your application receives the original method, path, query, and body, plus these headers:

* `Host`: the Service hostname
* `X-Forwarded-Host`: the Service hostname
* `X-Forwarded-Proto`: `https`
* `X-Forwarded-For`: the client IP address

Microsandbox sets these headers itself and replaces any copies the client sent, so your application can trust them. Header names starting with `msb-` or `x-msb-`, and cookies named `msb_*` or `__Host-msb_*`, are reserved for Microsandbox: they never reach your application, and your responses cannot set those cookies.

On the Free plan, a browser's first visit shows a notice page before your application. Automated browsers, such as end-to-end tests, can skip it by sending an `msb-skip-browser-warning` header with any value.

## Error responses

When Microsandbox answers instead of your application, the response has an `x-msb-error` header. Microsandbox removes this header from your application's responses, so it always means Microsandbox answered. The body is JSON with the same code in `error.code`, or an HTML page when the client asks for `text/html`.

| Status | `x-msb-error` | Meaning | What to do |
| - | - | - | - |
| `404` | `not_found` | The Service does not exist, or is disabled or detached. | Check the URL. Enable the Service or set a target. |
| `429` | `service_busy_inflight` | The target sandbox has too many requests in progress. | Retry with backoff, or spread traffic across more sandboxes. |
| `431` | `request_header_too_large` | The request headers are too large. | Send fewer or smaller headers. |
| `502` | `port_closed` | Nothing is listening on the target port. | Start your application on that port. |
| `502` | `guest_closed` | Your application closed the connection or sent an invalid HTTP response. | Check your application's logs. |
| `503` | `no_target` | The target sandbox was removed. | Set a new target. |
| `503` | `sandbox_not_running` | The target sandbox is not running. | Start the sandbox. |
| `503` | `service_unavailable` | Microsandbox cannot serve the request right now. | Retry. If `Retry-After` is set, wait that many seconds first. |
| `504` | `upstream_timeout` | The target port did not accept the connection within 10 seconds. | Retry. If it persists, check that your application is running and not overloaded. |

Only `service_unavailable` means Microsandbox itself cannot serve the request. The other codes describe the Service, its target sandbox, your application, or the request.

## Get a Service

Get a Service by its slug to read its URL, its status (`enabled`, `disabled` or `detached`), its target, and when it was created and last changed. A Service object holds the state from the response that returned it; `refresh` returns a current copy.

```typescript TypeScript theme={null}
let svc = await Service.get("my-app");
console.log(svc.url, svc.status, svc.updatedAt);
svc = await svc.refresh();
```

```rust Rust theme={null}
let svc = Service::get("my-app").await?;
println!("{} {:?} {}", svc.url(), svc.status(), svc.updated_at());
let svc = svc.refresh().await?;
```

```python Python theme={null}
svc = await Service.get("my-app")
print(svc.url, svc.status, svc.updated_at)
svc = await svc.refresh()
```

```go Go theme={null}
svc, err := m.GetService(ctx, "my-app")
if err != nil {
    return err
}
fmt.Println(svc.URL(), svc.Status(), svc.UpdatedAt())
if svc, err = svc.Refresh(ctx); err != nil {
    return err
}
```

```ruby Ruby theme={null}
svc = Microsandbox::Service.get("my-app")
puts [svc.url, svc.status, svc.updated_at]
svc = svc.refresh
```

```bash CLI theme={null}
msb service inspect my-app
msb service inspect my-app --format json
```

For scripts, add `--format json` to any `msb service` command except `remove`. A Service prints as an object with `slug`, `url`, `status`, `target`, `created_at`, and `updated_at`.

## List Services

Lists are in slug order and include disabled and detached Services. Filter by sandbox to find its Services.

```typescript TypeScript theme={null}
const page = await Service.listWith((l) => l.sandbox("new-sandbox"));
```

```rust Rust theme={null}
let page = Service::list_with(|l| l.sandbox("new-sandbox")).await?;
```

```python Python theme={null}
page = await Service.list_with(sandbox="new-sandbox")
```

```go Go theme={null}
page, err := m.ListServicesWith(ctx, m.WithServiceListSandbox("new-sandbox"))
```

```ruby Ruby theme={null}
page = Microsandbox::Service.list(sandbox: "new-sandbox")
```

```bash CLI theme={null}
msb service ls --sandbox new-sandbox
```

The SDKs return one page at a time: 20 Services by default, up to 100 with a limit. Pass a page's next cursor to get the following page; the last page has none. `msb service ls` lists every Service.

```typescript TypeScript theme={null}
let cursor: string | undefined;
do {
  const page = await Service.listWith((l) => (cursor ? l.cursor(cursor) : l));
  for (const svc of page.services) console.log(svc.slug, svc.url);
  cursor = page.nextCursor;
} while (cursor);
```

```rust Rust theme={null}
let mut cursor: Option<String> = None;
loop {
    let page = Service::list_with(|l| match cursor.take() {
        Some(next) => l.cursor(next),
        None => l,
    })
    .await?;
    for svc in &page.services {
        println!("{} {}", svc.slug(), svc.url());
    }
    cursor = page.next_cursor;
    if cursor.is_none() {
        break;
    }
}
```

```python Python theme={null}
cursor = None
while True:
    page = await Service.list_with(cursor=cursor)
    for svc in page.services:
        print(svc.slug, svc.url)
    cursor = page.next_cursor
    if cursor is None:
        break
```

```go Go theme={null}
var opts []m.ServiceListOption
for {
    page, err := m.ListServicesWith(ctx, opts...)
    if err != nil {
        return err
    }
    for _, svc := range page.Services {
        fmt.Println(svc.Slug(), svc.URL())
    }
    if page.NextCursor == nil {
        break
    }
    opts = []m.ServiceListOption{m.WithServiceListCursor(*page.NextCursor)}
}
```

```ruby Ruby theme={null}
cursor = nil
loop do
  page = Microsandbox::Service.list(cursor: cursor)
  page.fetch("services").each { |svc| puts [svc.slug, svc.url] }
  cursor = page.fetch("next_cursor")
  break unless cursor
end
```

```bash CLI theme={null}
msb service ls --format json
```

## Remove a Service

Removing a Service releases its slug.

```typescript TypeScript theme={null}
await svc.remove();
```

```rust Rust theme={null}
svc.remove().await?;
```

```python Python theme={null}
await svc.remove()
```

```go Go theme={null}
err := svc.Remove(ctx)
```

```ruby Ruby theme={null}
svc.remove
```

```bash CLI theme={null}
msb service remove my-app
```

## When a change fails

Create, update, enable, disable, and remove each send one request and never retry. Most errors mean nothing changed. If an error says the change may have been applied, get the Service and check it before trying again. An update, enable, or disable that is already in effect succeeds without a change. See [SDK errors](https://docs.microsandbox.dev/sdk/errors.md#service-changes).

## Audit events

Each committed Service change records one event in the organization's audit log. A change that is already in effect records nothing.

Read Service events with the audit-log API. Authenticate with an API key.

```http theme={null}
GET /v1/events?event_type=service_created
```

You can also filter with `since`, `until`, `cursor`, and `limit`. Each event has `id`, `org_id`, `category` (`audit`), `event_type`, `actor`, `payload`, and `timestamp`.

| Event type | Payload keys |
| - | - |
| `service_created` | `slug`, `sandbox_id`, `port` |
| `service_rotated` | `slug`, `from_sandbox_id`, `to_sandbox_id`, `port` |
| `service_disabled` | `slug`, `sandbox_id` |
| `service_enabled` | `slug`, `sandbox_id` |
| `service_deleted` | `slug`, `sandbox_id` |

Every payload also has `org_id`. A `service_rotated` event means the Service was pointed at a new target. Its `from_sandbox_id` is `null` when the Service had no target.

The `sandbox_id` on `service_disabled` and `service_enabled` is `null` when the target sandbox was deleted during the change. On `service_deleted`, it is `null` when the Service was detached.

Events contain sandbox IDs, not sandbox names. Deleting a target sandbox automatically detaches its Service without recording a `service_*` event.

## Limits

> [accordion] Service limits
>
> | Limit | Free plan | Paid plans |
> | - | - | - |
> | Services per organization | 100 | 1,000 |
> | Services per sandbox | 20 | 20 |
>
> You can request a higher limit for your organization. If a limit drops below your current usage, existing Services keep working; creating a Service, or moving one to a sandbox at its limit, is refused until you are under it.
>
> Going over these limits fails with `service_quota_exceeded` or `sandbox_service_limit_exceeded` (see [Service changes](https://docs.microsandbox.dev/sdk/errors.md#service-changes)). Disabled Services count toward both limits, and detached Services count toward the organization limit. Too many concurrent requests to one sandbox return `429 service_busy_inflight`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.