# Path Operations

```python
from fastapi import FastAPI

# Defining the FastAPI() function
app = FastAPI()

# You need to use a decorator to "activate" the FastAPI logic
# Otherwise it's just a plain python function
@app.get("/") # This is a http method (GET)

# The function itself
def root():

# In true pythonic fashion, function will return result
    return {"message": "Hello World"}
```

* Decorator:

```python
@app
```

* Method:

```python
.get
```

* Path:

```python
("/") # Root path
```

![](/files/s9K9of3dH0RYeUrCl2lW)

{% hint style="info" %}
Note: If you make changes to the code, pass in the --reload flag to the uvicorn webserver so it restarts automatically

```python
uvicorn main:app --reload
```

Use this only for the development environment
{% endhint %}

Example of New Path Operation:

```python
# New Path Operation
@app.get("/posts")
def get_posts():
    return {"data": "This is your post"}
```

{% hint style="info" %}
The code will go down the list of paths and find the first one that matches the URL pattern

For Example:

```python
@app.get("/")
def root():
    return {"message": "Hello World"}
    
@app.get("/")
def get_posts():
    return {"data": "This is your post"}
```

* Note that both have the same path ("/")
* Once this goes down the list it will find the root function first
* It will return the first message:

```python
{"message": "Hello World"}
```

So what it looks for is as follows:

* Request GET method
* With the URL: "/"
  {% endhint %}

{% hint style="warning" %}
THE ORDER IN WHICH YOU STRUCTURE YOUR ROUTES/PATH OPERATIONS DOES MATTER!

IT WILL DETERMINE HOW YOUR API WORKS
{% endhint %}

### Resources:

* If you don't know which method to use, [click here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) for HTTP Methods


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.arkannis.net/programming/python/frameworks/fastapi/path-operations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
