<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Murali </title>
    <description>The latest articles on DEV Community by Murali  (@mmurali).</description>
    <link>https://dev.to/mmurali</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1178508%2Fc39b6249-005f-4564-8d18-acaf3dcbb739.png</url>
      <title>DEV Community: Murali </title>
      <link>https://dev.to/mmurali</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mmurali"/>
    <language>en</language>
    <item>
      <title>Day 2: Understanding Pydantic Models and Data Validation in FastAPI</title>
      <dc:creator>Murali </dc:creator>
      <pubDate>Sun, 06 Dec 2026 12:53:51 +0000</pubDate>
      <link>https://dev.to/mmurali/day-2-understanding-pydantic-models-and-data-validation-in-fastapi-k6o</link>
      <guid>https://dev.to/mmurali/day-2-understanding-pydantic-models-and-data-validation-in-fastapi-k6o</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;On Day 1, I created my first FastAPI application and learned how to run it locally.&lt;/p&gt;

&lt;p&gt;Today, I focused on an important concept in API development: &lt;strong&gt;how to define, validate, and manage the data received by an API&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When an application receives information from a user or another application, we cannot simply assume that the data is correct. It may contain missing fields, incorrect values, or unexpected data types.&lt;/p&gt;

&lt;p&gt;This is where Pydantic becomes useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Understanding Python Type Hints
&lt;/h2&gt;

&lt;p&gt;Python is a dynamically typed language. We can assign different types of values to variables.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;incident_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1001&lt;/span&gt;
&lt;span class="n"&gt;severity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"CRITICAL"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python allows us to write type hints to communicate what type of value we expect.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;incident_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1001&lt;/span&gt;
&lt;span class="n"&gt;severity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"CRITICAL"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;int&lt;/code&gt; indicates an integer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;str&lt;/code&gt; indicates a string.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;My learning:&lt;/strong&gt; Type hints improve code readability and help developers understand the expected data types. However, type hints alone do not automatically validate incoming API data at runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. What Is Pydantic?
&lt;/h2&gt;

&lt;p&gt;Pydantic is a Python library used to validate and manage data based on defined models.&lt;/p&gt;

&lt;p&gt;FastAPI uses Pydantic to validate incoming request data.&lt;/p&gt;

&lt;p&gt;For example, imagine that an incident management application receives information about a production issue.&lt;/p&gt;

&lt;p&gt;We may expect the following information:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Title of the incident&lt;/li&gt;
&lt;li&gt;Description of the issue&lt;/li&gt;
&lt;li&gt;Severity&lt;/li&gt;
&lt;li&gt;Other required details&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of manually checking every field, we can define a Pydantic model.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Creating an IncidentCreate Model
&lt;/h2&gt;

&lt;p&gt;I created an &lt;code&gt;IncidentCreate&lt;/code&gt; model to represent the information required when a user submits a new incident.&lt;/p&gt;

&lt;p&gt;The model defines the expected structure of the incoming data.&lt;/p&gt;

&lt;p&gt;For example, a simplified Pydantic model looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pydantic&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BaseModel&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;IncidentCreate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;severity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the application has a clear definition of the expected input.&lt;/p&gt;

&lt;p&gt;If the incoming data does not match the model requirements, Pydantic can report a validation error.&lt;/p&gt;

&lt;p&gt;In the actual exercise, I also explored field constraints and restricting severity values to an allowed set using &lt;code&gt;Literal&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Understanding Optional Values and Field Constraints
&lt;/h2&gt;

&lt;p&gt;Not every field in an application needs to be mandatory.&lt;/p&gt;

&lt;p&gt;Python's optional type annotations help describe values that may be absent or &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;

&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pydantic also provides &lt;code&gt;Field&lt;/code&gt; to define additional rules for fields, such as minimum or maximum length.&lt;/p&gt;

&lt;p&gt;This helps us define data requirements more clearly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My learning:&lt;/strong&gt; Validation should happen at the application boundary. It is better to identify invalid input early than allow incorrect data to travel through the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Why Do We Need Separate Request and Response Models?
&lt;/h2&gt;

