Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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:

Download

Sample Code (.zip)

4.7 KB
Download

Course Slides (.pdf)

9.8 MB
Avatar image for vigo

vigo on Aug. 23, 2022

well, sample code zip file is empty :)

Avatar image for Darren Jones

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!

Avatar image for Joao Antonio Ferreira

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
Avatar image for Darren Jones

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.