# Ports (/docs/go-sdk/ports)

> For AI agents: the complete documentation index is at https://www.daytona.com/llms.txt.

## Ports [#ports]

```go
type Ports struct {
	// contains filtered or unexported fields
}
```

Ports inspects listeners and manages a sandbox's preview endpoints.
Obtain it from Sandbox.Ports. It is safe for concurrent use.

#### Ports.ExpireSignedPreviewLinks [#portsexpiresignedpreviewlinks]

```go
func (p *Ports) ExpireSignedPreviewLinks(ctx context.Context) error
```

ExpireSignedPreviewLinks revokes every signed preview link on every sandbox
port, including while deleting. Links created afterwards remain usable.

#### Ports.Expose [#portsexpose]

```go
func (p *Ports) Expose(ctx context.Context, port int, opts *ExposePortOptions) (*PreviewEndpoint, error)
```

Expose registers port, or updates an existing endpoint's visibility.
Pass &ExposePortOptions\{Public: true\} for public, credential-free access:
the returned PreviewEndpoint.URL opens in a browser with no token. Browsers
may first see a preview warning page (unless the organization turned it
off); scripts and headless browsers skip it by sending the header
PreviewWarningSkipHeader with PreviewWarningSkipHeaderValue.
Nil options select private access. Ports outside 1..65535 and reserved
daemon ports 2280, 2281, 22220 return ErrBadRequest before any request.
Deleted or deleting sandboxes return ErrConflict.

**Example**:

```go
ctx := context.Background()
client, err := daytona.New()
if err != nil {
	log.Fatal(err)
}
defer func() {
	if closeErr := client.Close(); closeErr != nil {
		log.Print(closeErr)
	}
}()
sandbox, err := client.Get(ctx, "sandbox-id", nil)
if err != nil {
	log.Fatal(err)
}
endpoint, err := sandbox.Ports().Expose(ctx, 8080, &daytona.ExposePortOptions{Public: true})
if err != nil {
	log.Fatal(err)
}
// Public endpoints require no preview token.
_ = endpoint.URL
```

#### Ports.IsInUse [#portsisinuse]

```go
func (p *Ports) IsInUse(ctx context.Context, port int) (bool, error)
```

IsInUse reports whether port has a listener. Values outside 1..65535 return
ErrBadRequest locally. The toolbox only supports 3000..9999 and returns
BAD_REQUEST for other ports, rather than reporting false.

#### Ports.List [#portslist]

```go
func (p *Ports) List(ctx context.Context) (*Page[int], error)
```

List returns every listening TCP port in one non-pageable page.
The toolbox reports non-negative int32 values, without a 65535 upper bound.

#### Ports.ListEndpoints [#portslistendpoints]

```go
func (p *Ports) ListEndpoints(ctx context.Context, opts *ListEndpointsOptions) (*Page[PreviewEndpoint], error)
```

ListEndpoints returns one page of preview endpoints.
Deleted or deleting sandboxes return ErrConflict.

#### Ports.ListEndpointsAll [#portslistendpointsall]

```go
func (p *Ports) ListEndpointsAll(ctx context.Context, opts *ListEndpointsOptions) iter.Seq2[PreviewEndpoint, error]
```

ListEndpointsAll traverses pages until exhaustion, cursor repetition, an
error, cancellation, or the consumer stopping. Options are copied immediately.

#### Ports.PreviewLink [#portspreviewlink]

```go
func (p *Ports) PreviewLink(ctx context.Context, port int) (*PreviewLink, error)
```

PreviewLink returns a preview URL and token, privately exposing port first
if needed. Public endpoints return an empty token. Private endpoints prefer
a newly minted token scoped to this port alone; caller-supplied sandbox
credentials and mint failures retain a usable preview route's token.
A mint failure is returned when the route has no URL or private token.
Invalid or reserved preview ports return ErrBadRequest locally.
Deleted or deleting sandboxes return ErrConflict.

**Example**:

```go
ctx := context.Background()
client, err := daytona.New()
if err != nil {
	log.Fatal(err)
}
defer func() {
	if closeErr := client.Close(); closeErr != nil {
		log.Print(closeErr)
	}
}()
sandbox, err := client.Get(ctx, "sandbox-id", nil)
if err != nil {
	log.Fatal(err)
}
link, err := sandbox.Ports().PreviewLink(ctx, 8080)
if err != nil {
	log.Fatal(err)
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, link.URL, nil)
if err != nil {
	log.Fatal(err)
}
request.Header.Set("X-Daytona-Preview-Token", link.Token)
request.Header.Set(daytona.PreviewWarningSkipHeader, daytona.PreviewWarningSkipHeaderValue)
// Send request using an HTTP client with a suitable timeout.
```

#### Ports.SignedPreviewLink [#portssignedpreviewlink]

```go
func (p *Ports) SignedPreviewLink(
	ctx context.Context, port int, opts *SignedPreviewLinkOptions,
) (*SignedPreviewLink, error)
```

