# Scale to zero with a serverless endpoint

A serverless endpoint gives you one HTTPS address for a model server. GPU workers start when requests arrive and are released when it is quiet, so with a minimum of zero workers you pay nothing while it is idle. If the **Endpoints** page says endpoints are not available yet, they are not switched on for your account.

## How you are billed

There is no price per request. Each worker is an ordinary server, billed by the second at the hourly price of its GPU and disk from the moment it starts, including while it loads the model, until it is released. The price is shown on the endpoint page. See [Serverless endpoints](/docs/endpoints).

## 1. Create an API key

1. Open **API keys** and select **Create key**.
2. Name it `endpoint-test` and under **Permissions** choose **Servers**. This scope can call an endpoint and wake its workers.
3. Select **Create key**, copy the key and keep it safe. It is shown only once.

In your terminal, store it for the commands below:

```bash
export APIDLY_API_KEY="paste-your-key-here"
```

## 2. Create the endpoint

1. Open **Endpoints** and select **New endpoint**. It has three steps: **GPU**, **Template** and **Scaling**.
2. Pick a GPU with **VRAM** of 16 GB or more and select **Next**.
3. Choose **vLLM endpoint worker (OpenAI-compatible)** and select **Next**.
4. Name it `chat-endpoint` and keep the settings: **Minimum workers** 0, **Maximum workers** 2, **Idle timeout (minutes)** 5.
5. Select **Create endpoint**.

The worker serves the template's default model, `Qwen/Qwen2.5-1.5B-Instruct`. The console form has no variables field. To serve another model, create the endpoint with the [command-line tool](/docs/cli) and `--env MODEL=...`.

To limit spending, set **Spending cap per hour**. The endpoint then never runs more workers than that amount pays for.

## 3. Find the address

Open the endpoint. The page shows **Endpoint URL** and **OpenAI base URL**, code samples in curl, Python and JavaScript, and a **Test request** box. The base URL looks like this, with your own endpoint id:

```text
https://apidly.com/api/v1/endpoints/EPN_ID/openai/v1
```

## 4. Call it

The first request after an idle period has to wait while a worker starts, finds a machine and loads the model. That can take several minutes. A single request is held for at most 240 seconds, so you may get `503` with a `Retry-After` header first. That is normal: wait and try again.

```bash
curl -i https://apidly.com/api/v1/endpoints/EPN_ID/openai/v1/chat/completions \
  -H "Authorization: Bearer $APIDLY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "Qwen/Qwen2.5-1.5B-Instruct", "messages": [{"role": "user", "content": "Say hello in five words."}]}'
```

The same call in Python (`pip install openai`), with a loop that waits for the worker:

```python
import os
import time

from openai import APIStatusError, OpenAI

client = OpenAI(
    base_url="https://apidly.com/api/v1/endpoints/EPN_ID/openai/v1",
    api_key=os.environ["APIDLY_API_KEY"],
    max_retries=0,
)
for attempt in range(60):
    try:
        reply = client.chat.completions.create(
            model="Qwen/Qwen2.5-1.5B-Instruct",
            messages=[{"role": "user", "content": "Say hello in five words."}],
        )
        print(reply.choices[0].message.content)
        break
    except APIStatusError as e:
        if e.status_code != 503:
            raise
        wait = int(e.response.headers.get("Retry-After", "10"))
        print(f"No worker ready yet. Trying again in {wait} s.")
        time.sleep(wait)
```

Once a worker is ready, answers come back at once. The **Workers** list on the endpoint page shows it as ready.

## 5. Watch it scale to zero

Stop sending requests. After the idle timeout, 5 minutes here, the worker is released and the page shows **No workers right now. One starts when a request arrives.** From then on the endpoint costs nothing until the next request. To keep one worker ready, raise **Minimum workers**; you then pay for it all the time.

## When you are done: pause or delete

- **Pause** releases all workers. Requests are refused with `503` and nothing is billed. **Resume** brings it back.
- **Delete** removes the endpoint for good. Requests stop at once and its workers are released within a few minutes. This cannot be undone.
- Avoid **Stop** for a long break: it keeps the workers' disks, and disks are billed.

From a terminal: `apidly endpoints pause EPN_ID` or `apidly endpoints rm EPN_ID`. Also revoke the `endpoint-test` key on the **API keys** page if you no longer need it.

## Next

- [Serverless endpoints](/docs/endpoints)
- [Save money](/docs/tutorial-save-money)
- [Protect an endpoint: rate limit, priorities, lifetimes, automatic rollback](/docs/tutorial-protect-an-endpoint)
