How to Build Your First API in Python with FastAPI

Introduction

An API is how two programs talk to each other. When your app asks a server for data, it usually asks through an API. In this post, you will build your first API — an endpoint that returns a "Hello" message and a list — and test it in the browser.

Quick answer: Install FastAPI, create app = FastAPI(), write one route with @app.get(), run it with uvicorn, and open http://127.0.0.1:8000/ in the browser. The docs are generated automatically at /docs.

Finished result first: In the browser, / will show {"message": "Hello from PyBlitz API"} and /docs will show interactive docs where the endpoint can also be tested.

Prerequisites: Python and pip installed, basic terminal use. No previous API experience is required.

How your first FastAPI API works

Step 1: Install

pip install fastapi uvicorn

Step 2: Code — main.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
    return {"message": "Hello from PyBlitz API"}

@app.get("/items")
def items():
    return {"items": ["Python", "FastAPI", "API"]}

Step 3: Run and test

uvicorn main:app --reload

Open in the browser:

  • http://127.0.0.1:8000/ — the Hello message
  • http://127.0.0.1:8000/docs — automatic interactive docs, where the endpoint can also be tested

Why FastAPI?

  • An API can be built with very little code.
  • Docs are generated automatically — they do not need to be written separately.
  • Python type hints also help check input.

Common mistakes

  • Wrong uvicorn command: If the file is main.py, the command is main:app — if the file name changes, the name in the command must change too.
  • Port already busy: If port 8000 is busy, add --port 8001.
  • Nothing shows in the browser: Check the terminal first — is the server running, or did it show an error?

What if it fails?

  • ModuleNotFoundError: fastapi: Run pip install fastapi uvicorn again, and check that you are in the correct Python environment.
  • Address already in use: Port 8000 is busy — try uvicorn main:app --reload --port 8001.
  • 404 on /items: Check the spelling in the URL — it is /items, not /item.

Expected output (browser, on /):

{"message": "Hello from PyBlitz API"}

FAQ

Q: Is this API live on the internet?
No, it only runs locally on your own computer. Deploying it live is a separate step.

Q: How is it different from Flask?
Both are fine. FastAPI has the advantage of automatic docs and speed, which is why it is a straightforward path for beginners.

Conclusion

You have built your first API — one route, one run command, and a test in the browser. The next step in this pillar will add data (POST), and then a complete small project will follow.

See more videos on YouTube: https://youtube.com/@pyblitz — Subscribe for more practical Python projects, explained step by step.

← Newer Post Older Post →

💬 Comments

PyBlitz — Learn Python. Build Real Projects.

Search This Blog

5 Python Tips — October 10, 2026 🐍

Today's quick roundup — five handy Python snippets you can copy, run, and learn from. Perfect for beginners and daily practice! 🐍 1. Password Generator 🔑 Generate a strong 12-character password by picking random letters, digits, and symbols with random.choice . import random, string chars = string.ascii_letters + string.digits + "!@#$%" password = "".join(random.choice(chars) for _ in range(12)) print("Your password:", password) 2. Guess the Number 🎲 A classic guessing game: Python picks a secret number between 1 and 20, and the while loop gives you "too high" / "too low" hints until you get it right. import random secret = random.randint(1, 20) guess = 0 while guess != secret: guess = int(input("Guess a number (1-20): ")) if guess secret: print("Too high!") print("You got it!") 3. Countdown Timer ⏱️ A simple countdown: the while loop prints the remaining seconds and pau...

How to Organize Messy Folders Automatically with Python

Introduction If your Downloads folder is full of mixed files, finding anything takes far too long. In this post, you will build a small Python script that sorts files by type into clean folders — Images, Documents, Videos, Music, and Others. No paid tool is needed, and no extra library needs to be installed — only Python's standard library. Quick answer: Use os and shutil to read each file's extension, create a folder for that type if it does not exist yet, and move the file there with shutil.move() . The full code is below. Finished result first: After running, the test folder will contain Images/, Documents/, Videos/, Music/, and Others/, with every test file in its correct category folder. Prerequisites: Python installed, basic terminal use, and a test folder. No library installation is required. How to organize folders with Python — step by step Choose a folder — try a test folder first, never run this directly on your real Downloads folder first. Read each fil...

Python Automation Toolkit: 5 Small Scripts That Save You Time Every Day

Introduction Automation does not mean big software — it means letting a script do the small jobs that eat your time every day. In this series, we will build 5 such scripts, one by one, each tested on test data first and on real data only later. Quick answer: Start with the File Organizer (Draft 1), then Bulk Rename, then a simple report/CSV script. The rule for every script is the same: test data first, real work second. This post is the Python automation scripts series opener — the full plan, with each script's own full article to follow. Finished result first (by the end of the series): 5 small scripts, each verified on a test folder, that handle everyday repeat work — sorting, renaming, reporting, and backup — by themselves. Prerequisites: Basic Python (variables, loops, functions). Each script post will list its own prerequisites separately. Python automation scripts — the 5 scripts (series plan) File Organizer — sort files into folders by type (Draft 1, full code in...

⚡ Follow PyBlitz

⚡ PyBlitz Playground — run Python right here

Code

Output

Press Run to see the output...
Python engine loads on first use (one-time, ~10 seconds).