Skip to content
Project template·Pro

FastAPI link shortener

A link shortener in Python with FastAPI and SQLite: it creates short codes, redirects and counts clicks. It comes with a one-page form and pytest tests. It needs Python 3.10 or newer with pip in the Linux environment and installs 14 pinned packages.

You need PocketCode Pro to install it.

If PocketCode is installed on your Android device, this opens the page in the app.

Preview

Files

README.md
main.py
pytest.ini
requirements-dev.txt
requirements.txt
shortener/__init__.py
shortener/app.py
shortener/codes.py
shortener/home.py
shortener/store.py
tests/test_links.py

README.md

# Link shortener: FastAPI + SQLite A small web API in Python with FastAPI: paste a long link, get a short code, and `/<code>` sends you on to the long one while it counts the clicks. Links are kept in SQLite (the `sqlite3` module that comes with Python). It has a one-page form at `/`, typed request and response models with Pydantic, input checks, and tests. ## Run it in PocketCode You need the app's Linux environment with Python 3.10 or newer and pip (the Developer pack in Terminal › Linux includes both). 1. Open the terminal in this project's folder (Open Terminal Here, in the editor menu). 2. Install FastAPI and the Uvicorn server (14 packages, versions pinned in `requirements.txt`; needs internet): ```sh python3 -m pip install --break-system-packages -r requirements.txt ``` 3. Start the server: ```sh python3 main.py ``` Or open `main.py` and tap Run (the Run panel should say Python 3 (Linux), since the packages are installed there). 4. Open http://localhost:8000 in the DevBrowser (or in the phone's browser) and shorten a link. The short link opens the long one. Stop it with Ctrl+C in the terminal, or Stop in the Run panel. Links are kept in `data/links.db`. If you prefer a virtual environment: `python3 -m venv .venv`, then `. .venv/bin/activate` and `pip install -r requirements.txt`. ## Try the API From a second terminal tab: ```sh

This preview is shortened. You get the full content when you install it.

Changes in 1.0.0

First version.

Related

Flask + SQLite web appProProject templateA reading list in Flask with server-rendered pages and plain forms, SQLite storage and a small JSON API. The tests use Flask's test client. It needs Python and pip in the Linux environment and installs 7 pinned packages.Python CLI with SQLiteProProject templateAn expense tracker for the terminal. It adds and lists expenses, totals them per category each month and exports CSV. It only uses the standard library (argparse, sqlite3), so there's nothing to pip install, and it has unittest tests. It needs Python in the Linux environment.Python CSV reportProProject templateA script that turns a CSV of sales into a summary in the terminal and an HTML page with bar charts of totals per month, category and product. It only uses the standard library and includes sample data and tests. It needs Python in the Linux environment.Express + SQLite tasks APIProProject templateA tasks API on Express 5 with filtering, paging and tests. Data goes to node:sqlite, so nothing native has to compile. It needs the Linux environment with Node.js 22.13 or newer, and npm install fetches about 3.5 MB.Node.js JSON API without dependenciesProProject templateA notes API on node:http: create, list, update and delete notes, with input checks and JSON errors, saved to a file. You don't install anything. Tap Run on server.js and the app's own Node.js starts it on localhost:3000. The project includes tests.Next.js + Tailwind + i18nProProject templateNext.js 15 project with next-intl and Tailwind CSS, and one page in English and Spanish at /en and /es. Dark mode follows the system. Needs the Linux environment with Node.js; the first npm install downloads around 400 MB.