Ports
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
func (p *Ports) ExpireSignedPreviewLinks(ctx context.Context) errorExpireSignedPreviewLinks revokes every signed preview link on every sandbox port, including while deleting. Links created afterwards remain usable.
Ports.Expose
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:
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.URLPorts.IsInUse
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
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
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
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
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:
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
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:
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
func (p *Ports) Unexpose(ctx context.Context, port int) errorUnexpose 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
type ExposePortOptions struct {
// Public permits credential-free access; false (the default) requires a token.
Public bool
}ExposePortOptions controls preview visibility.
ListEndpointsOptions
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
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
func (p PreviewEndpoint) GoString() stringGoString returns the same connection details as String.
PreviewEndpoint.String
func (p PreviewEndpoint) String() stringString returns the port, visibility and credential-free endpoint URL.
PreviewLink
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
func (p PreviewLink) GoString() stringGoString returns a credential-free description.
PreviewLink.String
func (PreviewLink) String() stringString returns a credential-free description.
SignedPreviewLink
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
func (p SignedPreviewLink) GoString() stringGoString returns a credential-free description.
SignedPreviewLink.String
func (SignedPreviewLink) String() stringString returns a credential-free description.
SignedPreviewLinkOptions
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
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"
)