&lt;p&gt;One important concept I learned today is that the data received by an API and the data returned by an API do not always have the same structure.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Request model – IncidentCreate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The user provides information such as the incident title, description, and severity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Response model – IncidentResponse&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The application can return additional information, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generated incident ID&lt;/li&gt;
&lt;li&gt;Incident status&lt;/li&gt;
&lt;li&gt;Creation timestamp&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The user does not need to provide these system-generated values.&lt;/p&gt;

&lt;p&gt;This is why I created two separate models:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;IncidentCreate&lt;/code&gt; – defines incoming data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;IncidentResponse&lt;/code&gt; – defines outgoing data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keeping them separate makes the API structure clearer and easier to maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Understanding model_dump()
&lt;/h2&gt;

&lt;p&gt;Pydantic provides a useful method called &lt;code&gt;model_dump()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It converts a Pydantic model into a Python dictionary.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;incident_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;incident_input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes, we need to pass the dictionary values into another model.&lt;/p&gt;

&lt;p&gt;Python's dictionary unpacking operator &lt;code&gt;**&lt;/code&gt; makes this convenient.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;IncidentResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;incident_input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"OPEN"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;model_dump()&lt;/code&gt; converts the input model into a dictionary.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;**&lt;/code&gt; unpacks the dictionary values.&lt;/li&gt;
&lt;li&gt;Additional fields are supplied when creating the response.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is useful when building API responses from validated input data.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. My Practical Exercise and Result
&lt;/h2&gt;

&lt;p&gt;I created and tested the models in my local project.&lt;/p&gt;

&lt;p&gt;The exercise helped me understand how to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define a Pydantic model.&lt;/li&gt;
&lt;li&gt;Validate incoming incident information.&lt;/li&gt;
&lt;li&gt;Separate request and response structures.&lt;/li&gt;
&lt;li&gt;Convert a model into a dictionary using &lt;code&gt;model_dump()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create a response model using dictionary unpacking.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My test successfully created an incident response with the following example values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ID: 1001
Severity: CRITICAL
Status: OPEN
Timestamp: UTC timestamp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This confirmed that the input data could be used to construct a response containing additional application-generated information.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Important Lessons from Day 2
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;What I understood&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Type hints&lt;/td&gt;
&lt;td&gt;Communicate expected data types&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pydantic&lt;/td&gt;
&lt;td&gt;Validates and manages structured data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BaseModel&lt;/td&gt;
&lt;td&gt;Base class for creating Pydantic models&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Field&lt;/td&gt;
&lt;td&gt;Adds constraints to model fields&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Optional values&lt;/td&gt;
&lt;td&gt;Allow a field to be absent or &lt;code&gt;None&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Literal&lt;/td&gt;
&lt;td&gt;Restricts a field to specified values&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Request model&lt;/td&gt;
&lt;td&gt;Defines the data received by an API&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Response model&lt;/td&gt;
&lt;td&gt;Defines the data returned by an API&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;model_dump()&lt;/td&gt;
&lt;td&gt;Converts a Pydantic model into a dictionary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;**&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Unpacks dictionary values into function or model arguments&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  9. Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Today I understood that API development is not just about creating endpoints.&lt;/p&gt;

&lt;p&gt;A good API should also define what data it accepts, validate that data, and return a consistent response.&lt;/p&gt;

&lt;p&gt;Pydantic makes this process easier by allowing us to define data structures and validation rules using Python classes.&lt;/p&gt;

&lt;p&gt;I also learned why separating request and response models is useful when developing maintainable applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My key takeaway from Day 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;An API should not blindly trust incoming data. It should validate the data before using it.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What Comes Next?
&lt;/h2&gt;

&lt;p&gt;In Day 3, I will continue developing the Incident Management API and work on accepting incident creation requests through an actual API endpoint.&lt;/p&gt;

&lt;p&gt;The goal is to connect the models I created today with the FastAPI application.&lt;/p&gt;




</description>
      <category>api</category>
      <category>backend</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Day 01: Building My First FastAPI Application</title>
      <dc:creator>Murali </dc:creator>
      <pubDate>Sun, 06 Dec 2026 12:46:32 +0000</pubDate>
      <link>https://dev.to/mmurali/day-01-building-my-first-fastapi-application-36d3</link>
      <guid>https://dev.to/mmurali/day-01-building-my-first-fastapi-application-36d3</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Every software application starts with a development environment.&lt;/p&gt;

