Building a URL Shortener With FastAPI and Python (Summary)
You’ve built a web app with FastAPI to create and manage shortened URLs. With your URL shortener, you can now convert long URLs into tiny, shareable links. When someone clicks your shortened URL, then your URL shortener app will forward them to the targeted URL.
In this video course, you learned how to:
- Create a REST API with FastAPI
- Run a development web server with Uvicorn
- Model an SQLite database
- Investigate the auto-generated API documentation
- Interact with the database with CRUD actions
Here are some ideas for additional features:
- Custom URL key: Let your users create custom URL keys instead of a random string.
- Peek URL: Create an endpoint for your users to check which target URL is behind a shortened URL.
- Graceful Forward: Check if the website exists before forwarding.
When you feel ready, then hosting your project on Heroku is a great next step. If you want to make your URL shortener Python project even more user-friendly, then you can consider adding a front end. With a front end, you can provide a nice-looking interface for your users, so they don’t need to know about any API endpoints themselves.
No matter in which direction you take the development of your URL shortener, make sure to promote it in the comments below. Bonus points if you post a shortened link that forwards to your hosted project!
For more information on concepts covered in this video course, check out:
- Using FastAPI to Build Python Web APIs
- Python’s Requests Library (Guide)
- Python’s urllib.request for HTTP Requests
- Python Type Checking (Guide)
- Introduction to Python SQL Libraries
- Data Management With Python, SQLite, and SQLAlchemy
- Refactoring Python Applications for Simplicity
- pydantic documentation
- Uvicorn documentation
- FastAPI documentation
- Swagger UI overview
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Summary. Well done, you’ve made it to the end of the course. You’ve built a web app with FastAPI to create and manage shortened URLs. With your URL shortener, you can now convert long URLs into tiny, shareable links.
00:17 When someone clicks your shortened URL, then the shortener app will forward them to the targeted URL.
00:24 In this course, you learned how to create a REST API with FastAPI run a development server with Uvicorn, model a SQLite database, investigate the auto-generated API documentation, and interact with the database with CRUD actions.
00:45 The code that you wrote focused on getting the app working first, but you didn’t stop there. You took the time to inspect the codebase and spotted opportunities to refactor the code.
00:56 You’re now in a great position to think about extending your Python URL shortener. We hope you found this course useful, and we’ll see you again soon at realpython.com.
Darren Jones RP Team on Aug. 23, 2022
@vigo - Our apologies - the file has now been updated with the correct content - thanks for bringing it to our attention!
Joao Antonio Ferreira on Sept. 12, 2022
It is possible to create a Python script to invoke uvicorn? I tried this code below but it didn’t work.
File: run-app.py
import uvicorn
from .shortener_app import app
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000, log_level="info")
python3 run-app.py
Traceback (most recent call last):
File "~/dev/url_shortener/run-app.py", line 5, in <module>
from .shortener_app import app
ImportError: attempted relative import with no known parent package
Darren Jones RP Team on Sept. 14, 2022
Hi Joao
There are just a couple of small errors in your imports, rather than there being a fundamental issue with running uvicorn.
I’ve made a couple of changes to the code, and this works; firstly changing from a relative import - .shortener_app
to shortener_app
- note the missing .
.
Secondly, importing main
and invoking main.app
import uvicorn
from shortener_app import main
if __name__ == "__main__":
uvicorn.run(main.app, host="127.0.0.1", port=8000, log_level="info")
This is working for me on my system (as they say!) - let me know how you get on.
Become a Member to join the conversation.
vigo on Aug. 23, 2022
well, sample code zip file is empty :)