How to create your first project with fastAPI
fastAPI is a modern web framework for building APIs with Python. It is fast, easy to use, and comes with built-in support for data validation, serialization, and documentation. In this article, we will guide you through the process of creating your first project using fastAPI.
Step 1: Install fastAPI
To get started, you need to install fastAPI. You can do this using pip by running the following command:
pip install fastapi
Step 2: Create a new Python file
Next, create a new Python file for your project. You can name it whatever you like, for example, app.py.
Step 3: Write your first fastAPI application
Now, you can start writing your fastAPI application. Here is a simple example to get you started:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
This code defines a simple fastAPI application with a single route ‘/’ that returns a JSON response {‘Hello’: ‘World’}.
Step 4: Run your fastAPI application
To run your fastAPI application, you can use the uvicorn server. Install uvicorn by running the following command:
pip install uvicorn
Then, you can start your fastAPI application by running:
uvicorn app:app --reload
This command starts the uvicorn server and reloads your application automatically when you make changes to your code.
Step 5: Test your fastAPI application
Now that your fastAPI application is running, you can test it by opening a web browser and navigating to http://localhost:8000/. You should see the JSON response {‘Hello’: ‘World’} displayed on the page.
Congratulations! You have successfully created your first project with fastAPI. Now you can start building more complex APIs and explore the features of fastAPI further.
refer url : https://fastapi.tiangolo.com/tutorial/