URL shortener using FastAPI
My demo URL shortener (GH repo here) is a REST API accepts a regular URL via POST and returns a short code that can be used to get a redirect to the original URL. Users of the API receive a short code and a secret administrative key that they can use to check usage statistics for that short code and optionally delete the short code so it won’t work anymore.
Example of providing a full URL to get a short code:
curl -X 'POST' \
'https://www.timstewart.io/fastapi-demo-url-shortener/?full_url=https://www.thnr.net' \
-H 'accept: application/json'
API response:
{
"full_url": "https://www.thnr.net",
"short_code": "FX931",
"admin_key": "1TYRV42Otbua6R9L",
"when_created_unix": 1695159402,
"access_count": 0
}
After visiting https://www.timstewart.io/fastapi-demo-url-shortener/FX931 several times in my browser, I can check the stats for this short URL using the secret administrative key I was issued when creating it:
curl -X 'GET' \
'https://www.timstewart.io/fastapi-demo-url-shortener/FX931/1TYRV42Otbua6R9L' \
-H 'accept: application/json'
API response:
{
"full_url": "https://www.thnr.net",
"short_code": "FX931",
"admin_key": "1TYRV42Otbua6R9L",
"when_created_unix": 1695159402,
"access_count": 9
}
Tech Stack
- Python + FastAPI
- SQLite3 for persistent storage of short codes, URLs, and usage stats
- Nginx + gunicorn for web service