<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FastAPI &#8211; Abdelhadi Dyouri</title>
	<atom:link href="https://adyouri.com/category/fastapi/feed" rel="self" type="application/rss+xml" />
	<link>https://adyouri.com</link>
	<description>Learn Programming</description>
	<lastBuildDate>Tue, 11 Jun 2024 11:27:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.3</generator>

<image>
	<url>https://adyouri.com/wp-content/uploads/2023/10/cropped-favicon-32x32.png</url>
	<title>FastAPI &#8211; Abdelhadi Dyouri</title>
	<link>https://adyouri.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>What is FastAPI Used For?</title>
		<link>https://adyouri.com/what-is-fastapi-used-for</link>
		
		<dc:creator><![CDATA[Abdelhadi Dyouri]]></dc:creator>
		<pubDate>Mon, 10 Jun 2024 18:26:37 +0000</pubDate>
				<category><![CDATA[FastAPI]]></category>
		<category><![CDATA[fastapi]]></category>
		<guid isPermaLink="false">https://adyouri.com/?p=1819</guid>

					<description><![CDATA[FastAPI is becoming the go to framework for building APIs in Python, and you must be wondering: what exactly is FastAPI used for? And how can I take advantage of it? I’ll break down everything you need to know about FastAPI in this article, and I will answer your main question: What is FastAPI used...]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">FastAPI is becoming the go to framework for building <a href="https://adyouri.com/what-is-an-api-in-simple-terms">APIs</a> in Python, and you must be wondering: what exactly is FastAPI used for? And how can I take advantage of it?</p>



<p class="wp-block-paragraph">I’ll break down everything you need to know about FastAPI in this article, and I will answer your main question: What is FastAPI used for?</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="768" height="512" src="https://adyouri.com/wp-content/uploads/2024/06/FastAPI.webp" alt="What is FastAPI Used For?" class="wp-image-1821" srcset="https://adyouri.com/wp-content/uploads/2024/06/FastAPI.webp 768w, https://adyouri.com/wp-content/uploads/2024/06/FastAPI-300x200.webp 300w" sizes="(max-width: 768px) 100vw, 768px" /></figure>





<h2 class="wp-block-heading">What is FastAPI Used For?</h2>



<p class="wp-block-paragraph"><a href="https://fastapi.tiangolo.com/" target="_blank" rel="noopener">FastAPI</a> is a fast web framework used for creating <a href="https://en.wikipedia.org/wiki/API" target="_blank" rel="noopener">APIs</a> with Python. It’s designed to be easy to use and understand, making it perfect for both beginners and experienced developers. FastAPI uses Python type hints for an efficient and straightforward way to define API endpoints, validate data, and generate documentation automagically.</p>



<h2 class="wp-block-heading">Why Use FastAPI?</h2>



<p class="wp-block-paragraph">Here are some key points that make FastAPI stand out:</p>



<ul class="wp-block-list">
<li><strong>Fast</strong>: FastAPI is designed for very fast response times.</li>



<li><strong>Easy</strong>: It has a simple and intuitive syntax, reducing the learning curve.</li>



<li><strong>Automatic Documentation</strong>: It automatically generates interactive API documentation with <a href="https://swagger.io/tools/swagger-ui/" target="_blank" rel="noopener">Swagger UI </a>and ReDoc.</li>



<li><strong>Type Safety</strong>: Leveraging Python’s <a href="https://docs.python.org/3/library/typing.html" target="_blank" rel="noopener">type hints</a>, FastAPI ensures type safety and better code completion.</li>
</ul>



<h2 class="wp-block-heading">Installing FastAPI</h2>



<p class="wp-block-paragraph">To help you understand what FastAPI is used for, we will get your hands dirty and actually use it to create a simple API.<br><br>First, let’s create a Python virtual environment then get FastAPI installed:</p>



<pre class="wp-block-code"><code>python -m venv env

source env/bin/activate</code></pre>



<p class="wp-block-paragraph">You’ll also need an ASGI server to run it, such as <a href="https://github.com/encode/uvicorn" target="_blank" rel="noopener">Uvicorn</a>. You can install both FastAPI and <code>uvicorn</code> using pip:</p>



<pre class="wp-block-code"><code>pip install fastapi uvicorn
</code></pre>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img decoding="async" width="800" height="438" src="https://adyouri.com/wp-content/uploads/2024/06/FastAPI-installation.webp" alt="FastAPI installation" class="wp-image-1826" style="width:500px" srcset="https://adyouri.com/wp-content/uploads/2024/06/FastAPI-installation.webp 800w, https://adyouri.com/wp-content/uploads/2024/06/FastAPI-installation-300x164.webp 300w, https://adyouri.com/wp-content/uploads/2024/06/FastAPI-installation-768x420.webp 768w" sizes="(max-width: 800px) 100vw, 800px" /></figure>
</div>


<h2 class="wp-block-heading">Creating Your First FastAPI App</h2>



<p class="wp-block-paragraph">Creating an API with FastAPI is straightforward. First open a new Python file named <code>app.py</code> :<br></p>



<pre class="wp-block-code"><code>nano app.py</code></pre>



<p class="wp-block-paragraph">Write the following code inside of this file:</p>



<pre class="wp-block-code"><code>from fastapi import FastAPI

app = FastAPI()

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

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}</code></pre>



