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.

Locked learning resources

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

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

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

Unlock This Lesson

Already a member? Sign-In

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.