Simple Start! How to Run a Minimal “Hello, World!” API with FastAPI
📋 Article Highlights (Summary)
- Who Is This For?
Beginners comfortable with basic Python but new to web frameworks who want to experience a working API - What You Will Learn
- Minimal FastAPI project structure
- Implementing a simple “Hello, World!” endpoint in
main.py
- Starting the server with Uvicorn
- Checking the response via curl or browser
- Using auto-generated documentation (Swagger UI / ReDoc)
- What You’ll Gain
- Grasp the fundamentals of API development by running an API with just a few files and lines of code
- Experience FastAPI’s ease of use and the convenience of auto-generated docs, and take the next step
🎯 Target Audience
- Student Engineer A (early 20s)
Wants to implement an API for a class project but doesn’t know where to start - Professional Engineer B (30s)
Wants to quickly turn an existing Python tool into a testable API - Hobbyist C (40s)
Wants to create a simple, callable endpoint for a personal project
♿ Accessibility Level
- Readability: Short paragraphs and bullet points with balanced use of kanji, hiragana, and katakana
- Code Presentation: Fixed-width code blocks with clear comments
- Navigation: Key-point summaries at the end of each section for screen-reader users
- Terminology: Inline explanations for technical terms on their first appearance
1. Project Setup & Folder Structure
First, create your working folder:
mkdir fastapi-hello
cd fastapi-hello
- Tip: Use a clear folder name with letters, numbers, and hyphens for easy management.
If you haven’t created a virtual environment yet, run:
python3 -m venv .venv && source .venv/bin/activate
Key Points
- Create the
fastapi-hello/
folder- Activate a virtual environment in
.venv
if needed
2. Minimal Code: Create main.py
Create main.py
in the project root and paste the following:
# main.py
from fastapi import FastAPI # Import the FastAPI class
app = FastAPI() # Create the application instance
@app.get("/") # Define an HTTP GET endpoint at the root ("/")
async def read_root():
"""
A simple endpoint that returns Hello, World!
"""
return {"message": "Hello, World!"}
- Notes:
from fastapi import FastAPI
calls in the framework.- The
@app.get("/")
decorator maps the endpoint and HTTP method. - Using
async def
makes future asynchronous extensions easier.
Key Points
- Create the app with
FastAPI()
- Set routing with
@app.get("/")
- Return a simple dictionary (
dict
) as the response
3. Start the Server with Uvicorn
Use the ASGI server Uvicorn to run your app:
uvicorn main:app --reload
- main:app refers to the
app
instance inmain.py
. --reload
automatically restarts the server on code changes, ideal for development.
On success, you’ll see:
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
Key Points
- Run
uvicorn main:app --reload
to start the dev server- Note the URL displayed in the logs
4. Check the Response & Use Auto-Documentation
4.1 Verify with curl
In another terminal (or the same one), run:
curl http://127.0.0.1:8000/
Expected output:
{"message":"Hello, World!"}
4.2 Verify in Browser
- URL: http://127.0.0.1:8000/
→ You’ll see the JSON response.
4.3 Swagger UI / ReDoc
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
These pages auto-generate your API spec and let you test requests interactively.
Key Points
- Verify with curl or browser
- Explore
/docs
and/redoc
for auto-generated documentation
5. Summary & Next Steps
We’ve shown how to run a minimal “Hello, World!” API with FastAPI in just a few steps:
- Prepare the project folder
- Implement the endpoint in
main.py
- Start the server with Uvicorn
- Confirm behavior via curl, browser, and auto-docs
Next, try…
- Extending Routes: Add path and query parameters
- Request Bodies: Use Pydantic models to handle input data
- Data Persistence: Integrate with SQLAlchemy or Tortoise ORM for CRUD operations
Use this simple API as a springboard to dive deeper into FastAPI!