# Serve an OpenAI-compatible API with vLLM

You will start a server that speaks the OpenAI chat API and call it from your own code. The **vLLM** template runs a model server on port 8000. Any tool that can talk to an OpenAI-style API can use it by changing its base address.

## 1. Deploy the server

1. Open **Deploy**. Pick a GPU with **VRAM** of 16 GB or more, sorted by **Price, low to high**, and select **Continue**.
2. Above the template cards, select the **Language models** filter. Select the **vLLM (OpenAI-compatible)** card, then **Continue**.
3. Name the server `api-test`. Under **Environment variables**, check `MODEL`. The template sets it to `Qwen/Qwen2.5-1.5B-Instruct`, a small public model. Keep it for the first run.
4. Select **Continue**, check **Per hour** and select **Start my server**.

You can also use **Quick start** and choose **Serve a model as an API**: it picks this template and the cheapest server in stock that fits.

At the time of writing the vLLM template does not run on RTX 50-series and other Blackwell GPUs; **Deploy** marks those configurations and you choose another GPU.

To serve another model, set `MODEL` to its Hugging Face name. `MAX_MODEL_LEN` (8192 by default) is the longest prompt plus answer, in tokens, the server accepts; a lower value needs less GPU memory. For a model that needs a login, add your Hugging Face token as `HF_TOKEN` and mark it as secret. The model must fit in the GPU memory; see the [GPU memory guide](/docs/gpu-memory-guide).

## 2. Wait for the model

When the status is **Running**, open the server, select the **Connect** tab and copy the **App address** of port 8000. In the examples below it is `SERVER_ADDRESS`: the host and port, without `http://`. The server answers only after it has downloaded and loaded the model, which takes a few minutes the first time. Check from your own computer:

```bash
curl -i http://SERVER_ADDRESS/health
```

The same tab shows the API key, generated for this server when it started (or the `VLLM_API_KEY` you set yourself, which the tab does not show). Press the eye button to see it. Every request must carry it, so use it wherever the examples below say `YOUR_KEY`. Under **Try it from a terminal** the tab also has a ready-made `curl` line; **Copy with key** copies it with your key filled in.

`HTTP/1.1 200 OK` means it is ready. Until then you see an error such as `Connection refused`; wait and try again. Now list the model name your requests must use:

```bash
curl http://SERVER_ADDRESS/v1/models \
  -H "Authorization: Bearer YOUR_KEY"
```

The answer contains `"id":"Qwen/Qwen2.5-1.5B-Instruct"`.

## 3. Call it

With curl:

```bash
curl http://SERVER_ADDRESS/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_KEY" \
  -d '{"model": "Qwen/Qwen2.5-1.5B-Instruct", "messages": [{"role": "user", "content": "Say hello in five words."}]}'
```

With Python (`pip install openai`):

```python
from openai import OpenAI

client = OpenAI(base_url="http://SERVER_ADDRESS/v1", api_key="YOUR_KEY")
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)
```

With JavaScript (Node 18 or newer, `npm install openai`, save as `chat.mjs`, run `node chat.mjs`):

```js
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "http://SERVER_ADDRESS/v1", apiKey: "YOUR_KEY" });
const reply = await client.chat.completions.create({
  model: "Qwen/Qwen2.5-1.5B-Instruct",
  messages: [{ role: "user", content: "Say hello in five words." }],
});
console.log(reply.choices[0].message.content);
```

All three print a short greeting. Pass the key as `api_key`; the OpenAI libraries send it in the `Authorization: Bearer` header. Add `"stream": true` to get the answer piece by piece.

## Keep it private

> [!WARNING]
> This server answers only requests that carry its API key; without it the answer is `401`. Anyone who has the key can use the server, and the time they use is billed to you. Do not publish the key or put it in a public repository.

To keep the port closed to others, add an SSH key when you deploy and use a tunnel, with `PORT` and `HOST` from the SSH command on the **Connect** tab. Then use `http://localhost:8000/v1` as the base address:

```bash
ssh -N -L 8000:localhost:8000 -p PORT root@HOST
```

For an API with your own API key and no idle cost, use a serverless endpoint instead: [Scale to zero with a serverless endpoint](/docs/tutorial-serverless-endpoint).

## When you are done: stop or delete

- **Stop** ends the compute charge. The disk with the downloaded model is kept and still charged. After **Start**, the **Connect** tab shows the new address.
- **Destroy** ends every charge and deletes the disk. See [Start, stop and restart](/docs/start-stop-restart).

## Next

- [Scale to zero with a serverless endpoint](/docs/tutorial-serverless-endpoint)
- [Automate with the CLI and API](/docs/tutorial-cli-and-api)
