DEV Community

Fiyinfoluwa Ojo
Fiyinfoluwa Ojo

Posted on

Routing & Controllers: Handling Dynamic Data in URLs

What are Dynamic Routes?

Static routes like /about always return the same thing.
Dynamic routes change their response based on what's in the URL.

This is how real APIs work — /user/1and /user/2
return different users, not the same response.

My 3 Dynamic Routes for Day 4

1. User by ID

@app.get("/user/{user_id}")
def get_user(user_id: int):
    return {
        "user_id": user_id,
        "message": f"Fetching user with ID {user_id}"
    }
Enter fullscreen mode Exit fullscreen mode

2. Product by Name

@app.get("/product/{product_name}")
def get_product(product_name: str):
    return {
        "product": product_name,
        "message": f"Fetching product: {product_name}"
    }

Enter fullscreen mode Exit fullscreen mode

3. Nested Parameters — User + Order

@app.get("/user/{user_id}/order/{order_id}")
def get_user_order(user_id: int, order_id: int):
    return {
        "user_id": user_id,
        "order_id": order_id,
        "message": f"Order {order_id} for user {user_id}"
    }

Enter fullscreen mode Exit fullscreen mode

Why Nested Parameters Matter

/user/1/order/42 tells you exactly what resource
you're dealing with — user 1's order number 42.
This is how e-commerce APIs, banking systems,
and social platforms structure their routes.

Here's the live response from all 3 dynamic routes:

Dynamic routes response

Dynamic routes response

Dynamic routes response

Lessons Learned

Dynamic routing is what makes APIs actually useful.
Without it, you'd need a separate endpoint for
every single user, product, or resource — which is impossible.

Day 4 done. 26 more to go.

GDGoCBowen30dayChallenge

Top comments (0)