<p class="wp-block-paragraph">In this example:</p>



<ul class="wp-block-list">
<li>We import FastAPI and create an instance of it.</li>



<li>We define a couple of endpoints using the <code>@app.get</code> decorator.</li>



<li>The first endpoint returns a simple JSON response.</li>



<li>The second endpoint accepts a path parameter (<code>item_id</code>) and an optional query parameter (<code>q</code>).</li>
</ul>



<h2 class="wp-block-heading">Running Your FastAPI App</h2>



<p class="wp-block-paragraph">To run the app, use Uvicorn:</p>



<pre class="wp-block-code"><code>uvicorn app:app --reload</code></pre>



<p class="wp-block-paragraph">This command starts the server with auto-reloading enabled. You can now navigate to <code>http://127.0.0.1:8000</code> in your browser to see your API in action.</p>



<h2 class="wp-block-heading">Testing Your FastAPI App with Curl</h2>



<p class="wp-block-paragraph">Testing your FastAPI application with <code>curl</code> is straightforward. To test the root endpoint (<code>GET /</code>), use the following command:</p>



<pre class="wp-block-code"><code>curl -X GET "http://127.0.0.1:8000"</code></pre>



<p class="wp-block-paragraph">The output should be as follows:</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="800" height="199" src="https://adyouri.com/wp-content/uploads/2024/06/output-1.webp" alt="FastAPI Output" class="wp-image-1822" style="width:788px;height:auto" srcset="https://adyouri.com/wp-content/uploads/2024/06/output-1.webp 800w, https://adyouri.com/wp-content/uploads/2024/06/output-1-300x75.webp 300w, https://adyouri.com/wp-content/uploads/2024/06/output-1-768x191.webp 768w" sizes="(max-width: 800px) 100vw, 800px" /></figure>



<h3 class="wp-block-heading">Test the Item Endpoint (<code>GET /items/{item_id}</code>)</h3>



<p class="wp-block-paragraph">This endpoint should return the <code>item_id</code> and an optional query parameter <code>q</code>.</p>



<p class="wp-block-paragraph">To test it without the query parameter:</p>



<pre class="wp-block-code"><code>curl -X GET "http://127.0.0.1:8000/items/1"</code></pre>



<p class="wp-block-paragraph">This should return something like:</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="800" height="199" src="https://adyouri.com/wp-content/uploads/2024/06/output-2.webp" alt="FastAPI output query parameter" class="wp-image-1823" srcset="https://adyouri.com/wp-content/uploads/2024/06/output-2.webp 800w, https://adyouri.com/wp-content/uploads/2024/06/output-2-300x75.webp 300w, https://adyouri.com/wp-content/uploads/2024/06/output-2-768x191.webp 768w" sizes="auto, (max-width: 800px) 100vw, 800px" /></figure>



<p class="wp-block-paragraph">To test it with the query parameter:</p>



<pre class="wp-block-code"><code>curl -X GET "http://127.0.0.1:8000/items/1?q=test"</code></pre>



