Sub topic 5
Designing routes and path parameters
This chapter assumes you know HTTP methods and status codes, and that you have written a basic Lambda handler. You should also know that an HTTP API sends requests to integrations such as Lambda.
What a route is
A route is a method plus a path, written as a single string called the route key. GET /items/{id} is one route. POST /items is another. When a request arrives, API Gateway matches it to a route, then calls the integration attached to that route. If nothing matches, API Gateway returns 404 Not Found and your function is never invoked.
Path parameters
Curly braces mark a variable segment. In GET /items/{id}, a request for /items/42 gives your function pathParameters of {"id": "42"}. Values always arrive as strings, so convert them yourself if you need a number.
A greedy parameter ends in +, as in /files/{proxy+}. It matches the rest of the path, including slashes. /files/a/b/c.txt produces {"proxy": "a/b/c.txt"}. A greedy parameter must be the last segment of the path.
The $default route
The special route key $default catches every request that no other route matches. It is useful for a single function that does its own routing, or for a fallback that returns a custom 404. Specific routes always win over $default, and more specific paths win over greedy ones. For example, /items/{id} beats /items/{proxy+} for /items/42.
Routing in the handler
With payload format 2.0, the matched route key is in event["requestContext"]["routeKey"]. This lets one function serve several routes:
import json
def respond(status, body):
return {
"statusCode": status,
"headers": {"content-type": "application/json"},
"body": json.dumps(body),
}
def handler(event, context):
route = event["requestContext"]["routeKey"]
if route == "GET /items/{id}":
item_id = event["pathParameters"]["id"]
return respond(200, {"id": item_id})
if route == "POST /items":
return respond(201, {"created": True})
return respond(404, {"message": "Not found"})Compare against the route key, not the raw path. The route key contains the template ({id}), so it stays the same whatever value the client sends.
One function or many?
You can attach every route to one function, or give each route its own. The trade-offs are:
| Approach | Advantages | Drawbacks |
|---|---|---|
| One function per route | Small packages, separate permissions and metrics, independent deploys | More resources to manage |
| One function for many routes | Fewer resources, one warm container serves everything | Broader permissions, larger package, routing code to maintain |
A common middle path is to group routes by resource, such as one function for /items and /items/{id}.
Design guidelines
- Use nouns for paths and let the method carry the action:
GET /items, not/getItems. - Keep path parameters for identifying a resource. Use query strings for filtering and paging.
- Avoid overlapping greedy routes unless you need them, because the matching rules are harder to reason about.
Exercise
A request for GET /files/reports/2026/q1.pdf arrives at an API with the routes GET /files/{name} and GET /files/{proxy+}. Which route matches, and what does pathParameters contain?
Show answer
GET /files/{name} cannot match because {name} covers only one segment and the path has three after /files/. The greedy route GET /files/{proxy+} matches, and pathParameters is {"proxy": "reports/2026/q1.pdf"}.