SignedPreviewLink returns a self-contained URL needing no headers or other
credentials. Nil options or zero ExpiresIn selects the 60-second server default.
Positive lifetimes must be whole seconds; the deployment caps the maximum,
typically seven days. Invalid ports, reserved daemon ports, and
non-whole-second or negative lifetimes return
ErrBadRequest locally. Unavailable sandboxes may return ErrConflict.

**Example**:

```go
ctx := context.Background()
client, err := daytona.New()
if err != nil {
	log.Fatal(err)
}
defer func() {
	if closeErr := client.Close(); closeErr != nil {
		log.Print(closeErr)
	}
}()
sandbox, err := client.Get(ctx, "sandbox-id", nil)
if err != nil {
	log.Fatal(err)
}
link, err := sandbox.Ports().SignedPreviewLink(ctx, 8080, &daytona.SignedPreviewLinkOptions{ExpiresIn: 5 * time.Minute})
if err != nil {
	log.Fatal(err)
}
// Hand link.URL to a browser or another tool: no headers are required.
_ = link.URL
if revokeErr := sandbox.Ports().ExpireSignedPreviewLinks(ctx); revokeErr != nil {
	log.Fatal(revokeErr)
}
```

#### Ports.Unexpose [#portsunexpose]

```go
func (p *Ports) Unexpose(ctx context.Context, port int) error
```

Unexpose removes a preview endpoint, including while its sandbox is deleting.
Ports outside 1..65535 return ErrBadRequest locally; reserved ports are
accepted here so an existing registration can always be removed.

## ExposePortOptions [#exposeportoptions]

```go
type ExposePortOptions struct {
	// Public permits credential-free access; false (the default) requires a token.
	Public bool
}
```

ExposePortOptions controls preview visibility.

## ListEndpointsOptions [#listendpointsoptions]

```go
type ListEndpointsOptions struct {
	// Cursor continues a preceding page; empty starts at the beginning.
	Cursor string
	// Limit is 1..200; zero uses the server default.
	Limit int
}
```

ListEndpointsOptions selects one page of preview endpoints.

## PreviewEndpoint [#previewendpoint]

```go
type PreviewEndpoint struct {
	// Port is the sandbox's TCP port.
	Port int
	// Public permits access without credentials.
	Public bool
	// URL opens the endpoint without an embedded credential.
	URL string
	// CreatedAt is when the endpoint was registered.
	CreatedAt time.Time
}
```

PreviewEndpoint describes a registered sandbox port.

#### PreviewEndpoint.GoString [#previewendpointgostring]

```go
func (p PreviewEndpoint) GoString() string
```

GoString returns the same connection details as String.

#### PreviewEndpoint.String [#previewendpointstring]

```go
func (p PreviewEndpoint) String() string
```

String returns the port, visibility and credential-free endpoint URL.

## PreviewLink [#previewlink]

```go
type PreviewLink struct {
	// URL is the preview URL.
	URL string
	// Token is sent in X-Daytona-Preview-Token; empty means public access.
	Token string
	// ExpiresAt is zero when the token's expiry is unknown.
	// JWT expiry is advisory metadata only, not verified authorization.
	ExpiresAt time.Time
}
```

PreviewLink grants access to one preview endpoint.

#### PreviewLink.GoString [#previewlinkgostring]

```go
func (p PreviewLink) GoString() string
```

GoString returns a credential-free description.

#### PreviewLink.String [#previewlinkstring]

```go
func (PreviewLink) String() string
```

String returns a credential-free description.

## SignedPreviewLink [#signedpreviewlink]

```go
type SignedPreviewLink struct {
	// URL includes the signed token in its hostname.
	URL string
	// Token is the signed preview token.
	Token string
	// ExpiresAt is the server-reported expiration.
	ExpiresAt time.Time
}
```

SignedPreviewLink is a self-contained URL requiring no authentication header.

#### SignedPreviewLink.GoString [#signedpreviewlinkgostring]

```go
func (p SignedPreviewLink) GoString() string
```

GoString returns a credential-free description.

#### SignedPreviewLink.String [#signedpreviewlinkstring]

```go
func (SignedPreviewLink) String() string
```

String returns a credential-free description.

## SignedPreviewLinkOptions [#signedpreviewlinkoptions]

```go
type SignedPreviewLinkOptions struct {
	// ExpiresIn is a positive whole-second lifetime; zero uses the server
	// default of 60 seconds. The deployment sets the maximum lifetime.
	ExpiresIn time.Duration
}
```

SignedPreviewLinkOptions selects the lifetime of a signed preview URL.

## PreviewWarningSkipHeader, PreviewWarningSkipHeaderValue [#previewwarningskipheader-previewwarningskipheadervalue]

```go
const (
	// PreviewWarningSkipHeader skips the preview warning page for non-interactive clients.
	PreviewWarningSkipHeader = "X-Daytona-Skip-Preview-Warning"
	// PreviewWarningSkipHeaderValue is the value to send with PreviewWarningSkipHeader.
	PreviewWarningSkipHeaderValue = "true"
)
```
