# Set Up Python fast API

To start a project with FastAPI in Python, follow these steps:

### 1\. Install Python & Dependencies

Make sure you have Python 3.7 or later installed. You can check your Python version with:

```css
python --version
```

2. ## Create a virtual environment:
    
3. ```css
    python -m venv .venv
    ```
    
4. Activate the virtual environment:
    
    * On Windows:
        
        ```css
        .venv\Scripts\activate
        ```
        
    * On macOS/Linux:
        
        ```css
        source .venv/bin/activate
        ```
        

install them using `pip`:

```css
pip install fastapi uvicorn
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1744204220219/da68308b-4db4-4e91-896a-2193ce18cc19.png align="center")

### 2\. Create main.py

```python
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, FastAPI!"}
```

### 4\. Run the FastAPI Application

Now, you can run the FastAPI app using `uvicorn`. From your project directory, run:

```css
uvicorn main:app --reload
```

* `main`: This refers to the Python file [`main.py`](http://main.py) (without the `.py` extension).
    
* `app`: This is the FastAPI app instance.
    
* `--reload`: This flag enables auto-reloading during development, so the server restarts when you make changes to your code.
    

You should see output similar to this:

```css
INFO:     Will watch for changes in these directories: ['<your_project_directory>']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1744204271185/1bac3b2b-8245-4c00-a9c0-fb70fcbd6118.png align="center")

### 5\. Access the FastAPI App

Open a browser and go to:

```css
http://127.0.0.1:8000
```

You should see a JSON response:

```css
{
  "message": "Hello, FastAPI!"
}
```

### 6\. Explore the Documentation

FastAPI automatically generates interactive documentation for your API. To view it:

* **Swagger UI**: Go to [`http://127.0.0.1:8000/docs`](http://127.0.0.1:8000/docs)
    
* **ReDoc**: Go to [`http://127.0.0.1:8000/redoc`](http://127.0.0.1:8000/redoc)
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733500425036/772b9bba-9376-4154-9bb2-3dccc3dcb74a.png align="center")
    

### 7\. Add More Routes

You can add more endpoints (routes) to your app by defining more functions with FastAPI decorators. For example:

```css
from fastapi import FastAPI

app = FastAPI()

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

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

In this case, the endpoint `/items/{item_id}` will accept an `item_id` as a URL parameter, and an optional query parameter `q`.

### 8\. Test Your API

You can test the new `/items/{item_id}` endpoint by navigating to:

```css
http://127.0.0.1:8000/items/42?q=test
```

This should return:

```css
{
  "item_id": 42,
  "q": "test"
}
```

### 10\. Optional: Create a `requirements.txt`

To share the project with others, you can generate a `requirements.txt` file that lists all your dependencies:

```css
pip freeze > requirements.txt
```

Others can install the dependencies by running:

```css
pip install -r requirements.txt
```
