Why Basic URI Design Isn't Enough for Production APIs
If you've ever built a simple REST API, you probably know the basics: GET /bikes to list bikes, POST /bikes to create one, GET /bikes/{id} to fetch a single record. These fundamentals are essential, but real-world applications rarely stay this simple for long.
Once your data model grows to include relationships—brands that own bikes, bikes that have spare parts, users who leave reviews—your URI design needs to evolve too. This is where many developers stumble. They either flatten everything into a single resource collection, or they overcomplicate their routes with inconsistent, hard-to-predict patterns.
In this deep-dive, we'll go beyond the basics and explore advanced URI design techniques that experienced API architects use to build clean, scalable, and maintainable endpoints—the kind of production-grade patterns you'd expect in a well-architected FastAPI application.
Modeling Real-World Relationships with Nested Resources
Consider a showroom application with three core entities: brands, bikes, and spare parts. A brand owns many bikes, and each bike has many spare parts. How should your URIs reflect this hierarchy?
The Parent-Child Pattern
The most common and intuitive approach is to nest child resources under their parent:
GET /brands/{brandId}/bikes— list all bikes belonging to a specific brandGET /brands/{brandId}/bikes/{bikeId}— retrieve a specific bike within that brandGET /brands/{brandId}/bikes/{bikeId}/spares— list all spare parts for that bike
This pattern makes the relationship between resources immediately obvious just by reading the URI. Anyone consuming your API can understand the data hierarchy without needing to dig through documentation.
Avoiding Excessive Nesting
While nesting is powerful, it's easy to overdo it. A URI like /brands/{brandId}/bikes/{bikeId}/spares/{spareId}/reviews/{reviewId}/comments becomes unwieldy and fragile. As a general rule of thumb, limit nesting to two or three levels deep. Beyond that, consider promoting the deeply nested resource to a top-level collection with a reference back to its parent, such as:
GET /comments?reviewId={reviewId}
This keeps your routing logic manageable while still preserving the relational context through query parameters.
Handling Complex Relationships Between Resources
Not all relationships are strictly hierarchical. Sometimes resources relate to each other in ways that don't fit a simple parent-child model. Here are some patterns for handling these cases gracefully.
Many-to-Many Relationships
Suppose bikes can belong to multiple categories, and categories can contain multiple bikes. Rather than forcing an artificial hierarchy, you can expose an association resource:
GET /bikes/{bikeId}/categoriesPOST /bikes/{bikeId}/categories— associate a category with a bikeDELETE /bikes/{bikeId}/categories/{categoryId}— remove the association
This treats the relationship itself as a manageable resource, which aligns well with REST principles and keeps your API predictable.
Referencing Resources Without Full Nesting
Sometimes it's cleaner to keep resources flat and use query parameters to express relationships instead of deep nesting. For example:
GET /spares?bikeId={bikeId}&brandId={brandId}
This approach shines when a resource can be filtered by multiple, independent parent relationships—something that's awkward to express through nested paths alone.
Filtering, Sorting, and Pagination: Query Parameters Done Right
Advanced URI design isn't just about hierarchy—it's also about giving API consumers flexible ways to query collections without creating a new endpoint for every use case.
Filtering
Use query parameters to let clients narrow down results:
GET /bikes?brand=Yamaha&color=redGET /spares?minPrice=10&maxPrice=100
Sorting
Allow consumers to control ordering with a dedicated parameter:
GET /bikes?sort=price&order=asc
Pagination
For large collections, always paginate rather than returning everything at once:
GET /bikes?page=2&limit=20
Combining these patterns gives you a single, powerful endpoint instead of dozens of specialized ones—an approach that scales far better as your FastAPI backend grows.
Understanding API Maturity: The Richardson Maturity Model
To evaluate how "RESTful" your API truly is, it helps to understand the Richardson Maturity Model, a framework that breaks REST adoption into four levels:
- Level 0: A single URI and a single HTTP verb (usually POST) handle everything. This is essentially RPC over HTTP, not REST.
- Level 1: Multiple URIs represent different resources, but only one HTTP method is used to interact with all of them.
- Level 2: Multiple URIs combined with proper HTTP verbs (GET, POST, PUT, DELETE) — this is where most well-designed production APIs live.
- Level 3: Adds HATEOAS (Hypermedia as the Engine of Application State), where responses include links guiding clients to related actions and resources dynamically.
Most production APIs comfortably operate at Level 2, and that's perfectly acceptable for the vast majority of applications. Understanding where your API sits on this scale helps you make informed architectural decisions rather than chasing theoretical purity.
A Quick Note on HATEOAS
While full HATEOAS adoption is rare in practice, understanding the concept sharpens your API design instincts. The core idea is simple: instead of clients hardcoding every possible endpoint, the API response itself includes hypermedia links describing available next actions. For example, a bike resource might include a link to its spare parts collection directly in the response body, allowing clients to navigate the API dynamically rather than relying on out-of-band documentation.
Best Practices Checklist for Complex API Routing
- Keep nesting shallow — two to three levels maximum
- Use plural nouns for collections (
/bikes, not/bike) - Favor query parameters for filtering, sorting, and pagination over creating new endpoints
- Model many-to-many relationships as their own association resources
- Stay consistent across your entire API surface—naming conventions matter
- Evaluate your API's maturity level periodically as it evolves
Bringing It All Together in FastAPI
These patterns aren't just theoretical—they translate directly into how you structure routers and path operations in FastAPI. By organizing your route files around resource hierarchies and using dependency injection for shared logic like pagination or filtering, you can build an API that stays clean and maintainable even as your data model grows more complex.
This is exactly the kind of architectural thinking covered in depth in the Full Stack Python Architect: FastAPI + React + Vibe Coding course, where resource-based URI design is explored through hands-on examples using brands, bikes, and spare parts—building the intuition you need to architect real-world, production-grade APIs.
Featured Video
[Video embed placeholder: RESTful API - More About Resource Based URIs]
Conclusion: Key Takeaways
Designing resource-based URIs for complex APIs is as much an art as it is a science. The goal is always clarity and predictability—your endpoints should tell a story about your data model without requiring extensive documentation to decipher.
Remember these core principles: nest resources to reflect genuine parent-child relationships but avoid excessive depth, use query parameters for filtering and pagination rather than proliferating endpoints, model many-to-many relationships as first-class associations, and periodically evaluate your API against the Richardson Maturity Model to ensure it stays truly RESTful.
Mastering these advanced patterns will set your APIs apart—making them easier for other developers to consume, easier for your team to maintain, and ready to scale as your application grows.