Docs / Get started

Quickstart: serverless endpoint

Deploy a serverless endpoint from a Python handler and call it over HTTP. Workers start on demand and scale to zero when the queue is empty.

1. Write a handler

A handler is a function that receives a job and returns JSON-serializable output. Load models at module level so they are loaded once per worker, not once per job.

worker/handler.py
import apipods
import torch
from diffusers import AutoPipelineForText2Image

# Runs once when the worker starts (and is captured by Warmstart).
pipe = AutoPipelineForText2Image.from_pretrained(
    "stabilityai/sdxl-turbo", torch_dtype=torch.float16
).to("cuda")


def handler(job):
    prompt = job["input"]["prompt"]
    steps = int(job["input"].get("steps", 4))
    image = pipe(prompt, num_inference_steps=steps, guidance_scale=0.0).images[0]
    path = f"/volume/outputs/{job['id']}.png"
    image.save(path)
    return {"path": path}


apipods.serverless.start({"handler": handler})

2. Add a Dockerfile and apipods.toml

worker/Dockerfile
FROM apipods/worker-base:1.9.1-py3.12-cuda12.8.1

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY handler.py .
CMD ["python", "-u", "handler.py"]
worker/apipods.toml
[endpoint]
name = "sdxl-turbo"
gpu_pool_ids = ["L40S", "L4"]
min_workers = 0
max_workers = 5
idle_timeout_in_sec = 5
execution_timeout_in_sec = 600
network_volume_id = "vol_c7d2h4ke"

gpu_pool_ids is a priority list: workers start on the first pool with capacity. See Endpoint settings.

3. Deploy

apipods endpoints deploy builds the image locally, pushes it to registry.apipods.com/<org>/<name> and creates the endpoint (or rolls out a new version if it exists).

shell
$ apipods endpoints deploy ./worker
Building registry.apipods.com/acme/sdxl-turbo:20261118-1002 ... done
Pushing ... done (4.1 GB, 38s)
Endpoint ep_4n8wq2zt (sdxl-turbo) is ready
Run URL: https://run.apipods.com/v1/ep_4n8wq2zt

Prefer your own registry or CI? Follow Build & push a worker image and Create an endpoint.

4. Send a request

/runsync waits up to 90 seconds for the result. For longer jobs use /run and poll /status, or pass a webhook.

curl -X POST https://run.apipods.com/v1/ep_4n8wq2zt/runsync \
  -H "Authorization: Bearer $APIPODS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"input": {"prompt": "a lighthouse on a cliff at dusk, 35mm photo", "steps": 4}}'
200 OK
{
  "id": "job_3x9wq2mt",
  "status": "COMPLETED",
  "delayTime": 812,
  "executionTime": 1934,
  "workerId": "wrk_8q3m2x9k",
  "output": {
    "imageUrl": "https://s3.eu-nl-1.apipods.com/vol_c7d2h4ke/outputs/job_3x9wq2mt.png",
    "seed": 1730299
  },
  "createdAt": "2026-11-18T10:02:13Z",
  "completedAt": "2026-11-18T10:02:16Z"
}

delayTime is how long the job waited in the queue (including any cold start) and executionTime is how long the handler ran, both in milliseconds.

What you pay

Flex workers are billed per second from the moment a worker starts until its idle timeout expires. With no traffic the endpoint scales to zero and costs nothing except any attached network volume. Pool rates are on the Pricing page.