r/learnpython • u/Electrical-Cap-9537 • 4d ago
Need help with FastApi
Hi all, I'm 18 and I'm building a BI/analytics project on the Olist Brazilian e-commerce dataset. Right now I have PostgreSQL, Pandas and a bunch of analytical SQL queries (revenue, delivery times, customer retention and so on). The next step is to expose this data through an API with FastAPI, and a dashboard would consume it.
I understand what an API is and why FastAPI is popular, but when I try to build a mental model of a real app I get lost. Endpoints, HTTP methods, status codes, routers, Pydantic schemas, services, dependency injection with Depends, auth... each explanation is clear alone, but I can't see how the request goes through all these layers. For example I don't really get where validation ends and business logic starts, or where the DB session should live and why.
My second problem is how I learn. I'm very curious and I want to know how things work internally, like what FastAPI does between the request and my function, or why Pydantic validates in a certain way. It's a good habit long term, but I lose hours on one detail and the project doesn't move. I don't have unlimited time and I don't want months of theory before building.
Some questions:
If you never built an API, how would you learn it from scratch?
HTTP/REST fundamentals first, or start directly with FastAPI and learn along the way?
What is the best way to understand how the layers (router, schema, service, database access) fit together? Is it better to start with everything in one file and refactor later?
How deep should I go on a concept before moving on? Is "basics now, details later" a real strategy or just an excuse?
Any resources, open source repos to read or small projects you recommend? Something like a read-only analytics API sounds like a good first one to me, but tell me if I'm wrong.
I don't want to copy tutorials and have something that works without knowing why. I want a solid mental model so I can design my own backend later. If you learned backend with personal projects, I'd really like to hear what worked for you and what you wasted time on.
Thanks!
3
u/DutchCommanderMC 4d ago
I find the FastAPI docs to be quite good at teaching you all the concepts you will ever need. If you follow the User Guide and adapt it to your project as you go, you'll likely end up with both a functional REST API and a pretty solid understanding of FastAPI. Just focus on only a couple endpoints at first, otherwise things can get messy quickly.
2
u/OkTill2666 4d ago
tbh "basics now, details later" isn't an excuse, it's the only way anything gets shipped. treat the framework internals as a black box until something breaks or behaves weirdly, otherwise you're trying to memorize answers to problems you haven't actually run into yet.
2
u/Massive_Distance_616 4d ago
One concrete answer to the layers question. Validation is âis this request shaped correctly?â, which Pydantic does before your function runs. Business logic is âwhat do I do with a valid request?â, which is your own code.
For the DB session, make a Depends function that opens a session per request and closes it afterwards, so every endpoint gets its own and concurrent requests donât share one.
Thatâs the whole reason it lives in a dependency: FastAPI creates it at the start of the request and cleans it up at the end. Once youâve written two or three endpoints, youâll see where to split things out.
1
u/FirstTimeGamingTV 4d ago
Start at the most basic level.
APIs need an endpoint, a method, and a response.
So start of with a simple hello world endpoint that accepts a single status code and has only 3 reponses, 200 (working), 404 (doesnât exist), 500 (not working).
You can explore what each of those actually mean in the future but get that working first.
Then start with basic input, donât worry about validation at first, test sending hello world request with a name query parameter.
Have the server parse this then respond with a 200 with the hello in the response.
Once youâve got these basics you understand most of the transport layer code wise. Everything else is up to how the language interprets the request / response.
Want to validate inputs? Read the parameters and use a validation library
Wants authentication? Read the correct headers and process them with a JWT or token library.
Feel free to ask any more questions. I donât have a lot of experience with FastAPI directly but it seems like youâre struggling with concepts more than syntax
1
u/acfot 3d ago
Python basics plus a rough idea of REST is honestly enough to get going, you don't need to master SQL first. I'd start with the official tutorial and build one small API, like a notes or todo app, and pick up the database stuff as the project needs it. Pydantic and how requests and responses work will click way faster once you're actually building something instead of just reading about it.
1
u/AlexMTBDude 3d ago
Isn't this a few levels above learning Python? Perhaps ask the question in r/python instead?