# How I Built and Deployed an AI Vision API from Scratch with YOLOv8 and FastAPI

> Source: <https://dev.to/lokendra_parihar/how-i-built-and-deployed-an-ai-vision-api-from-scratch-with-yolov8-and-fastapi-1i87>
> Published: 2026-08-18 12:53:49+00:00

I am a CS and Mathematics student from Ujjain, India. A few months ago

I was trying to learn computer vision and build real projects with it.

Then I hit a wall.

Every tutorial, every library, every framework assumed you had an NVIDIA

GPU. CUDA this. GPU memory that. "Just spin up a cloud GPU instance" —

at $2-3 per hour, which adds up to hundreds of dollars per month.

I have an Intel integrated graphics chip. 8GB RAM. No NVIDIA card. No

budget for cloud GPUs.

I couldn't do the thing I wanted to learn.

So I decided to build the solution I wished existed.

**LARA — Lightweight Adaptive Recognition API**

A computer vision API where you send an image and get back detected

objects, confidence scores, and bounding box coordinates in one simple

API call. The heavy AI computation runs on my server. You get the

results instantly. No GPU needed on your end. Ever.

Here is all the code a developer needs to use LARA:

``` python
import requests

response = requests.post(
    "https://web-production-41d94.up.railway.app/detect",
    headers={"x-api-key": "your-api-key"},
    files={"file": open("image.jpg", "rb")}
)

print(response.json())
```

That's it. Three lines. You get back:

```
{
  "status": "success",
  "count": 2,
  "detections": [
    {"label": "person", "confidence": 0.92, "box": [10, 20, 150, 300]},
    {"label": "car",    "confidence": 0.87, "box": [200, 50, 500, 400]}
  ]
}
```

Here is exactly what I used to build LARA:

**AI Model — YOLOv8 nano**

I chose YOLOv8 nano specifically because it is the smallest and fastest

variant of YOLOv8. It detects 80+ object classes accurately while being

light enough to run on a basic CPU server without timing out.

**API Framework — FastAPI**

FastAPI auto-generates beautiful API documentation, handles file uploads

cleanly, and is fast enough for production workloads. It also generates

OpenAPI specs automatically which made listing on RapidAPI easy.

**Database — Supabase**

Three tables: users, usage, billing. Every API call gets logged. Every

developer gets tracked. Supabase gave me a production-grade PostgreSQL

database with a clean Python client and Row Level Security — for free.

**Payments — Razorpay**

For Indian developers and businesses. Razorpay handles the billing so

I don't have to.

**Hosting — Railway**

Deployed via Docker. Railway auto-deploys every time I push to GitHub.

The entire deployment pipeline took about 30 minutes to set up.

I started with a basic FastAPI app:

``` python
from fastapi import FastAPI, File, UploadFile, Header, HTTPException
from PIL import Image
from ultralytics import YOLO
import io

app = FastAPI(title="LARA API")
model = YOLO("yolov8n.pt")

@app.post("/detect")
async def detect(file: UploadFile = File(...), x_api_key: str = Header(...)):
    contents = await file.read()
    img = Image.open(io.BytesIO(contents)).convert("RGB")
    results = model(img, verbose=False)

    detections = []
    for box in results[0].boxes:
        label = model.names[int(box.cls)]
        confidence = round(float(box.conf), 3)
        x1, y1, x2, y2 = [round(float(v)) for v in box.xyxy[0]]
        detections.append({
            "label": label,
            "confidence": confidence,
            "box": [x1, y1, x2, y2]
        })

    return {"status": "success", "detections": detections, "count": len(detections)}
```

I added API key validation and usage logging with Supabase:

``` python
def get_user(api_key: str):
    result = supabase.table("users").select("*").eq("api_key", api_key).execute()
    if result.data:
        return result.data[0]
    return None

def log_usage(api_key: str, endpoint: str):
    supabase.table("usage").insert({
        "api_key": api_key,
        "endpoint": endpoint,
        "timestamp": datetime.now().isoformat()
    }).execute()
```

YOLOv8 nano sometimes detects the same object twice with slightly

different bounding boxes. I implemented NMS (Non-Maximum Suppression)

manually to remove duplicates:

```
seen_boxes = []
for box in results[0].boxes:
    x1, y1, x2, y2 = [round(float(v)) for v in box.xyxy[0]]

    duplicate = False
    for seen in seen_boxes:
        sx1, sy1, sx2, sy2 = seen
        inter_area = max(0, min(x2,sx2)-max(x1,sx1)) * max(0, min(y2,sy2)-max(y1,sy1))
        union_area = (x2-x1)*(y2-y1) + (sx2-sx1)*(sy2-sy1) - inter_area
        iou = inter_area / union_area if union_area > 0 else 0
        if iou > 0.5:
            duplicate = True
            break

    if not duplicate and confidence >= 0.3:
        detections.append({...})
        seen_boxes.append((x1, y1, x2, y2))
```

The biggest challenge was the Linux server not having the display

libraries OpenCV needs. The fix was switching to

`opencv-python-headless`

and building with a proper Dockerfile:

```
FROM python:3.10-slim
WORKDIR /app
RUN apt-get update && apt-get install -y libglib2.0-0 libgl1 libxcb1
COPY requirements.txt .
RUN pip install --no-cache-dir opencv-python-headless==4.10.0.84
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
```

If you are a developer, student, or researcher who wants to add computer

vision to your project without:

LARA is for you.

A parking management system. A security camera app. A retail analytics

dashboard. A wildlife monitoring tool. All of these need object

detection. None of them should need a $3000 GPU setup to get started.

Register at the landing page and get your API key instantly — no credit

card, no waiting:

👉 [https://lokendraparihar-9977.github.io/lara-landing](https://lokendraparihar-9977.github.io/lara-landing)

Full API docs:

👉 [https://web-production-41d94.up.railway.app/docs](https://web-production-41d94.up.railway.app/docs)

Free tier includes 100 API calls/month. Paid plans start at ₹249/month

(≈ $2.99) for 500 calls — cheaper than 30 minutes on a cloud GPU.

I am building specialized models for:

If you have a use case you want LARA to support, let me know in the

comments. I am building this based on real developer needs.

*Built by Lokendra Singh Parihar — CS + Mathematics student,
Vikram University, Ujjain, India.*
