That's perfectly fine, but in many use cases we would want to use <ahref="https://fastapi.tiangolo.com/tutorial/dependencies/"class="external-link"target="_blank">FastAPI Dependencies</a>, for example to **verify** that the client is **logged in** and get the **current user** before executing any other code in the *path operation*.
These dependencies are also very useful during **testing**, because we can **easily replace them**, and then, for example, use a new database for our tests, or put some data before the tests, etc.
So, let's refactor these sessions to use **FastAPI Dependencies**.
## Create a **FastAPI** Dependency
A **FastAPI** dependency is very simple, it's just a function that returns a value.
It could use `yield` instead of `return`, and in that case **FastAPI** will make sure it executes all the code **after** the `yield`, once it is done with the request.
Now let's make FastAPI execute a dependency and get its value in the *path operation*.
We import `Depends()` from `fastapi`. Then we use it in the *path operation function* in a **parameter**, the same way we declared parameters to get JSON bodies, path parameters, etc.
Here's a tip about that `*,` thing in the parameters.
Here we are passing the parameter `session` that has a "default value" of `Depends(get_session)` before the parameter `hero`, that doesn't have any default value.
Python would normally complain about that, but we can use the initial "parameter" `*,` to mark all the rest of the parameters as "keyword only", which solves the problem.
You can read more about it in the FastAPI documentation <ahref="https://fastapi.tiangolo.com/tutorial/path-params-numeric-validations/#order-the-parameters-as-you-need-tricks"class="external-link"target="_blank">Path Parameters and Numeric Validations - Order the parameters as you need, tricks</a>
The value of a dependency will **only be used for one request**, FastAPI will call it right before calling your code and will give you the value from that dependency.
If it had `yield`, then it will continue the rest of the execution once you are done sending the response. In the case of the **session**, it will finish the cleanup code from the `with` block, closing the session, etc.
Then FastAPI will call it again for the **next request**.
Because it is called **once per request**, we will still get a **single session per request** as we should, so we are still fine with that. ✅
And because dependencies can use `yield`, FastAPI will make sure to run the code **after** the `yield` once it is done, including all the **cleanup code** at the end of the `with` block. So we are also fine with that. ✅
## The `with` Block
This means that in the main code of the *path operation function*, it will work equivalently to the previous version with the explicit `with` block.
In fact, you could think that all that block of code inside of the `create_hero()` function is still inside a `with` block for the **session**, because this is more or less what's happening behind the scenes.
But now, the `with` block is not explicitly in the function, but in the dependency above:
You just learned how to use **FastAPI dependencies** to handle the database session. This will come in handy later when testing the code.
And you will see how much these dependencies can help the more you work with FastAPI, to handle **permissions**, **authentication**, resources like database **sessions**, etc. 🚀
If you want to learn more about dependencies, checkout the <ahref="https://fastapi.tiangolo.com/tutorial/dependencies/"class="external-link"target="_blank">FastAPI docs about Dependencies</a>.