# Expose a port

> Give a web app in your rental its own Service URL on ambi.run, open to everyone or only to callers with a service key, and see what it costs and how to keep it working.

Source: https://legion.ambient.xyz/docs/renting/services/expose-a-port

Exposing a port makes a [service](https://legion.ambient.xyz/docs/renting/services/overview) **on a rental**: it points at one port of one rental, your machine. It gets a Service URL, `https://<name>-<suffix>.ambi.run`, and an access setting: **Public**, or **API key required**. After this page you have a web app of yours reachable over HTTPS.

Exposing is on by default for every organization. An organization owner can switch it off, and Legion can pause it for an organization or take down one URL that breaks the [Acceptable Use Policy](https://legion.ambient.xyz/acceptable-use).

## Before you expose a port

- Your app must listen on `0.0.0.0`, not `127.0.0.1`, on that port inside the container.
- It must speak plain HTTP. Legion handles HTTPS.
- HTTP, WebSockets and server-sent events work.
- Ports 22 and 22022 are for SSH and can't be exposed. To reach an app privately, use `ssh -L` instead. See [Connecting](https://legion.ambient.xyz/docs/renting/connecting).
- The rental must be starting or running.

## Expose it

1. Open the rental, then its **Services** tab, and press **Expose a port**. This opens the same **New service** flow as the Services page, with **On a rental** and the rental chosen.
2. Enter the **Port** your app listens on.
3. Choose **Access**. See [Access](https://legion.ambient.xyz/docs/renting/services/keys-and-security#access) for what each choice means:

   - **API key required** (suggested for an HTTP app): callers must send a service key.
   - **Public:** anyone who has the URL can reach it. You confirm this choice.
4. Optionally limit who can connect to **Only these addresses**: list public IPv4 or IPv6 ranges, one per line. **Add my current address** adds yours. It works with either access choice.
5. Choose a name if you want something other than the default. The Service URL is `https://<name>-<suffix>.ambi.run`, for example `https://web-box-7k2f.ambi.run`: the name must be unique within your organization, and Legion adds a short random ending so two organizations can use the same name and URLs are not guessable. The ending never changes, and the full URL is shown on the service and in the create response.
6. Optionally add a **Description**, such as Jupyter, and press **Create service**.

If you choose **API key required**, the first key's secret is shown once, in a dialog that stays until you confirm you saved it. The service shows **Starting** while the URL is set up, then **Serving**. If it needs attention, the reason is under it. Press the copy button to copy the Service URL.

The rental's **Services** tab lists every service on that rental. The **Network** tab no longer lists them.

From a terminal:

```sh
legion services create --rental quiet-river-7k2f --port 8080 --access keys
```

The command asks before it creates anything and prints the key once, or pass `--yes` in a script. `--access` is `public` (the default for a port, so say `keys` to require a key) or `keys`, and `--source CIDR` (repeatable) limits addresses with either. Switch it later with `legion services update NAME --access public`. `legion svc` is the short form of `legion services`.

With the API, [createService](https://legion.ambient.xyz/docs/api/services/create-service) with a target in mode `rental` does the same:

```sh
curl -X POST https://api.legion.ambient.xyz/v1/services \
  -H "Authorization: Bearer $LEGION_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{"target": {"mode": "rental", "rental_id": "6f1c2b8e-3d4a-4f5e-9b7c-2a1d0e8f9c3b", "port": 8080}, "access": {"mode": "keys"}, "initial_key": {"name": "default"}}'
```

See [Create a service](https://legion.ambient.xyz/docs/renting/services/create#create-it-with-the-api) for every field. Changes to a service need a Full access API key.

## Service URLs

Every new service gets `https://<name>-<suffix>.ambi.run`, where the suffix is a short random ending Legion picks once and never changes. Services that already existed keep the URL they had. Rentals that exposed a port before services existed keep their addresses, and each one is now a service on that rental in your Services list. Visitors reach it over HTTPS on port 443. The host's IP address is never shown.

Switching the access of a service never changes its URL.

## What it costs

A service on a rental costs nothing of its own: you pay for the rental. Data your app sends to visitors is measured and charged above a free allowance. The first 100 GiB a month for your organization is free; after that it is $0.12 per GiB.

- **What is measured:** the bytes your rental sends toward the visitor through `ambi.run`. SSH through the gateway and the browser terminal count towards the same allowance. Data visitors send in is free.
- **Updated** about every minute. The month is a UTC calendar month.
- **Where to see it:** the rental's **Cost** tab shows **Data sent out** by door, with this rental's charge. **Spend**, **Rentals** shows **Data transfer** for the whole organization.

See [Data sent out of Legion](https://legion.ambient.xyz/docs/renting/credit-and-billing#data-sent-out-of-legion) for the details.

## Pre-made models: the first start takes a while

A rental created with a pre-made model gets a service with its Service URL and a first service key right away, but the model is not usable yet. The host first pulls the image, then downloads the model's weights, then loads them onto the GPUs. For a large image or model this can take tens of minutes the first time.

- The rental's page shows where it is: pulling the image, starting the container, downloading the model, loading it, ready (each with a percent where the host knows one). The Service URL, the examples and **Open ComfyUI** wait for **Ready**, and the app says so until then.
- Until the model is loaded, its Service URL answers `This model is starting` with HTTP 503 and a `Retry-After` header. Retry after that time; don't treat it as an outage.

To watch the progress, connect over SSH and follow the model's log. The app shows the exact command for your rental; it is your usual SSH command followed by `tail -f /workspace/.legion/serve.log`. The model server's own output is in `/workspace/.legion/server.log`: use the same command with that file to see why a model won't start.

## Keep it working

- A response must start within 100 seconds.
- A long silence closes the connection. On a long stream, send something at least every minute, such as an SSE comment line.

## Keep it private

A Service URL isn't a secret: rental names can be guessed. Other services on `ambi.run` can also send requests to yours from a visitor's browser. Don't rely on SameSite cookies or on the URL staying private. Require an API key, or give a public service its own sign-in.

## Remove it

Delete the service from its page, or with `legion services delete NAME`. The Service URL stops working, and its keys with it.

## Limits

A rental and an organization can each have a limited number of exposed ports. When you reach one, the app tells you which, and you remove a port to add another.