&lt;p&gt;As part of my journey towards becoming a Cloud + AI Application Engineer, I decided to strengthen my Python development skills and explore modern API development using FastAPI.&lt;/p&gt;

&lt;p&gt;Although I have been working in IT for several years, my experience has primarily been with Visual Basic, C#, .NET Framework, .NET Core and AWS-hosted applications.&lt;/p&gt;

&lt;p&gt;Now, I'm expanding my skills towards Python, cloud engineering and AI application development.&lt;/p&gt;

&lt;p&gt;In this first lesson, I focused on setting up a Python development environment and building a simple REST API using FastAPI.&lt;/p&gt;

&lt;p&gt;Let's explore what I learned.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Setting Up the Development Environment
&lt;/h2&gt;

&lt;p&gt;I use the following tools for my development:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Operating System: Windows 11&lt;/li&gt;
&lt;li&gt;Code Editor: Visual Studio Code&lt;/li&gt;
&lt;li&gt;Programming Language: Python&lt;/li&gt;
&lt;li&gt;Version Control: Git&lt;/li&gt;
&lt;li&gt;Framework: FastAPI&lt;/li&gt;
&lt;li&gt;ASGI Server: Uvicorn&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why do we need these tools?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Python&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python is the programming language used to develop our application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Visual Studio Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;VS Code is a lightweight code editor that helps us write, organize, execute and debug application code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Git&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Git is a version control system. It helps developers track code changes and maintain different versions of their projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FastAPI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;FastAPI is a Python framework used to develop APIs quickly and efficiently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Uvicorn&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Uvicorn is an ASGI server that runs our FastAPI application and handles incoming HTTP requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Creating a Python Virtual Environment
&lt;/h2&gt;

&lt;p&gt;One important concept I learned is the Python virtual environment.&lt;/p&gt;

&lt;p&gt;Imagine working on two different Python applications.&lt;/p&gt;

&lt;p&gt;Application A requires one version of a library, while Application B requires another version.&lt;/p&gt;

&lt;p&gt;Installing all dependencies globally can create conflicts between applications.&lt;/p&gt;

&lt;p&gt;A virtual environment solves this problem by maintaining separate Python packages for each project.&lt;/p&gt;

&lt;p&gt;I created a virtual environment using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;venv&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;venv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To activate it in Windows PowerShell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;venv&lt;/span&gt;\&lt;span class="nc"&gt;Scripts&lt;/span&gt;\&lt;span class="nc"&gt;Activate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ps1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After activation, Python packages installed in this environment are isolated from other projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is this important?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Avoids dependency conflicts.&lt;/li&gt;
&lt;li&gt;Keeps project dependencies separate.&lt;/li&gt;
&lt;li&gt;Makes application setup easier for other developers.&lt;/li&gt;
&lt;li&gt;Helps maintain consistent development environments.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Installing FastAPI and Uvicorn
&lt;/h2&gt;

&lt;p&gt;After activating the virtual environment, I installed the required packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="n"&gt;uvicorn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;FastAPI provides the framework for developing APIs, while Uvicorn runs the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Understanding Our First FastAPI Application
&lt;/h2&gt;

&lt;p&gt;I created a Python file named &lt;code&gt;main.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The initial implementation was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;root&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although this application contains only a few lines, each statement has an important purpose.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the code
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Import FastAPI&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This imports the FastAPI class from the FastAPI library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Create a FastAPI instance&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates an instance of the FastAPI application.&lt;/p&gt;

&lt;p&gt;The variable &lt;code&gt;app&lt;/code&gt; represents our application and is used to register API endpoints.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Define an API endpoint&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a decorator that registers a GET endpoint for the root URL &lt;code&gt;/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When a client sends an HTTP GET request to this URL, FastAPI executes the associated function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Return a response&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;root&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function returns a Python dictionary.&lt;/p&gt;

&lt;p&gt;FastAPI automatically converts this dictionary into a JSON response.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Running the Application Using Uvicorn
&lt;/h2&gt;

&lt;p&gt;Now that the application is ready, we need a server to run it.&lt;/p&gt;

