Routing¶
Verified status as of March 28, 2026. Runtime note: FastFN auto-installs function-local dependencies from
requirements.txt/package.json; host runtimes are required infastfn dev --native, whilefastfn devdepends on a running Docker daemon. FastFN uses file-system routing, similar to Next.js.
The file structure of your project determines the public URL paths.
Visualizing File-Based Routing¶
Instead of maintaining a massive routes.py file, FastFN uses your folder structure to define your API.
functions/
├── users/
│ ├── index.py # -> GET /users
│ └── [id].py # -> GET /users/:id
│ └── _shared.py # private helper
└── settings.js # -> GET /settings
Basic Routing¶
| File Path | URL Path |
|---|---|
users/index.py |
/users |
settings/profile.js |
/settings/profile |
Dynamic Segments¶
You can use brackets [] to create dynamic path parameters.
| File Path | URL Path | Example Match |
|---|---|---|
users/[id].py |
/users/:id |
/users/42 |
posts/[category]/[slug].py |
/posts/:category/:slug |
/posts/tech/fastfn-intro |
Accessing Parameters (Direct Injection)¶
FastFN automatically injects route params as direct function arguments. Just declare the param name in your handler signature:
How injection works per runtime
| Runtime | Mechanism | Signature |
|---|---|---|
| Python | inspect.signature kwargs |
def handler(event, id): |
| Node.js | Second arg when handler.length > 1 |
async (event, { id }) => |
| PHP | ReflectionFunction second arg |
function handler($event, $params) |
| Lua | Always passed as second arg | function handler(event, params) |
| Go | Params merged into event map | event["id"].(string) |
| Rust | Params merged into event value | event["id"].as_str() |
Backward compatible
Existing handler(event) signatures work unchanged. Params are only injected when the handler declares them.
Multiple Parameters¶
For nested segments like posts/[category]/[slug]/get.py:
Catch-All Wildcards¶
For files/[...path]/get.py, the entire remaining path is captured:
Route Precedence¶
If you have overlapping routes, FastFN follows strict precedence:
- Static Routes:
users/settings.py(Specific) - Dynamic Routes:
users/[id].py(General) - Catch-all Routes:
users/[...slug].py(Most General)
Most Specific Wins
FastFN applies a deterministic "most specific wins" ordering, so a catch-all route cannot steal a more specific match.
Private helpers and mixed folders¶
You can import local modules without publishing them as endpoints.
- In a pure file tree, prefix private helpers with
_: users/_shared.pyusers/_shared.js- In a single-entry folder, sibling modules stay private by default:
whatsapp/handler.jsis the public functionwhatsapp/core.jsis private implementation code- Subdirectories under a single-entry folder can still expose explicit routes:
whatsapp/admin/index.js->/whatsapp/adminwhatsapp/admin/get.health.js->GET /whatsapp/admin/healthwhatsapp/admin/helpers.jsstays private
This gives you a Lambda-style entrypoint directory without losing Next.js-style nested routes.
HTTP Methods¶
By default, a route is GET unless you opt into another method.
To restrict methods or handle them differently:
Option 1: Logic inside handler¶
Option 2: fn.config.json¶
Add a config file next to your handler:
users/[id]/fn.config.json:
Now POST /users/42 will automatically return 405 Method Not Allowed.
Option 3: Method-Specific Files¶
Instead of branching on event.method inside one handler, create separate files:
users/
get.py # handles GET /users
post.py # handles POST /users
[id]/
get.py # handles GET /users/:id
put.py # handles PUT /users/:id
delete.py # handles DELETE /users/:id
Each file only handles one method. FastFN infers the method from the filename prefix. See Zero-Config Routing for the full details.
Flow Diagram¶
flowchart LR
A["Client request"] --> B["Route discovery"]
B --> C["Policy and method validation"]
C --> D["Runtime handler execution"]
D --> E["HTTP response + OpenAPI parity"]
Objective¶
Clear scope, expected outcome, and who should use this page.
Prerequisites¶
- FastFN CLI available
- Runtime dependencies by mode verified (Docker for
fastfn dev, OpenResty+runtimes forfastfn dev --native)
Validation Checklist¶
- Command examples execute with expected status codes
- Routes appear in OpenAPI where applicable
- References at the end are reachable
Troubleshooting¶
- If runtime is down, verify host dependencies and health endpoint
- If routes are missing, re-run discovery and check folder layout