Get Started With FastAPI

Get Started With FastAPI

FastAPI is a web framework for building APIs with Python. It leverages standard Python type hints to provide automatic validation, serialization, and interactive documentation. When you’re deciding between Python web frameworks, FastAPI stands out for its speed, developer experience, and built-in features that reduce boilerplate code for API development:

Use Case Pick FastAPI Pick Flask or Django
You want to build an API-driven web app
You need a full-stack web framework
You value automatic API documentation

Whether you’re building a minimal REST API or a complex backend service, understanding core features of FastAPI will help you make an informed decision about adopting it for your projects. To get the most from this tutorial, you’ll benefit from having basic knowledge of Python functions, HTTP concepts, and JSON handling.

Take the Quiz: Test your knowledge with our interactive “Get Started With FastAPI” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Get Started With FastAPI

This hands-on quiz will test your knowledge of FastAPI basics, from installation and endpoints to automatic JSON responses and Swagger UI.

Install FastAPI the Right Way

Installing FastAPI correctly sets the foundation for a smooth development experience. Unlike many Python packages that you can install with just pip install <package>, FastAPI’s installation approach has evolved to provide better out-of-the-box functionality. Understanding these installation options will save you time and prevent common configuration issues.

The recommended way to install FastAPI is with the [standard] extra dependencies. This ensures you get all the tools you need for developing an API without having to hunt down additional packages later:

Shell
$ python -m pip install "fastapi[standard]"

The quotes around "fastapi[standard]" ensure the command works correctly across different terminals and operating systems. With the command above, you install several useful packages, including the FastAPI CLI and uvicorn, an ASGI server for running your application.

If you prefer a minimal installation without these extras, then you can install just the core framework. However, you’ll likely need to install uvicorn separately to run your application:

Shell
$ python -m pip install fastapi uvicorn

For this tutorial, you’ll use the [standard] installation to take advantage of all FastAPI’s built-in capabilities. You can verify your installation by checking the FastAPI version in the Python REPL:

Python
>>> import fastapi
>>> fastapi.__version__
'0.116.1'

With FastAPI properly installed, you’re ready to create your first web API application. The next step is to build a minimal example that demonstrates FastAPI’s core capabilities.

Create the Most Minimal FastAPI App

Creating a basic FastAPI application requires just a few lines of code. You’ll start with a basic “Hello, World!” example that demonstrates the framework’s approach to building APIs.

You can name your FastAPI main file however you like. Common names are app.py, api.py, or main.py. To follow along, create a file called main.py:

Python main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
    return {"message": "Hello, FastAPI!"}

Locked learning resources

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

Unlock This Article

Already a member? Sign-In

Locked learning resources

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

Unlock This Article

Already a member? Sign-In

About Philipp Acsany

Philipp is a core member of the Real Python team. He creates tutorials, records video courses, and hosts Office Hours sessions to support your journey to becoming a skilled and fulfilled Python developer.

» More about Philipp

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

What Do You Think?

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.


Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

Become a Member to join the conversation.

Keep Learning

Related Topics: intermediate api front-end web-dev