&lt;p&gt;I executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;uvicorn&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;reload&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand the command.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;uvicorn&lt;/code&gt;: Starts the ASGI server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;main&lt;/code&gt;: Refers to the Python file &lt;code&gt;main.py&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app&lt;/code&gt;: Refers to the FastAPI instance created inside that file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--reload&lt;/code&gt;: Automatically reloads the application when source code changes during development.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After starting the server, I accessed:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http://127.0.0.1:8000/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The browser displayed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hello World"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It worked!&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Exploring Automatic API Documentation
&lt;/h2&gt;

&lt;p&gt;One interesting feature of FastAPI is automatic API documentation.&lt;/p&gt;

&lt;p&gt;I opened:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http://127.0.0.1:8000/docs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;FastAPI displayed an interactive Swagger UI.&lt;/p&gt;

&lt;p&gt;Using this interface, developers can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explore available API endpoints.&lt;/li&gt;
&lt;li&gt;Understand HTTP methods.&lt;/li&gt;
&lt;li&gt;Execute API requests.&lt;/li&gt;
&lt;li&gt;Inspect response bodies and status codes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is particularly useful during API development and testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Creating a Health Check Endpoint
&lt;/h2&gt;

&lt;p&gt;Next, I added another endpoint to check whether the application is running properly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/health"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;health_check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"healthy"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I accessed:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http://127.0.0.1:8000/health&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The application returned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"healthy"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why do we need a health check endpoint?
&lt;/h3&gt;

&lt;p&gt;A health check endpoint provides a simple way to verify that an application is responding.&lt;/p&gt;

&lt;p&gt;It can be useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application monitoring.&lt;/li&gt;
&lt;li&gt;Deployment verification.&lt;/li&gt;
&lt;li&gt;Infrastructure health checks.&lt;/li&gt;
&lt;li&gt;Automated monitoring systems.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, a basic health endpoint only confirms that the endpoint responds. It does not necessarily verify database connectivity or other external dependencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Managing Project Dependencies
&lt;/h2&gt;

&lt;p&gt;Another important practice is maintaining a &lt;code&gt;requirements.txt&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;I generated it using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;freeze&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;requirements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file contains the installed Python package dependencies and their versions.&lt;/p&gt;

&lt;p&gt;Another developer can install the listed dependencies using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="n"&gt;requirements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes project setup easier and reduces dependency-related issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Using Git for Version Control
&lt;/h2&gt;

&lt;p&gt;I also initialized Git in my project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I created a &lt;code&gt;.gitignore&lt;/code&gt; file to prevent unnecessary or sensitive files from being committed.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.venv/
__pycache__/
.env
*.pyc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why exclude &lt;code&gt;.venv&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Because other developers can recreate the virtual environment using the project's dependency file.&lt;/p&gt;

&lt;p&gt;There is no need to commit all installed packages and virtual environment files into the source code repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. What I Learned Today
&lt;/h2&gt;

&lt;p&gt;Although this was a simple application, I learned several fundamental concepts.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to create and activate a Python virtual environment.&lt;/li&gt;
&lt;li&gt;How to install Python dependencies.&lt;/li&gt;
&lt;li&gt;What FastAPI is and how to create an application instance.&lt;/li&gt;
&lt;li&gt;How HTTP GET endpoints work.&lt;/li&gt;
&lt;li&gt;What Uvicorn does.&lt;/li&gt;
&lt;li&gt;How automatic API documentation works.&lt;/li&gt;
&lt;li&gt;How to create a basic health check endpoint.&lt;/li&gt;
&lt;li&gt;Why &lt;code&gt;requirements.txt&lt;/code&gt; is important.&lt;/li&gt;
&lt;li&gt;How Git and &lt;code&gt;.gitignore&lt;/code&gt; help maintain a project.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Coming from a .NET development background, I found several concepts familiar, but implementing them using Python and FastAPI helped me understand a different approach to API development.&lt;/p&gt;

&lt;p&gt;The most important takeaway from Day 1 is that building a software application is not just about writing code.&lt;/p&gt;

&lt;p&gt;A good development environment, dependency management, version control and understanding the underlying framework are equally important.&lt;/p&gt;

&lt;p&gt;This is just the beginning of my journey.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next: Day 2 – Understanding Python application structure, data models and request validation.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  CloudEngineering #Python #FastAPI #SoftwareEngineering
&lt;/h1&gt;

</description>
      <category>api</category>
      <category>backend</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
