avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1908

Abstract

unction_">read_root</span>(): <span class="hljs-keyword">return</span> {<span class="hljs-string">"message"</span>: <span class="hljs-string">"Hello, World"</span>}</pre></div><p id="1423">To run this application, use Uvicorn from the command line:</p><div id="f1f3"><pre>uvicorn <span class="hljs-selector-tag">main</span>:app <span class="hljs-attr">--reload</span></pre></div><p id="c772">Visit <code>http://127.0.0.1:8000</code> in your web browser, and you should see the JSON response <code>{"message": "Hello, World"}</code>.</p><h2 id="4848">Handling Path Parameters</h2><p id="330a">FastAPI allows you to define path parameters in your API routes. Let’s modify our previous example to accept a path parameter for a user’s ID:</p><div id="b65f"><pre><span class="hljs-meta">@app.get(<span class="hljs-params"><span class="hljs-string">"/users/{user_id}"</span></span>)</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">read_user</span>(<span class="hljs-params">user_id: <span class="hljs-built_in">int</span></span>): <span class="hljs-keyword">return</span> {<span class="hljs-string">"user_id"</span>: user_id}</pre></div><p id="74cb">Now, when you visit <code>http://127.0.0.1:8000/users/123</code>, you will receive the response <code>{"user_id": 123}</code>.</p><h2 id="9b95">Receiving JSON Data in Requests</h2><p id="77cf">FastAPI integrates seamlessly with Pydantic for handling JSON data in requests. Let’s modify our example to accept a POST request with a JSON payload:</p><div id="e03f"><pre><span class="hljs-keyword">from</span> pydantic <span class="hljs-keyword">import</span> BaseModel

<span class="hljs-keyword">class</span> <span class="hljs-title class_">Item</span>(<span class="hljs-title class_ inherited__">BaseModel</span>): name: <span class="hljs-built_in">str</span> description: <spa

Options

n class="hljs-built_in">str</span> = <span class="hljs-literal">None</span>

<span class="hljs-meta">@app.post(<span class="hljs-params"><span class="hljs-string">"/items/"</span></span>)</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">create_item</span>(<span class="hljs-params">item: Item</span>): <span class="hljs-keyword">return</span> item</pre></div><p id="9e5e">When you send a POST request to <code>http://127.0.0.1:8000/items/</code> with a JSON payload like <code>{"name": "item1", "description": "This is item 1"}</code>, you will receive the same JSON payload in response.</p><h2 id="2689">Conclusion</h2><p id="53b5">In this tutorial, we covered the basics of creating RESTful APIs with FastAPI in Python. We learned how to set up FastAPI, create API endpoints, handle path parameters, and receive JSON data in requests. FastAPI’s performance and ease of use make it a compelling choice for building modern web APIs with Python.</p><p id="1ee6">Now that you have a solid understanding of the fundamentals, you can continue learning about FastAPI for your specific use cases.</p><p id="b77d">For more in-depth learning and advanced features, you can refer to the FastAPI documentation and explore additional resources available online. Happy coding with FastAPI!</p><div id="7cdd" class="link-block"> <a href="https://readmedium.com/python-datetime-module-85295576060f"> <div> <div> <h2>Python DateTime Module</h2> <div><h3>undefined</h3></div> <div><p>undefined</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*4kSdlOKEQqdYroo_Bdg_dA.jpeg)"></div> </div> </div> </a> </div></article></body>

Python REST APIs with FastAPI

Building REST APIs with FastAPI in Python

In this tutorial, we will explore the main concepts of FastAPI and learn how to use it to quickly create web APIs that implement best practices by default. By the end of this tutorial, you will be able to start creating production-ready web APIs and have the understanding needed to go deeper and learn more for your specific use cases.

Overview of FastAPI

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. It is designed to be easy to use and highly performant, making it a popular choice for building RESTful APIs.

Setting Up FastAPI

To get started with FastAPI, first, you need to install it using pip:

pip install fastapi

You will also need an ASGI server, such as Uvicorn, to serve your FastAPI application:

pip install uvicorn

Creating Your First FastAPI Application

Let’s create a simple FastAPI application that responds to HTTP GET requests with a JSON payload. Create a new file called main.py and add the following code:

from fastapi import FastAPI

app = FastAPI()

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

To run this application, use Uvicorn from the command line:

uvicorn main:app --reload

Visit http://127.0.0.1:8000 in your web browser, and you should see the JSON response {"message": "Hello, World"}.

Handling Path Parameters

FastAPI allows you to define path parameters in your API routes. Let’s modify our previous example to accept a path parameter for a user’s ID:

@app.get("/users/{user_id}")
async def read_user(user_id: int):
    return {"user_id": user_id}

Now, when you visit http://127.0.0.1:8000/users/123, you will receive the response {"user_id": 123}.

Receiving JSON Data in Requests

FastAPI integrates seamlessly with Pydantic for handling JSON data in requests. Let’s modify our example to accept a POST request with a JSON payload:

from pydantic import BaseModel

class Item(BaseModel):
    name: str
    description: str = None

@app.post("/items/")
async def create_item(item: Item):
    return item

When you send a POST request to http://127.0.0.1:8000/items/ with a JSON payload like {"name": "item1", "description": "This is item 1"}, you will receive the same JSON payload in response.

Conclusion

In this tutorial, we covered the basics of creating RESTful APIs with FastAPI in Python. We learned how to set up FastAPI, create API endpoints, handle path parameters, and receive JSON data in requests. FastAPI’s performance and ease of use make it a compelling choice for building modern web APIs with Python.

Now that you have a solid understanding of the fundamentals, you can continue learning about FastAPI for your specific use cases.

For more in-depth learning and advanced features, you can refer to the FastAPI documentation and explore additional resources available online. Happy coding with FastAPI!

APIs
Rest
Fastapi
ChatGPT
Python
Recommended from ReadMedium