<p class="wp-block-paragraph">This should return something like:</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="800" height="179" src="https://adyouri.com/wp-content/uploads/2024/06/output-3.webp" alt="fastapi query params" class="wp-image-1824" srcset="https://adyouri.com/wp-content/uploads/2024/06/output-3.webp 800w, https://adyouri.com/wp-content/uploads/2024/06/output-3-300x67.webp 300w, https://adyouri.com/wp-content/uploads/2024/06/output-3-768x172.webp 768w" sizes="auto, (max-width: 800px) 100vw, 800px" /></figure>



<h2 class="wp-block-heading">FastAPI Features</h2>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="768" height="512" src="https://adyouri.com/wp-content/uploads/2024/06/Features.webp" alt="FastAPI Features" class="wp-image-1825" srcset="https://adyouri.com/wp-content/uploads/2024/06/Features.webp 768w, https://adyouri.com/wp-content/uploads/2024/06/Features-300x200.webp 300w" sizes="auto, (max-width: 768px) 100vw, 768px" /></figure>



<h3 class="wp-block-heading">Automatic Interactive Documentation</h3>



<p class="wp-block-paragraph">FastAPI automatically generates interactive API documentation. By default, you get Swagger UI at <code>http://127.0.0.1:8000/docs</code> and ReDoc at <code>http://127.0.0.1:8000/redoc</code>. This feature is a huge time-saver for both development and collaboration.</p>



<h3 class="wp-block-heading">Data Validation with Pydantic</h3>



<p class="wp-block-paragraph">With FastAPI, you get data validation out of the box. By using Pydantic models, you can ensure the data your API receives is valid. Here’s an example:</p>



<pre class="wp-block-code"><code>from pydantic import BaseModel

class Item(BaseModel):
    name: str
    price: float
    is_offer: bool = None

@app.post("/items/")
def create_item(item: Item):
    return item</code></pre>



<p class="wp-block-paragraph">In this example, FastAPI will validate that the incoming request body matches the <code>Item</code> model.</p>



<h3 class="wp-block-heading">FastAPI Asynchronous Support</h3>



<p class="wp-block-paragraph">FastAPI supports asynchronous request handlers, allowing you to build highly concurrent applications:</p>



<pre class="wp-block-code"><code>@app.get("/async-items/")
async def read_async_items():
    return {"message": "This is an async endpoint"}</code></pre>



<h2 class="wp-block-heading">Conclusion</h2>



<p class="wp-block-paragraph">FastAPI is a game-changer for Python developers looking to build fast, robust, and easy-to-maintain APIs. In my experience, its modern features, automatic documentation, and type safety make it a joy to work with.</p>



<p class="wp-block-paragraph">You can use FastAPI to build a simple <a href="https://adyouri.com/what-is-a-headless-api">headless API</a> or a complex backend system, or even integrate it with the <a href="https://adyouri.com/chatgpt-api-python">ChatGPT API</a> for AI magic! Give it a try, and you’ll see why it’s quickly becoming the go-to framework for many Python developers.</p>



<h2 class="wp-block-heading">FAQ: FastAPI</h2>



<h3 class="wp-block-heading">Why should I use FastAPI?</h3>



<p class="wp-block-paragraph">FastAPI is designed to be easy to use and learn, while also providing high performance and automatic generation of interactive API documentation. It also supports asynchronous programming, which can handle many requests concurrently.</p>



<h3 class="wp-block-heading">What kind of documentation does FastAPI generate?</h3>



<p class="wp-block-paragraph">FastAPI automatically generates interactive API documentation using Swagger UI and ReDoc. These can be accessed at <code>/docs</code> and <code>/redoc</code> endpoints, respectively.</p>



<h3 class="wp-block-heading">Can I use FastAPI with existing Flask or Django projects?</h3>



<p class="wp-block-paragraph">Yes, you can integrate FastAPI with existing Flask or Django projects, although it requires some setup. FastAPI can run as a microservice within a larger application or be used to build new components incrementally.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: adyouri.com @ 2026-08-12 11:23:19 by W3 Total Cache
-->