Simple FastAPI application

To get started with a FastAPI application, follow these steps:

1. Install FastAPI

First, make sure you have Python 3.6 or higher installed. Then, install FastAPI using pip:


pip install fastapi

 

2. Create a FastAPI App: 

Create a new Python file (e.g., main.py) and import FastAPI:


from fastapi import FastAPI

app = FastAPI()

 

3. Define a Route: Add a route to your app using a path operation decorator (e.g., @app.get("/")):


@app.get("/")

async def root():

    return {"message": "Hello World"}

 

4. Run the Development Server: Start the live server using the following command:


fastapi dev main.py

 

Access Your App:

Open your browser at http://127.0.0.1:8000 to see the JSON response: {"message": "Hello World"}.

 

Interactive API Docs:

Explore the automatic interactive API documentation at http://127.0.0.1:8000/docs (provided by Swagger UI).

 

Alternative API Docs:

Check out the alternative documentation at http://127.0.0.1:8000/redoc (provided by ReDoc).

 

OpenAPI Schema:

If you’re curious about the raw OpenAPI schema, view it directly at http://127.0.0.1:8000/openapi.json .

 

Happy coding!

No comments: