Beginner’s Guide to FastAPI || Building Your Initial API

Posted by

Getting Started with FastAPI || Create Your First API

Getting Started with FastAPI

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It is easy to use, and has great features to make building APIs a breeze.

Installation

You can install FastAPI using pip:

pip install fastapi

Additionally, you can install an ASGI server to run your FastAPI application. For example, you can install uvicorn:

pip install uvicorn

Create Your First API

Now, let’s create a simple API using FastAPI. Create a new Python file (e.g., app.py) and add the following code:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

This code creates a FastAPI app with a single route that returns a JSON response with the message “Hello World” when the root URL is accessed.

Running Your API

To run your FastAPI application, use the uvicorn ASGI server. Run the following command in your terminal:

uvicorn app:app --reload

Now, visit http://127.0.0.1:8000/ in your web browser to see your API in action. You should see the message “Hello World” displayed on the page.

Conclusion

Congratulations! You’ve successfully created your first API using FastAPI. Explore more of FastAPI’s features and build powerful APIs with ease. Happy coding!

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@AliHassan-wm3re
2 months ago