{"id":324,"date":"2025-05-03T08:19:27","date_gmt":"2025-05-03T08:19:27","guid":{"rendered":"https:\/\/felixaugenstein.com\/blog\/?p=324"},"modified":"2025-07-09T14:11:16","modified_gmt":"2025-07-09T14:11:16","slug":"getting-started-with-fastapi-and-uv-a-lightweight-python-web-framework-stack","status":"publish","type":"post","link":"https:\/\/felixaugenstein.com\/blog\/getting-started-with-fastapi-and-uv-a-lightweight-python-web-framework-stack\/","title":{"rendered":"Getting Started with FastAPI and uv: A Lightweight Python Web Framework Stack"},"content":{"rendered":"\n<p>FastAPI is a modern Python web framework that makes it easy to build APIs quickly and efficiently, with automatic documentation and strong type support. Combined with <code>uv<\/code> \u2014 a lightweight developer tool that wraps Python virtual environments and package management \u2014 it becomes even simpler to set up and maintain projects.<\/p>\n\n\n\n<p>In this post, you&#8217;ll learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What FastAPI is<\/li>\n\n\n\n<li>What <code>uv<\/code> is<\/li>\n\n\n\n<li>How to set up and run a FastAPI project using <code>uv<\/code> and <code>uvicorn<\/code><\/li>\n\n\n\n<li>Where to find a full example on GitHub<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is FastAPI?<\/h2>\n\n\n\n<p>FastAPI is a modern, high-performance web framework for building APIs with Python 3.7+ based on standard Python type hints. It\u2019s built on top of <strong>Starlette<\/strong> for web handling and <strong>Pydantic<\/strong> for data validation.<\/p>\n\n\n\n<p><strong>Why FastAPI?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\ude80 Super-fast performance (comparable to NodeJS and Go)<\/li>\n\n\n\n<li>\ud83d\udcc3 Automatic interactive API docs (Swagger and ReDoc)<\/li>\n\n\n\n<li>\ud83e\udde0 Built-in validation using Python type hints<\/li>\n\n\n\n<li>\ud83d\udd12 Easy integration of authentication and dependency injection<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is <code>uv<\/code>?<\/h2>\n\n\n\n<p><a class=\"\" href=\"https:\/\/github.com\/astral-sh\/uv\"><code>uv<\/code><\/a> is a fast and modern Python package manager developed by Astral. It wraps around virtual environments and provides an easy command-line interface for installing packages, managing dependencies, and isolating your Python projects.<\/p>\n\n\n\n<p>You can think of it as a faster and more intuitive alternative to <code>pip<\/code>, <code>venv<\/code>, and <code>pip-tools<\/code> \u2014 all in one.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Setting Up FastAPI with <code>uv<\/code><\/h2>\n\n\n\n<p>Here\u2019s a quick guide to spin up a FastAPI app using <code>uv<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Create and activate your environment<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>uv venv<br>source .venv\/bin\/activate<\/code><\/pre>\n\n\n\n<p>or initialize with a specific Python version<\/p>\n\n\n\n<p><code><code>uv venv --python=python3.12<\/code><br>source .venv\/bin\/activate<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Install FastAPI and a web server<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>uv pip install fastapi uvicorn<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Create your app<\/h3>\n\n\n\n<p>Make a new file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>touch main.py<br><\/code><\/pre>\n\n\n\n<p>Edit <code>main.py<\/code> and add this code to your simple app:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>from fastapi import FastAPI<br><br>app = FastAPI()<br><br>@app.get(\"\/\")<br>def read_root():<br>    return {\"message\": \"Hello, FastAPI + uv!\"}<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Run your server<\/h3>\n\n\n\n<p>Start the development server using <code>uvicorn<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>uvicorn main:app --reload<br><\/code><\/pre>\n\n\n\n<p>Visit <a class=\"\" href=\"http:\/\/127.0.0.1:8000\">http:\/\/127.0.0.1:8000<\/a> to see the response, and check out the auto-generated docs at <a class=\"\" href=\"http:\/\/127.0.0.1:8000\/docs\">http:\/\/127.0.0.1:8000\/docs<\/a> or redoc documentation under <a href=\"http:\/\/127.0.0.1:8000\/redoc\">http:\/\/127.0.0.1:8000\/redoc<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Managing Packages<\/h2>\n\n\n\n<p>Install additional packages:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">uv pip install somepackage anotherpackage<code><br><\/code><\/pre>\n\n\n\n<p>Freeze your dependencies:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>uv pip freeze &gt; requirements.txt<br><\/code><\/pre>\n\n\n\n<p>Reactivate if you run into &#8216;ModuleNotFoundError&#8217; with: <\/p>\n\n\n\n<p><code>source .venv\/bin\/activate<\/code><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Reinstall from <code>requirements.txt<\/code> later with:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>uv pip install -r requirements.txt<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Full Example<\/h2>\n\n\n\n<p>You can find the full example project on GitHub here:<br>\ud83d\udc49 <a class=\"\" href=\"#\"><strong>GitHub Repository<\/strong><\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>FastAPI + <code>uv<\/code> is a powerful combo for building modern, fast, and maintainable APIs with minimal setup. Whether you\u2019re prototyping or building production-ready services, this stack gets you moving quickly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>FastAPI is a modern Python web framework that makes it easy to build APIs quickly and efficiently, with automatic documentation and strong type support. Combined with uv \u2014 a lightweight developer tool that wraps Python virtual environments and package management \u2014 it becomes even simpler to set up and maintain [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":326,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58,27,19],"tags":[],"class_list":["post-324","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fastapi","category-python","category-rest-apis"],"_links":{"self":[{"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/posts\/324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/comments?post=324"}],"version-history":[{"count":7,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/posts\/324\/revisions"}],"predecessor-version":[{"id":342,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/posts\/324\/revisions\/342"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/media\/326"}],"wp:attachment":[{"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/media?parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/categories?post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/tags?post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}