Documentation

Routes

Updated
Browse documentation

Published 2026-08-13. V2 is a prerelease; the web core is release-gated and other previews are named separately. What beta means.

Read this page as HTML, Markdown, or structured JSON—or open the published Markdown with an AI assistant. Gemini receives the prompt through your clipboard because its signed-out page does not reliably prefill URL text; paste when the new tab opens. External assistants need the public site URL.

Routes

Define routes inside Amber::Server.configure and attach each group to a named pipeline.

File: config/routes.cr — add these declarations inside the generated Amber::Server.configure block. Keep the existing :static routes.

Crystal
Amber::Server.configure do
  routes :web do
    get "/posts", PostsController, :index
    get "/posts/:id", PostsController, :show
    post "/posts", PostsController, :create
    patch "/posts/:id", PostsController, :update
    delete "/posts/:id", PostsController, :destroy
  end
end

Dynamic segments such as :id are available through params in the action. Amber also supports put, options, head, trace, and connect route macros.

Resource routes

resources creates conventional routes for index, new, create, show, edit, update, and destroy:

File: config/routes.cr — use these entries inside an existing Amber::Server.configure block, as an alternative to listing every route.

Crystal
routes :web do
  resources "/posts", PostsController
  resources "/profiles", ProfilesController, only: [:show, :edit, :update]
  resources "/events", EventsController, except: [:destroy]
end

Only declare actions implemented by the controller; missing resource actions fail during compilation.

Scopes and namespaces

A scope on routes prefixes the complete group. Nested namespace blocks add another path segment.

File: config/routes.cr — add this route group inside Amber::Server.configure.

Crystal
routes :api, "/api" do
  namespace "/v1" do
    resources "/posts", Api::PostsController, only: [:index, :show]
  end
end

Segment constraints

Constrain a dynamic segment with a regular expression when a route must reject non-matching values.

File: config/routes.cr — add the constrained route inside the existing :web route group.

Crystal
routes :web do
  get "/orders/:id", OrdersController, :show, {"id" => /\d+/}
end

Run amber routes from the project root to print the declared route table. Pair that inspection with request specs and the compiler to verify dispatch behavior.