Let's create a super basic "Hello World" server using FastAPI.
FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.7+ based on standard Python type hints.
1. Project Setup
First, let's set up your environment. It's highly recommended to use a virtual environment.
# 1. Create a directory for your project
mkdir fastapi_hello_world
cd fastapi_hello_world
# 2. Create a virtual environment (if you don't have venv, it's usually built-in with Python 3.3+)
python -m venv venv
# 3. Activate the virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows (Command Prompt):
.\venv\Scripts\activate
# On Windows (PowerShell):
.\venv\Scripts\Activate.ps1
# 4. Install FastAPI and Uvicorn
# Uvicorn is an ASGI server that runs your FastAPI application
pip install fastapi uvicorn
2. Create Your FastAPI Application
Now, create a Python file (e.g., main.py) inside your fastapi_hello_world directory and paste the following code:
main.py
from fastapi import FastAPI
# Create a FastAPI application instance
# This is the main object that will handle all your API requests.
app = FastAPI()
# Define a route using a decorator
# @app.get("/") means that this function will be executed when
# a GET request is made to the root URL ("/") of your server.
@app.get("/")
async def read_root():
"""
Returns a simple JSON message.
FastAPI automatically converts Python dictionaries to JSON responses.
"""
return {"message": "Hello, World!"}
# You can add more routes easily!
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None):
"""
Example of a route with a path parameter (item_id) and a query parameter (q).
"""
return {"item_id": item_id, "q": q}
3. Run the Server
Make sure your virtual environment is still active, then run the server using Uvicorn:
uvicorn main:app --reload
Let's break down this command:
uvicorn: The command to run the Uvicorn server.
main: Refers to the Python file main.py.
app: Refers to the FastAPI() instance named app inside main.py.
--reload: (Optional, but recommended for development) This flag tells Uvicorn to restart the server automatically whenever you make changes to your code.
4. Test Your Server
Once you run the uvicorn command, you'll see output similar to this:
INFO: Will watch for changes in .
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [12345]
INFO: Started server process [67890]
INFO: Waiting for application startup.
INFO: Application startup complete.
Now, open your web browser or use a tool like curl:
-
Visit in your browser: http://127.0.0.1:8000
You should see: {"message":"Hello, World!"}
-
Visit the items endpoint: http://127.0.0.1:8000/items/5?q=fastapi
You should see: {"item_id":5,"q":"fastapi"}
-
Check out the automatic API documentation (Swagger UI): http://127.0.0.1:8000/docs
This is one of the coolest features of FastAPI! It automatically generates interactive API documentation based on your code. You can test your endpoints directly from this page.
-
Check out the alternative API documentation (ReDoc): http://127.0.0.1:8000/redoc
5. Stop the Server
To stop the server, go back to your terminal and press CTRL+C.
That's it! You've successfully created and run a basic "Hello World" FastAPI server.