Customizing JSON Responses
Spree uses Alba serializers to build API v3 responses. You can replace any serializer via Spree Dependencies.Adding Custom Attributes
Let’s say you want to add amy_custom_attribute column to the Product API response.
Create a custom serializer that inherits from the core one:
app/serializers/my_app/product_serializer.rb
config/initializers/spree.rb:
Adding an Association
Let’s say you’ve created aSpree::Brand model that belongs to Product (see the tutorial for the full example).
Create a serializer for the new model:
app/serializers/spree/api/v3/brand_serializer.rb
app/serializers/my_app/product_serializer.rb
config/initializers/spree.rb:
?expand=brand:
Serializer Dependency Keys
Here are the most commonly customized v3 serializers:| Key | Default |
|---|---|
product_serializer | Spree::Api::V3::ProductSerializer |
variant_serializer | Spree::Api::V3::VariantSerializer |
cart_serializer | Spree::Api::V3::CartSerializer |
order_serializer | Spree::Api::V3::OrderSerializer |
line_item_serializer | Spree::Api::V3::LineItemSerializer |
category_serializer | Spree::Api::V3::CategorySerializer |
customer_serializer | Spree::Api::V3::CustomerSerializer |
address_serializer | Spree::Api::V3::AddressSerializer |
payment_serializer | Spree::Api::V3::PaymentSerializer |
fulfillment_serializer | Spree::Api::V3::FulfillmentSerializer |
Spree::Api::ApiDependencies for the full list.
Adding New Endpoints
Controller
Inherit fromSpree::Api::V3::Store::ResourceController for Store API endpoints:
app/controllers/spree/api/v3/store/brands_controller.rb
ResourceController provides:
Override these methods to customize:
| Method | Purpose |
|---|---|
model_class | ActiveRecord model (required) |
serializer_class | Alba serializer (required) |
scope | Base query — add .where() to filter |
find_resource | Custom ID lookup (slug fallback, etc.) |
permitted_params | Custom strong params |
collection_includes | Eager loading for index |
Routes
Add routes in your app’sconfig/routes.rb:
Permitted Attributes
For endpoints that accept writes (create/update), add your attributes:config/initializers/spree.rb
permitted_params in the controller:

