Phoenix router macros for mounting UCP and ACP routes.
Usage
defmodule MyAppWeb.Router do
use Phoenix.Router
use Bazaar.Phoenix.Router
pipeline :api do
plug :accepts, ["json"]
plug Bazaar.Plugs.UCP
end
# Optional: Add schema validation
pipeline :bazaar_validated do
plug Bazaar.Plugs.ValidateRequest
plug Bazaar.Plugs.ValidateResponse
end
scope "/" do
pipe_through [:api, :bazaar_validated]
# UCP protocol (default)
bazaar_routes "/", MyApp.Commerce.Handler
# ACP protocol
bazaar_routes "/acp", MyApp.Commerce.Handler, protocol: :acp
end
endSchema Validation
Bazaar includes plugs for validating requests and responses against Smelter-generated Ecto schemas:
Bazaar.Plugs.ValidateRequest- Validates incoming request bodiesBazaar.Plugs.ValidateResponse- Validates outgoing response bodies
Both plugs are optional but recommended for catching schema violations early in development.
Generated Routes (UCP)
| Method | Path | Description |
|---|---|---|
| GET | /.well-known/ucp | Discovery endpoint |
| POST | /checkout-sessions | Create checkout |
| GET | /checkout-sessions/:id | Get checkout |
| PUT | /checkout-sessions/:id | Update checkout |
| POST | /checkout-sessions/:id/complete | Complete checkout |
| POST | /checkout-sessions/:id/cancel | Cancel checkout |
| GET | /orders/:id | Get order |
| PUT | /orders/:id | Update order (with order_updates: true) |
| POST | /orders/:id/actions/cancel | Cancel order |
| GET | /products | List products |
| GET | /products/search | Search products |
| GET | /products/:id | Get product |
| POST | /webhooks/ucp | Webhook endpoint |
Generated Routes (ACP)
ACP uses slightly different URL patterns and HTTP methods:
| Method | Path | Description |
|---|---|---|
| POST | /checkout_sessions | Create checkout |
| GET | /checkout_sessions/:id | Get checkout |
| POST | /checkout_sessions/:id | Update checkout |
| POST | /checkout_sessions/:id/complete | Complete checkout |
| POST | /checkout_sessions/:id/cancel | Cancel checkout |
Note: ACP does not have a discovery endpoint.
Options
bazaar_routes "/api/v1", MyApp.Handler,
protocol: :ucp, # Protocol: :ucp (default) or :acp
only: [:checkout, :orders], # Limit capabilities
discovery: true, # Include discovery endpoint (UCP only)
webhooks: true, # Include webhook endpoint
order_updates: false, # Mount PUT /orders/:id (beyond the spec's REST binding)
validate_requests: true, # Enable request validation (requires plug in pipeline)
validate_responses: true # Enable response validation (requires plug in pipeline)
Summary
Functions
Mounts protocol routes at the given path using the specified handler.
Functions
Mounts protocol routes at the given path using the specified handler.
Examples
# UCP protocol (default)
bazaar_routes "/", MyApp.Handler
# ACP protocol
bazaar_routes "/acp", MyApp.Handler, protocol: :acp
# Multiple protocols at different paths
bazaar_routes "/ucp", MyApp.Handler, protocol: :ucp
bazaar_routes "/acp", MyApp.Handler, protocol: :acp