woman wearings coop neck floral top using her apple brand macbook
Photo by JÉSHOOTS on Pexels.com

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
    1. Minimal FastAPI project structure
    2. Implementing a simple “Hello, World!” endpoint in main.py
    3. Starting the server with Uvicorn
    4. Checking the response via curl or browser
    5. 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 in main.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

4.3 Swagger UI / 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:

  1. Prepare the project folder
  2. Implement the endpoint in main.py
  3. Start the server with Uvicorn
  4. 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!

By greeden

Leave a Reply

Your email address will not be published. Required fields are marked *

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)