Getting Started with FastAPI: Your First “Hello World” API

Joël-Steve N.
3 min read3 days ago

--

Welcome to the world of FastAPI, where building APIs is as enjoyable as sipping your favorite cup of coffee on a lazy Sunday morning! In this introductory guide, we’ll walk you through everything you need to know to install FastAPI, create your very first “Hello World” API, and explore the magic of auto-generated documentation with Swagger UI. Whether you’re a beginner or just looking to refresh your skills, this guide has got you covered.

1. Installing FastAPI and Uvicorn

Before diving into coding, let’s set up your development environment. FastAPI is a modern, high-performance web framework for Python that makes API development a breeze. To get started, follow these simple steps:

  1. Create a Virtual Environment
    Open your terminal and create a new directory for your project:
mkdir fastapi-hello-world
cd fastapi-hello-world

Then, create and activate a virtual environment:

python -m venv venv
# On macOS/Linux:
source venv/bin/activate
# On Windows:
venv\Scripts\activate

2. Install FastAPI and Uvicorn
FastAPI is lightweight, but you’ll also need Uvicorn, a lightning-fast ASGI server, to run your application:

pip install fastapi uvicorn

That’s it! You’re now ready to build your API.

2. Creating Your First “Hello World” API

Now, let’s get to the fun part — writing some code! We’ll create a simple API that returns a friendly “Hello World” message.

  1. Create a New Python File
    In your project directory, create a file named main.py.
  2. Write Your API Code
    Open main.py in your favorite code editor and add the following code:
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
return {"message": "Hello World!"}
  • Here’s what’s happening:
  • We import FastAPI and create an instance of it.
  • We define a GET endpoint at the root URL (/) that returns a JSON response with the message "Hello World!".

3. Run Your API
Start the server with Uvicorn by running:

uvicorn main:app --reload

The --reload flag is a developer’s best friend—it automatically reloads your code whenever you make changes.

4. Test It Out
Open your web browser and navigate to http://127.0.0.1:8000. You should see:

{"message": "Hello World!"}

Congratulations! You’ve just created your first FastAPI application.

3. Exploring Auto-Generated Documentation with Swagger UI

One of FastAPI’s coolest features is its automatic documentation generation. FastAPI uses OpenAPI to generate interactive docs, so you can easily test and understand your API without writing extra code.

  1. Access Swagger UI
    With your server still running, head over to http://127.0.0.1:8000/docs. You’ll be greeted by a sleek, interactive interface that lets you see all your endpoints in action. It’s like a playground for your API!
  2. Try It Out
    Click on the / endpoint, then hit the “Try it out” button, and finally “Execute.” You’ll see the response directly in your browser. It’s a great way to experiment and debug your API while having fun!
  3. Additional Documentation with ReDoc
    If you’re curious to see another style of documentation, visit http://127.0.0.1:8000/redoc for a different, yet equally impressive, presentation of your API details.

Final Thoughts

Building your first API with FastAPI is just the beginning. As you grow more comfortable with this powerful framework, you’ll discover endless possibilities — from adding authentication and middleware to integrating with databases and deploying at scale. And remember, every expert was once a beginner who typed “Hello World” for the first time.

So, why not embrace the journey? Grab your virtual coffee, dive into FastAPI, and enjoy the ride!

If you found this guide helpful and fun, don’t forget to subscribe for more tips, tutorials, and a dash of humor in your coding adventures.

Happy coding, and may your APIs be secure and your bugs be few!

--

--

Joël-Steve N.
Joël-Steve N.

Written by Joël-Steve N.

Senior Back-End Developer (Python, JavaScript, Java) | Community Manager & Tech Leader

No responses yet