> ## 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 Rust SDK.

Cloud-only (Available only on microsandbox cloud; the local backend returns MicrosandboxError::Unsupported.)

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

## Service

Accessors use the last response without making a request.

#### Service::builder()

```rust theme={null}
fn builder(slug: impl Into<String>) -> ServiceBuilder
```

> [accordion] Example
>
> ```rust theme={null}
> use microsandbox::Service;
>
> let svc = Service::builder("my-app").target("my-sandbox", 8080).create().await?;
> println!("{}", svc.url());
> ```

Start a [`ServiceBuilder`](#servicebuilder).

Parameters

`slug`impl Into\<String>Service slug, e.g. `"my-app"`.

Returns

[ServiceBuilder](#servicebuilder)Builder for the new Service.

#### Service::get()

```rust theme={null}
async fn get(slug: &str) -> MicrosandboxResult<Service>
```

Get a Service by slug.

Parameters

`slug`\&strService slug.

Returns

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

#### Service::list()

```rust theme={null}
async fn list() -> MicrosandboxResult<ServicePage>
```

List the first page in slug order, including disabled and detached Services.

Returns

[ServicePage](#servicepage)One page of Services.

#### Service::list\_with()

```rust theme={null}
async fn list_with(
    configure: impl FnOnce(ServiceListBuilder) -> ServiceListBuilder,
) -> MicrosandboxResult<ServicePage>
```

> [accordion] Example
>
> ```rust theme={null}
> use microsandbox::Service;
>
> let page = Service::list_with(|l| l.sandbox("my-sandbox").limit(50)).await?;
> for svc in &page.services {
>     println!("{} -> {:?}", svc.url(), svc.target());
> }
> ```

List a configured page in slug order. A limit outside 1 through 100 fails with `MicrosandboxError::InvalidConfig` before any request.

Parameters

`configure`impl FnOnce(ServiceListBuilder) -> ServiceListBuilderSets the [list options](#servicelistbuilder).

Returns

[ServicePage](#servicepage)One page of Services.

#### svc.slug()

```rust theme={null}
fn slug(&self) -> &str
```

The Service slug.

#### svc.url()

```rust theme={null}
fn url(&self) -> &str
```

The public URL as returned by the backend.

#### svc.status()

```rust theme={null}
fn status(&self) -> ServiceStatus
```

Whether the Service routes requests, is disabled, or has no target, as of the read that returned this value. `refresh()` returns a current copy. An unrecognized backend value is `ServiceStatus::Unknown`.

#### svc.target()

```rust theme={null}
fn target(&self) -> Option<ServiceTarget>
```

The target, or `None` when the Service is detached.

Returns

[Option\<ServiceTarget>](#servicetarget)The current target, or `None` when detached.

#### svc.created\_at()

```rust theme={null}
fn created_at(&self) -> DateTime<Utc>
```

The creation time.

#### svc.updated\_at()

```rust theme={null}
fn updated_at(&self) -> DateTime<Utc>
```

The last state-change time.

#### svc.update()

```rust theme={null}
async fn update(
    &self,
    configure: impl FnOnce(ServiceUpdate) -> ServiceUpdate,
) -> MicrosandboxResult<Service>
```

> [accordion] Example
>
> ```rust theme={null}
> let svc = svc.update(|u| u.target("new-sandbox", 8080)).await?;
> ```

Change the target without retrying. After `OutcomeUnknown`, get and check the Service.

Parameters

`configure`impl FnOnce(ServiceUpdate) -> ServiceUpdateSets the [fields to change](#serviceupdate).

Returns

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

#### svc.enable()

```rust theme={null}
async fn enable(&self) -> MicrosandboxResult<Service>
```

Enable the Service without retrying. To enable a detached Service, set a running target with `update()` instead.

#### svc.disable()

```rust theme={null}
async fn disable(&self) -> MicrosandboxResult<Service>
```

Disable the Service without retrying while retaining its target.

#### svc.remove()

```rust theme={null}
async fn remove(self) -> MicrosandboxResult<()>
```

Remove the Service and release its slug without retrying. This consumes the Service.

#### svc.refresh()

```rust theme={null}
async fn refresh(&self) -> MicrosandboxResult<Service>
```

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

Returns

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

## ServiceBuilder

#### builder.target()

```rust theme={null}
fn target(self, sandbox: impl Into<String>, port: u16) -> Self
```

Set the running sandbox and port that receive requests.

Parameters

`sandbox`impl Into\<String>Target sandbox name.

`port`u16Sandbox port, 1 through 65535.

#### builder.create()

```rust theme={null}
async fn create(self) -> MicrosandboxResult<Service>
```

Create the Service without retrying. A missing target or port zero fails with `MicrosandboxError::InvalidConfig` before any request. Repeating a create with the same slug and target returns the existing Service while it is enabled.

Returns

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

## ServiceUpdate

Fields for one target update. No fields or port zero fails with `MicrosandboxError::InvalidConfig` before any request.

#### u.target()

```rust theme={null}
fn target(self, sandbox: impl Into<String>, port: u16) -> Self
```

Set the running target. This also enables or reattaches the Service.

Parameters

`sandbox`impl Into\<String>Target sandbox name.

`port`u16Sandbox port, 1 through 65535.

## ServiceListBuilder

#### l.sandbox()

```rust theme={null}
fn sandbox(self, name: impl Into<String>) -> Self
```

Filter by target sandbox. An unknown name returns an empty page.

Parameters

`name`impl Into\<String>Sandbox name.

#### l.limit()

```rust theme={null}
fn limit(self, limit: u32) -> Self
```

Set the page size from 1 to 100; the default is 20.

Parameters

`limit`u32Page size.

#### l.cursor()

```rust theme={null}
fn cursor(self, cursor: impl Into<String>) -> Self
```

Continue from a previous `next_cursor`.

Parameters

`cursor`impl Into\<String>Opaque cursor.

## ServicePage

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

`next_cursor`Option\<String>Cursor for the next page, or `None` on the final page.

## ServiceStatus

This enum may gain variants, so a `match` needs a wildcard arm.

`Enabled`Routes requests to its target.

`Disabled`Retains its target without routing requests.

`Detached`Has no target.

`Unknown`The backend returned an unrecognized status.

## ServiceTarget

`sandbox`StringCurrent name of the target sandbox.

`port`u16Sandbox port that receives requests.

## ServiceError

A refused Service change or an unknown outcome, returned as `MicrosandboxError::Service { slug, error }`. `changed()` on either error returns a [`ChangeState`](#changestate). A missing Service returns `MicrosandboxError::ServiceNotFound`, an unavailable slug returns `MicrosandboxError::ServiceAlreadyExists`, and a missing target sandbox returns `MicrosandboxError::SandboxNotFound`.

`QuotaExceeded`The organization's Service limit was reached. Remove a Service or request a higher limit.

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

`Conflict`Another change to the Service committed first. Get it, then retry.

`Detached`The Service is detached. Set a target.

`TargetUnreachable`retry\_after: Option\<Duration>The target sandbox cannot be reached right now. Retry later, after `retry_after` when set.

`TargetNotRunning`The target sandbox is not running. Start it, then retry.

`ChangesPaused`retry\_after: Option\<Duration>Service changes are paused. Retry later, after `retry_after` when set.

`OutcomeUnknown`The change may have been applied. Get and check the Service before retrying.

`Other`code: String, status: u16, message: StringThe change was refused with a reason this SDK does not recognize. The raw code, HTTP status, and message remain available.

## ChangeState

Returned by `changed()` on `MicrosandboxError` and on `ServiceError`. Call `changed()` on every error from Service create, update, enable, disable, or remove, not only `MicrosandboxError::Service`. Invalid input, refusals, missing Services or sandboxes, unavailable slugs, unsupported backends, and connections that failed before the request was sent report `Unchanged`; other errors report `Unknown`.

`Unchanged`Nothing changed.

`Unknown`The change may have been applied. Get and check the Service before retrying.


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