LinkShort
๐งช View animated test results โ 15/15 โ
A production-ready URL shortener โ FastAPI backend, JWT auth, click tracking, React 19 SPA, SQLite.
LinkShort (repository: FastAPI-url) is a complete URL-shortening service
with a Python/FastAPI API, JWT-based authentication, per-user link
management, click statistics, and a served single-page application frontend.
Stack: FastAPI (Python) ยท SQLAlchemy ยท SQLite ยท JWT (python-jose) ยท React 19 SPA (static files served by FastAPI) ยท pytest
Highlights
- Full auth flow โ register โ login โ JWT bearer tokens;
/auth/mereturns the current user - 6-character short codes โ cryptographically random (
secrets), with collision retry - Click tracking โ every redirect increments the click counter
- Link expiration โ optional
expires_in_secondsper link; expired links answer410 Gonewhile stats stay viewable - Per-user URLs โ
/urls/mylists your links; deletes are owner-scoped - 302 redirects โ
/urls/r/{code}redirects to the target with a click recorded - Public stats โ
/urls/{code}/statsreturns target + click count - SPA serving โ FastAPI mounts the built React app and falls back to
index.htmlfor client-side routing - CORS open โ
allow_origins=["*"]for development
Tech stack
| Layer | Technology |
|---|---|
| API | FastAPI (app = FastAPI(title="URL Shortener", version="1.0.0")) |
| ORM | SQLAlchemy (Base.metadata.create_all on startup) |
| Database | SQLite |
| Auth | JWT bearer tokens (python-jose) |
| Password hashing | app/auth.py (hashed, never stored plaintext) |
| Frontend | React 19 SPA in backend/static/ |
| Tests | pytest (tests/test_api.py) |
API surface
| Method | Path | Auth | Description |
|---|---|---|---|
POST |
/auth/register |
โ | Create account, receive JWT |
POST |
/auth/login |
โ | Authenticate, receive JWT |
GET |
/auth/me |
Bearer | Current user profile |
POST |
/urls/shorten |
Bearer | Shorten a URL |
GET |
/urls/my |
Bearer | List your URLs (newest first) |
GET |
/urls/{code}/stats |
โ | Public click stats |
DELETE |
/urls/{code} |
Bearer | Delete your URL (owner-scoped) |
GET |
/urls/r/{code} |
โ | 302 redirect + click count |
GET |
/health |
โ | Liveness probe |
Repository layout
FastAPI-url/
โโโ app/
โ โโโ main.py # FastAPI app, CORS, SPA serving
โ โโโ auth.py # JWT creation/verification, password hashing
โ โโโ database.py # SQLAlchemy engine + session
โ โโโ models.py # User, URL models
โ โโโ schemas.py # Pydantic schemas (UserCreate, Token, URLOut, URLStats)
โ โโโ config.py # Settings
โ โโโ routers/
โ โโโ auth_router.py # /auth/* endpoints
โ โโโ urls.py # /urls/* endpoints
โโโ backend/static/ # Built React SPA (served by FastAPI)
โโโ tests/
โ โโโ test_api.py # API integration tests
โโโ ...
Short-code generation
def gen_short() -> str:
chars = string.ascii_letters + string.digits
return ''.join(secrets.choice(chars) for _ in range(6))
6 characters from a 62-char alphabet โ 5.7ร10ยนโฐ combinations; collisions are checked and retried against the database.
Related pages
- Getting Started โ local setup and first run
- API Reference โ every endpoint with requests/responses
- Deployment โ Docker, environments, production notes