> For the complete documentation index, see [llms.txt](https://fastapi-docs.duonghuuphuc.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fastapi-docs.duonghuuphuc.com/appendix-d.md).

# Appendix D

## Development Environment Setup and FastAPI Introduction

***

### Objective

By the end of this practice, students will be able to:

* Prepare a Python development environment for FastAPI.
* Understand the basic concepts behind FastAPI.
* Configure an isolated Python virtual environment.
* Apply basic Python Type Hints.
* Understand the role of Pydantic for data validation.
* Understand the basics of asynchronous programming (Concurrency).
* Develop and run a simple FastAPI application.

***

### Prerequisites

* Basic Python programming knowledge.
* Visual Studio Code (or another code editor).
* Internet connection for installing packages.

***

### Activities

#### Task 1. Install Python

* Install [Python](https://www.python.org/) 3.11 or later.
* Verify the installation:

```bash
python --version
```

***

#### Task 2. Create a Virtual Environment

Create and activate a Python virtual environment to isolate project dependencies. Using a virtual environment ensures that packages installed for one project do not interfere with others.

For this course, we recommend one of the following options:

* **Anaconda** (recommended for beginners): Includes **Anaconda Navigator**, a graphical user interface (GUI) for creating and managing virtual environments without using command-line tools.
* **venv** (recommended for this course): Python's built-in command-line tool for creating lightweight virtual environments.
* **virtualenv**: A widely used third-party tool that provides additional features beyond `venv`.
* **virtualenvwrapper**: A collection of utilities for managing multiple virtual environments more efficiently.

For **venv**, you can creates a new **Python virtual environment** named `.venv` in the current directory.

```bash
python -m venv .venv
```

Command for activating it in **Windows**

```bash
.venv\Scripts\activate
```

Command for activating it in **macOS/Linux**

```bash
source .venv/bin/activate
```

***

#### Task 3. Install FastAPI

Install FastAPI and Uvicorn.

```bash
pip install fastapi uvicorn
```

Verify the installation.

```bash
pip list
```

***

#### Task 4. Python Type Hints

Read the following concepts in [Unit 1](/unit-1-python-type-hint.md):

* Basic data types
* Function parameter annotations
* Return type annotations
* Optional and Union types
* List, Dict, and Tuple annotations

***

#### Task 5. Pydantic

Read how Pydantic in [Unit 2](/unit-2-pydantic.md):

* Defines request models
* Validates input data
* Converts JSON into Python objects
* Generates API documentation automatically

***

#### Task 6. Review Concurrency

Understand the difference between:

* Synchronous (`def`)
* Asynchronous (`async def`)

Read [Unit 3](/unit-3-concurrency.md) to know why FastAPI uses asynchronous programming to improve performance.

***

#### Task 7. Run Your First FastAPI Application

Create a file named `main.py`.

```python
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello FastAPI"}
```

Start the server.

```bash
uvicorn main:app --reload
```

Open your browser:

* `http://127.0.0.1:8000`
* `http://127.0.0.1:8000/docs`

Verify that:

* The API returns the expected JSON response.
* The interactive Swagger UI is accessible.

***

### Submission

Students must submit in the e-Learning system:

1. A screenshot of the running FastAPI application.
2. A screenshot of the Swagger UI (`/docs`).
3. The `main.py` source code.
