Skip to main content

fastapi-uvicorn-app

FastAPI vs Uvicorn: What's the Difference?

What is FastAPI?

FastAPI is a modern Python web framework for building APIs. It lets you define endpoints, handle requests, validate data, and build your application logic using Python type hints and async features.

What is Uvicorn?

Uvicorn is a lightning-fast ASGI server implementation. It serves your FastAPI (or any ASGI) application, handling HTTP requests and responses, and making your app accessible over the network.


Using Only FastAPI

  • FastAPI by itself is just the framework for building your API.
  • You can write your API code and define endpoints, but FastAPI does not include a production-ready server.
  • To actually serve your app to users, you still need an ASGI server (like Uvicorn).

Using Uvicorn with FastAPI

  • Uvicorn is the server that runs your FastAPI app and handles HTTP connections.
  • You typically start your app with a command like:
    uvicorn main:app --reload
  • In production, you should always use an ASGI server like Uvicorn to serve your FastAPI app for performance and reliability.

Key Point

  • FastAPI is for building the app (routes, logic, validation).
  • Uvicorn is for running the app and serving it to users.

You need both: FastAPI to create the API, and Uvicorn (or another ASGI server) to make it accessible over the network.

Analogy

  • FastAPI is like the engine of a car (it makes the car go).
  • Uvicorn is like the wheels (it lets the car move on the road).

You need both to drive!


Summary:

  • You can't serve a FastAPI app to the outside world without an ASGI server like Uvicorn.
  • In development, you might use uvicorn.run() inside your script, but in production, you should always run your app with the uvicorn command.