{"id":3267,"library":"sanic-routing","title":"Sanic-Routing","description":"Sanic-Routing is the standalone, high-performance core routing component used by the Sanic web framework. It handles URL parsing, route matching, and parameter extraction, providing a flexible and efficient routing engine. The library maintains an active development pace with frequent updates, typically releasing new versions quarterly.","status":"active","version":"23.12.0","language":"en","source_language":"en","source_url":"https://github.com/sanic-org/sanic-routing/","tags":["routing","web","sanic","http"],"install":[{"cmd":"pip install sanic-routing","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"While Sanic uses this internally, direct import for custom routing components is from `sanic_routing`.","wrong":"from sanic.router import Router","symbol":"Router","correct":"from sanic_routing import Router"}],"quickstart":{"code":"from sanic_routing import Router\nfrom sanic_routing.exceptions import NotFound\n\n# Initialize the router\nrouter = Router()\n\n# Define dummy handlers (in a real app, these would be your view functions)\ndef handler_root(request):\n    pass\n\ndef handler_greet(request, name):\n    pass\n\n# Add routes to the router\nrouter.add(\"/\", handler_root, methods=[\"GET\"])\nrouter.add(\"/greet/<name:str>\", handler_greet, methods=[\"GET\"])\nrouter.add(\"/files/<path:path>\", lambda req, path: None, methods=[\"GET\"])\n\n# Look up a route by URI and method\ntry:\n    # Matching a simple route\n    handler, params, uri, kwargs, route = router.get(\"/greet/Alice\", \"GET\")\n    print(f\"Found handler for /greet/Alice: {handler.__name__}\")\n    print(f\"Parameters: {params}\") # Expected: {'name': 'Alice'}\n\n    # Matching a path parameter\n    handler_path, params_path, uri_path, kwargs_path, route_path = router.get(\"/files/path/to/document.txt\", \"GET\")\n    print(f\"Found handler for /files/path/to/document.txt: {params_path}\") # Expected: {'path': 'path/to/document.txt'}\n\nexcept NotFound as e:\n    print(f\"Could not find route: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n\n# Example of a missing route\ntry:\n    router.get(\"/missing\", \"GET\")\nexcept NotFound as e:\n    print(f\"Attempted to get /missing, caught expected error: {type(e).__name__}\")","lang":"python","description":"This quickstart demonstrates how to initialize the `Router`, add various types of routes (simple, string parameter, path parameter), and then perform route lookups using `router.get()`. It also shows how to handle `NotFound` exceptions for non-existent routes."},"warnings":[{"fix":"Ensure your routes explicitly handle empty string segments if that was desired behavior, or update path definitions to be more specific (e.g., using `/<user_id:uuid>` for UUIDs) and avoid matching empty string segments.","message":"Route parameters defined with `<name:str>` or `<name:path>` types will no longer match empty strings. Previously, a route like `/users/<user_id:str>` might have matched `/users/` resulting in `user_id=''`.","severity":"breaking","affected_versions":">=22.3.0"},{"fix":"Update route definitions to use the new parameter types: change `<foo:string>` to `<foo:str>` and `<foo:number>` to `<foo:float>`.","message":"The route parameter types `<foo:string>` and `<foo:number>` were deprecated. They have been replaced with `<foo:str>` and `<foo:float>` respectively.","severity":"deprecated","affected_versions":">=0.7.0"},{"fix":"Be mindful of how URI parameters are handled and explicitly decode them if necessary, particularly if you rely on parameters always being fully unquoted before processing. For standard string casts via `<param:str>`, unquoting should still occur.","message":"Starting with version 23.6.0, `sanic-routing` will only unquote URI components when casting them to string types. This means that if you're not explicitly casting a parameter, it might retain its URL-encoded state (e.g., `%20` for space).","severity":"gotcha","affected_versions":">=23.6.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}