# Create a service

> Create an autoscaled service in five steps in the app, with the legion CLI, with the API or from an agent over MCP, and what you have after it is created.

Source: https://legion.ambient.xyz/docs/renting/services/create

After this page you have a service with a Service URL and one key, and you know how to create one in the app, from a terminal, with the API and from an agent.

This page creates an autoscaled service. To put a service on a port of a rental, see [Expose a port](https://legion.ambient.xyz/docs/renting/services/expose-a-port). To serve a model from one rental, rent a premade model: it gets a service with one key, and you can [turn autoscaling on](https://legion.ambient.xyz/docs/renting/services/overview#from-one-rental-to-autoscaling) later.

You need the permission to manage services (`services.manage`: organization owners and members have it) and credit in your organization. Creating a service with at least one always-on replica needs credit at once, and a service that scales to zero starts spending when its first request arrives.

## Create it in the app

Open **Rent**, then **Services**, then **New service**. The first choice is where it runs: **On a rental** ("Use a port on a machine you already rent") takes the short path in [Expose a port](https://legion.ambient.xyz/docs/renting/services/expose-a-port), and **Autoscaled** ("Legion runs it and scales with traffic") takes the five steps below.

While you go through the five steps, an estimate of the cost stays on the right (at the bottom on a phone) and updates as you choose. Your answers survive a reload.

### 1. Model

Choose what to serve.

- **Legion models** lists the premade models. Each shows the GPUs it needs at least and what it can do.
- **My own image** appears only when your organization may use its own images. Give the image, the port it serves on, the kind (`openai` or `http`) and the model name. Then say what it needs, because Legion can't know: the **minimum total GPU memory** per replica and the **GPU counts** it can use (1, 2, 4 and 8 by default). The Hardware step stays blocked until you have set them. A private image needs a registry credential saved first: see [Private images](https://legion.ambient.xyz/docs/renting/images#private-images). Your image runs on rented hosts, so don't put secrets in it.

Under **Advanced** you can add environment variables and secrets, change a model's settings, and, for your own image, set the command and arguments. See [Configuration](https://legion.ambient.xyz/docs/renting/configuration). Changing them later rolls the replicas.

Then pick the **Service name**. Use lower case letters, numbers and hyphens. The name must be unique within your organization, so two organizations can use the same name. Legion adds a short random ending to make the Service URL: the service will be at `https://<name>-<suffix>.ambi.run`, for example `https://my-llm-7k2f.ambi.run`. The ending keeps URLs from being guessable, never changes, and is shown on the service and in the create response.

### 2. Hardware

You don't have to choose GPUs. By default Legion picks the **cheapest hardware that fits**: for each replica it looks at every eligible offer and every GPU count the model can use, keeps the ones that can run it, and rents the cheapest in total. All you set are limits.

- **Never pay more than** (optional). The most one replica may cost per hour, fees included. This is a price for the whole replica, not per GPU. Legion won't start a replica above it. For spot, it is also your bid.
- **GPUs.** Choose **Any GPU that fits** (recommended, and the default) or **Only these GPUs**. The second shows a list of only the GPUs that fit the model, each with its count per replica, GPU memory, price per hour and the number of hosts offering it. Tick the ones you accept. The list has no order: price decides.
- **Spot or on-demand.** Spot is cheaper: a host can reclaim the GPU, and Legion starts a replacement. On-demand costs more: the GPU is yours until the replica stops. See [Interruptible rentals](https://legion.ambient.xyz/docs/renting/interruptible-rentals) for how the spot price works.
- **Placement.** Under **Advanced placement**, choose **eligible hosts** (the default: any host that passes Legion's checks) or **only my own hosts**. See [Own hosts and eligible hosts](https://legion.ambient.xyz/docs/renting/services/keys-and-security#own-hosts-and-eligible-hosts).
- **Your own image.** The requirements you gave in the Model step decide which GPUs fit and appear here. Premade models state their own.

The step shows the **likely hardware**: the cheapest fit right now, its price per hour, the range of prices a replica could cost and how many hosts can run it, for example "1 x RTX 4090 at 0.42 an hour, 14 hosts available". It is a current price, not a promise: see [How Legion chooses hardware](https://legion.ambient.xyz/docs/renting/services/scaling#how-legion-chooses-hardware). When no host fits, it says so and offers an alert: see [Offer alerts](https://legion.ambient.xyz/docs/renting/offer-alerts).

### 3. Scaling and budget

Set how far the service scales and what it may spend. Every field has a default, and [Scaling and cold starts](https://legion.ambient.xyz/docs/renting/services/scaling) explains each one.

- **Minimum and maximum replicas**, **always-on replicas**, **target requests per replica** and **idle timeout**.
- **Monthly spend cap.** Required. The form suggests one. When this month's spend reaches it, the service pauses. See [Service billing](https://legion.ambient.xyz/docs/renting/services/billing).
- **Email owners at.** The share of the cap, 50, 80 or 100 percent, at which Legion emails the owners.

### 4. Access

Choose **API key required** (the default) or **Public**, and optionally **Only these addresses**. See [Access](https://legion.ambient.xyz/docs/renting/services/keys-and-security#access). With an API key required, the service is created with its first key, so the URL is never open. Choosing **Public** for an autoscaled service shows a warning, and you confirm it: anyone with the URL can drive scale-up, up to your maximum replicas and your cap.

- **Key name.**
- **Expires**: in 7 days, 30 days, 90 days (the default), 1 year, or never. A key that never expires shows a warning and needs you to sign in again to confirm.
- **Requests per minute limit** (optional). It stops a runaway client from spending your cap.

With an API key required, callers need the key. Without it, requests are refused.

### 5. Review and create

Review the recap and choose **Create service**. The first time your organization creates a service, you also tick **I understand that hosts can see this traffic**. The text says: your replicas run on GPUs owned by Legion providers, and the host machine can see the model, the requests and the responses it handles. For your own image it adds that the host can see the image too.

Billing starts when the first replica starts, by the second, only while replicas run. With the minimum at 0 that is when the first request arrives.

### After you create it

The service opens with its status **Starting** and a dialog titled **Copy your key now**. This is the only time the key is shown. Legion keeps only a fingerprint of it, so a lost key can't be recovered, only replaced. Copy it, save it somewhere safe, tick the box and choose **Done**. If you reload before that, the key is gone: create another from the **Keys** tab and revoke the lost one.

The first replica needs a host, a model download and a start. The Overview shows the status and how long this usually takes. The Service URL answers once a replica is ready. Until then, calls get a 503 `service_starting`: see [Call a service](https://legion.ambient.xyz/docs/renting/services/call#when-no-replica-is-ready).

## Create it from a terminal

The [legion CLI](https://legion.ambient.xyz/docs/cli) has one command group for services, `legion services`, shortened to `legion svc`. It shows the cost before it creates anything:

```sh
legion services create support-chat \
  --catalog CATALOG_ID \
  --max-price 2.50 \
  --monthly-cap 400 \
  --ack-host-visibility
```

`--max-price` is an optional all-in price ceiling for one whole replica per hour (all its GPUs), and `--monthly-cap` is required, both in dollars. By default Legion picks the cheapest hardware that fits. To limit it, repeat `--gpu` with a GPU model (`--gpu H100 --gpu L40S`); the list is an allow-list and has no order. Anything you leave out is asked for on a terminal, with the platform's default in brackets. Before it creates anything, the command shows the likely hardware, the cost floor, the ceiling and the cap and asks you to confirm, or pass `--yes` in a script. `--ack-host-visibility` acknowledges that hosts can see the traffic, once per organization.

Other flags: `--spot`, `--placement own_hosts`, `--scale-to-zero` with `--min 0`, `--min`, `--max`, `--target-concurrency`, `--key-name` and `--key-expires 7d|30d|90d|365d|never`. For your own image use `--image IMAGE --port 8000 --kind openai|http --served-model NAME --min-gpu-memory 48 --gpu-counts 1,2`. The first key's secret is printed once.

The other commands are in the [command reference](https://legion.ambient.xyz/docs/cli/commands#services).

## Create it with the API

`createService` (`POST /v1/services`) takes the same choices as the form. It needs an `Idempotency-Key`, like [createRental](https://legion.ambient.xyz/docs/api/rentals/create-rental), so a retry never creates two services. Amounts are in minor units (cents).

The request has a `target`, an `access`, a `protocol` and, for an autoscaled service, a `monthly_spend_cap`:

- **`target`** starts with the `mode`, `autoscaled` or `rental`. With `autoscaled` it gives the model (`catalog_id`, or your own `image` with `serve`), `hardware` (`strategy: cheapest_fit`, `max_price_per_replica_hour`, optional `gpu_models`, `tier`, `placement`, and for your own image `min_gpu_memory_gb` and `gpu_counts`) and `scaling`. With `rental` it gives `rental_id` and `port`.
- **`access`** is `{ "mode": "public" | "keys", "sources": [...] }`. It defaults to `keys` for an autoscaled service and `public` for a service on a rental. `sources` is up to 16 CIDR ranges and works with either mode.
- **`protocol`** is `openai` or `http`.
- **`initial_key`** names the first key when access is `keys`, with `expires_in_days` (7, 30, 90 or 365) or `no_expiry`.
- **`acknowledge_host_visibility`** must be `true` the first time your organization creates an autoscaled service.
- **`name`** is required for an autoscaled service. For a service on a rental it defaults to the rental's name.

```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 '{"name": "support-chat", "target": {"mode": "autoscaled", "catalog_id": "CATALOG_ID"}, "access": {"mode": "keys"}, "monthly_spend_cap": {"amount_minor": 40000, "currency": "USD"}, "initial_key": {"name": "default", "expires_in_days": 90}, "acknowledge_host_visibility": true}'
```

The answer is `201` with the service and, under `initial_key`, the key and its `secret`, shown once. The response carries `Cache-Control: no-store`.

Before you create, `estimateService` (`POST /v1/services/estimate`, which takes the same `target` and `monthly_spend_cap`) returns `likely_hardware` (the cheapest GPU model and count that fit now, with `price_per_hour`), `price_range_per_replica_hour` (`min` and `max`), the cost per hour while idle, at the minimum and at the maximum, `cap_hours_at_max`, a suggested cap, the median `cold_start_seconds` of the model and `hosts_available`. `getServiceDefaults` (`GET /v1/services/defaults`) returns the defaults the platform uses, and the app and the CLI read them from there. When no host fits the model at your price, create answers `409 no_matching_offers`, so you hear it at once, not after waiting.

Setting a key to never expire, or creating or rotating a key on a signed-in session, needs a fresh sign-in (`403 step_up_required`): see [Keys and security](https://legion.ambient.xyz/docs/renting/services/keys-and-security#who-can-do-what).

Changing a service is `updateService` (`PATCH /v1/services/{service_id}`): scaling, budget, access, and the switch between one rental and autoscaling. Pausing and resuming are their own calls, or `paused` in the same request.

## Create it from an agent

An agent connected over [MCP](https://legion.ambient.xyz/docs/agents/mcp) creates a service with `create_service`, in either mode. It spends credit once a replica runs, so it asks first: without `confirm: true` it returns a preview with the price (the cost per hour while idle, at the minimum and at the maximum, and the cap), and does nothing. The agent passes `confirm: true` only after your yes. See [Service tools](https://legion.ambient.xyz/docs/agents/tools#service-tools).

The service tools that change anything work with a Full access API key. An Agent key, and an app connected by signing in, can only read services and get `403 api_key_scope` on a change: create the service in the app instead. A new key's secret comes back to the agent once, in the answer. The agent gives it to you to store, and shouldn't print it again or keep it.

## Before you move on

- Write the key down where your callers read secrets from, not in code.
- Open the Overview and check the cost card matches your plan.
- Call the service once: [Call a service](https://legion.ambient.xyz/docs/renting/services/call).
