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

> Manage public URLs for sandbox ports with the Ruby SDK.

Cloud-only (Available only on microsandbox cloud; the local backend raises UnsupportedError.)

See [Services](https://docs.microsandbox.dev/sandboxes/services.md) for usage and limits.

## Service

Properties use the last response without making a request.

#### Microsandbox::Service.builder()

```ruby theme={null}
Microsandbox::Service.builder(slug) # => ServiceBuilder
```

Start a builder for a Service.

Parameters

`slug`StringGlobally unique Service slug.

Returns

[ServiceBuilder](#servicebuilder)A builder to set the target on, then create.

#### Microsandbox::Service.create()

```ruby theme={null}
Microsandbox::Service.create(slug, target:) # => Service
```

> [accordion] Example
>
> ```ruby theme={null}
> target = Microsandbox::ServiceTarget.new("my-sandbox", 8080)
> svc = Microsandbox::Service.create("my-app", target: target)
> ```

Create a Service without retrying. A `nil` target or port zero raises `InvalidConfigError` before any request. Repeating a create with the same slug and target returns the existing Service while it is enabled.

Parameters

`slug`StringGlobally unique Service slug.

`target`[ServiceTarget](#servicetarget)Running sandbox and port that receive requests.

#### Microsandbox::Service.get()

```ruby theme={null}
Microsandbox::Service.get(slug) # => Service
```

Get a Service by slug.

Parameters

`slug`StringService slug.

Returns

[Service](#service)The Service's current state.

#### Microsandbox::Service.list()

```ruby theme={null}
Microsandbox::Service.list(sandbox: nil, cursor: nil, limit: nil) # => Hash
```

List one page in slug order, including disabled and detached Services. A limit outside 1 through 100 raises `InvalidConfigError` before any request.

> [accordion] Example
>
> ```ruby theme={null}
> page = Microsandbox::Service.list(sandbox: "my-sandbox", limit: 50)
> page.fetch("services").each { |svc| puts svc.slug }
> next_cursor = page.fetch("next_cursor")
> ```

Keyword parameters

`sandbox`String | nilOnly Services targeting this sandbox.

`cursor`String | nilOpaque cursor returned by the previous page.

`limit`Integer | nilPage size from 1 through 100; defaults to 20.

Returns

`"services"`[Array\<Service>](#service)Services in this page, in slug order.

`"next_cursor"`String | nilCursor for the next page, or `nil` on the final page.

Instance properties

#### svc.slug

`String` — The Service slug.

#### svc.url

`String` — The public URL as returned by the backend.

#### svc.status

`String` — `"enabled"`, `"disabled"`, `"detached"`, or `"unknown"`, as of the read that returned this value. `refresh` returns a current copy.

#### svc.target

[`ServiceTarget`](#servicetarget) ` | nil` — The target, or `nil` when detached.

#### svc.created\_at

`String` — RFC 3339 creation timestamp.

#### svc.updated\_at

`String` — RFC 3339 timestamp of the latest state change.

Instance methods

#### svc.update()

```ruby theme={null}
svc.update(target:) # => Service
```

Change the target without retrying. `target:` is required; a `nil` target or port zero raises `InvalidConfigError` before any request. Setting a target also enables or reattaches the Service.

Keyword parameters

`target`[ServiceTarget](#servicetarget)Running target; also enables or reattaches the Service.

Returns

[Service](#service)The Service as committed.

#### svc.enable()

```ruby theme={null}
svc.enable # => Service
```

Enable the Service without retrying. Set a target instead when it is detached.

#### svc.disable()

```ruby theme={null}
svc.disable # => Service
```

Disable the Service without retrying while retaining its target.

#### svc.remove()

```ruby theme={null}
svc.remove # => nil
```

Remove the Service and release its slug without retrying.

#### svc.refresh()

```ruby theme={null}
svc.refresh # => Service
```

Get the Service again and return its current state. This object is not changed.

## ServiceBuilder

#### builder.target()

```ruby theme={null}
builder.target(sandbox, port) # => ServiceBuilder
```

Set the running sandbox and port that receive requests.

Parameters

`sandbox`StringCurrent name of a running sandbox.

`port`IntegerSandbox port from 1 through 65,535. A negative number or one above 65,535 raises `RangeError` before any request.

#### builder.create()

```ruby theme={null}
builder.create # => Service
```

> [accordion] Example
>
> ```ruby theme={null}
> svc = Microsandbox::Service.builder("my-app")
>   .target("my-sandbox", 8080)
>   .create
> ```

Create the Service without retrying; consumes the builder.

## ServiceTarget

```ruby theme={null}
Microsandbox::ServiceTarget.new(sandbox, port)
```

A `Data` value; targets compare by value.

#### target.sandbox

`String` — Current name of the target sandbox.

#### target.port

`Integer` — Sandbox port from 1 through 65,535.

## ServiceError

`Microsandbox::ServiceError` reports a refused Service change or an unknown outcome. A missing Service raises `ServiceNotFoundError`, an unavailable slug raises `ServiceAlreadyExistsError`, and a missing target sandbox raises `SandboxNotFoundError`.

#### error.slug

`String` — Service slug.

#### error.reason

`String` — One of the `Microsandbox::ServiceErrorReason` constants below.

#### error.retry\_after

`Float | nil` — Retry delay in seconds when the backend returns one.

#### error.changed

`String` — `"unchanged"` or `"unknown"`. Unknown means the change may have been applied; get and check the Service.

Every `Microsandbox::Error` raised by `create`, `update`, `enable`, `disable`, or `remove` exposes this property, not only `ServiceError`.

#### error.raw\_code

`String | nil` — Unrecognized refusal code when `reason` is `ServiceErrorReason::OTHER`.

#### error.raw\_status

`Integer | nil` — Raw HTTP status when `reason` is `ServiceErrorReason::OTHER`.

#### error.raw\_message

`String | nil` — Raw refusal message when `reason` is `ServiceErrorReason::OTHER`.

## Service error reasons

`ServiceErrorReason::SERVICE_QUOTA_EXCEEDED`The organization's Service limit was reached. Remove a Service or request a higher limit.

`ServiceErrorReason::SANDBOX_SERVICE_LIMIT_EXCEEDED`The target sandbox's Service limit was reached. Remove or move a Service, or request a higher limit.

`ServiceErrorReason::SERVICE_CONFLICT`Another change to the Service committed first. Get it, then retry.

`ServiceErrorReason::SERVICE_DETACHED`The Service is detached. Set a target.

`ServiceErrorReason::TARGET_UNREACHABLE`The target sandbox cannot be reached right now. Retry later, after `retry_after` when set.

`ServiceErrorReason::SANDBOX_NOT_RUNNING`The target sandbox is not running. Start it, then retry.

`ServiceErrorReason::SERVICE_CHANGES_PAUSED`Service changes are paused. Retry later, after `retry_after` when set.

`ServiceErrorReason::OUTCOME_UNKNOWN`The change may have been applied. Get and check the Service before retrying.

`ServiceErrorReason::OTHER`A refusal this SDK does not recognize. `raw_code`, `raw_status`, and `raw_message` hold what was returned; `changed` still reports the outcome.


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