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

Cloud-only (Available only on microsandbox cloud; the local backend returns ErrCloudOnly.)

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

## Functions

#### CreateService()

```go theme={null}
func CreateService(ctx context.Context, slug string, target ServiceTarget) (*Service, error)
```

> [accordion] Example
>
> ```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())
> ```

Create a Service for a running target without retrying. Port zero fails with `ErrInvalidConfig` before any request. Repeating a create with the same slug and target returns the existing Service while it is enabled. Get and check the Service if `ctx` ends after the request is sent.

Parameters

`slug`stringService slug, e.g. `"my-app"`.

`target`[ServiceTarget](#servicetarget)The running sandbox and port that receive requests. The port is 1 through 65535.

Returns

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

#### GetService()

```go theme={null}
func GetService(ctx context.Context, slug string) (*Service, error)
```

Get a Service by slug.

Parameters

`slug`stringService slug.

Returns

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

#### ListServices()

```go theme={null}
func ListServices(ctx context.Context) (*ServicePage, error)
```

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

Returns

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

#### ListServicesWith()

```go theme={null}
func ListServicesWith(ctx context.Context, configure ...ServiceListOption) (*ServicePage, error)
```

> [accordion] Example
>
> ```go theme={null}
> page, err := m.ListServicesWith(ctx, m.WithServiceListSandbox("my-sandbox"), m.WithServiceListLimit(50))
> if err != nil {
>     return err
> }
> for _, svc := range page.Services {
>     fmt.Println(svc.URL(), svc.Target())
> }
> ```

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

Parameters

`configure`[...ServiceListOption](#servicelistoption)List options.

Returns

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

## Service

Accessors use the last response without making a request.

#### svc.Slug()

```go theme={null}
func (s *Service) Slug() string
```

The Service slug.

#### svc.URL()

```go theme={null}
func (s *Service) URL() string
```

The public URL as returned by the backend.

#### svc.Status()

```go theme={null}
func (s *Service) Status() ServiceStatus
```

Whether the Service routes requests, is disabled, or has no target, as of the read that returned it. `Refresh()` returns a current copy.

#### svc.Target()

```go theme={null}
func (s *Service) Target() *ServiceTarget
```

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

#### svc.CreatedAt()

```go theme={null}
func (s *Service) CreatedAt() time.Time
```

The creation time.

#### svc.UpdatedAt()

```go theme={null}
func (s *Service) UpdatedAt() time.Time
```

The last state-change time.

#### svc.Update()

```go theme={null}
func (s *Service) Update(ctx context.Context, target ServiceTarget) (*Service, error)
```

> [accordion] Example
>
> ```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
> }
> ```

Change the target without retrying. Port zero fails with `ErrInvalidConfig` before any request. Get and check the Service if `ctx` ends after the request is sent.

Parameters

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

Returns

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

#### svc.Enable()

```go theme={null}
func (s *Service) Enable(ctx context.Context) (*Service, error)
```

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

#### svc.Disable()

```go theme={null}
func (s *Service) Disable(ctx context.Context) (*Service, error)
```

Disable the Service without retrying while retaining its target.

#### svc.Remove()

```go theme={null}
func (s *Service) Remove(ctx context.Context) error
```

Remove the Service and release its slug without retrying.

#### svc.Refresh()

```go theme={null}
func (s *Service) Refresh(ctx context.Context) (*Service, error)
```

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

Returns

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

#### svc.Close()

```go theme={null}
func (s *Service) Close() error
```

Release the native handle that this value and its copies share. It does not remove the Service. Later requests from any copy fail with `ErrInvalidHandle`; accessors still answer. Calling `Close` is optional: the handle is released once no copy is reachable. It is safe to call more than once.

## ServiceListOption

#### WithServiceListSandbox()

```go theme={null}
func WithServiceListSandbox(name string) ServiceListOption
```

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

#### WithServiceListLimit()

```go theme={null}
func WithServiceListLimit(limit uint32) ServiceListOption
```

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

#### WithServiceListCursor()

```go theme={null}
func WithServiceListCursor(cursor string) ServiceListOption
```

Continue from a previous `NextCursor`.

## ServicePage

`Services`[\[\]\*Service](#service)Services in this page, in slug order.

`NextCursor`\*stringCursor for the next page, or `nil` on the final page.

## ServiceStatus

`ServiceStatusEnabled`"enabled"Routes requests to its target.

`ServiceStatusDisabled`"disabled"Retains its target without routing requests.

`ServiceStatusDetached`"detached"Has no target.

`ServiceStatusUnknown`"unknown"The backend returned an unrecognized status.

## ServiceTarget

`Sandbox`stringCurrent name of the target sandbox.

`Port`uint16Sandbox port, 1 through 65535.

## ServiceError

A refused Service change or an unknown outcome. Use `errors.As` to inspect it; `IsKind(err, ErrService)` also matches it. Check for a missing Service with `IsKind(err, ErrServiceNotFound)`, an unavailable slug with `IsKind(err, ErrServiceAlreadyExists)`, and a missing target sandbox with `IsKind(err, ErrSandboxNotFound)`.

`Slug`stringService slug.

`Reason`ServiceErrorReasonReason for the failure.

`RetryAfter`time.DurationRetry delay returned with the refusal, or zero when none was returned.

`Changed`[ChangeState](#changestate)`ChangeUnknown` means the change may have been applied; get and check the Service.

`RawCode`stringRaw error code returned with a `ServiceReasonOther` refusal; empty otherwise.

`RawStatus`intHTTP status returned with a `ServiceReasonOther` refusal; zero otherwise.

`RawMessage`stringRaw message returned with a `ServiceReasonOther` refusal; empty otherwise.

Reasons

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

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

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

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

`ServiceReasonTargetUnreachable`The target sandbox cannot be reached right now. Retry later, after `RetryAfter` when set.

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

`ServiceReasonChangesPaused`Service changes are paused. Retry later, after `RetryAfter` when set.

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

`ServiceReasonOther`A reason this SDK does not recognize; `RawCode`, `RawStatus`, and `RawMessage` preserve the refusal.

## ChangeState

Every error from `CreateService`, `Update`, `Enable`, `Disable`, or `Remove` carries `Changed` on the base `*Error`. Use `var e *m.Error` and `errors.As(err, &e)` to inspect it.

`ChangeUnchanged`"unchanged"Nothing changed.

`ChangeUnknown`"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.