Index

To return a paginated list of all variants within the store, make this request:

GET /api/variants

You can limit this to showing the variants for a particular product by passing through a product’s permalink:

GET /api/products/ruby-on-rails-tote/variants

or

GET /api/variants?product_id=ruby-on-rails-tote

Parameters

show_deleted
boolean - true to show deleted variants, false to hide them. Default: false. Only available to users with an admin role.
page
The page number of variants to display.
per_page
The number of variants to return per page

Response

Status: 200 OK
{
  "variants": [
    {
      "id": 1,
      "name": "Ruby on Rails Tote",
      "sku": "ROR-00011",
      "price": "15.99",
      "display_price": "$15.99",
      "weight": null,
      "height": null,
      "width": null,
      "depth": null,
      "is_master": true,
      "cost_price": "13.0",
      "permalink": "ruby-on-rails-tote",
      "description": "A text description of the product.",
      "options_text": "(Size: small, Colour: red)",
      "in_stock": true,
      "option_values": [
        {
          "id": 1,
          "name": "Small",
          "presentation": "S",
          "option_type_name": "tshirt-size",
          "option_type_id": 1
        }
      ],
      "images": [
        {
          "id": 1,
          "position": 1,
          "attachment_content_type": "image/jpg",
          "attachment_file_name": "ror_tote.jpeg",
          "type": "Spree::Image",
          "attachment_updated_at": null,
          "attachment_width": 360,
          "attachment_height": 360,
          "alt": null,
          "viewable_type": "Spree::Variant",
          "viewable_id": 1,
          "mini_url": "/spree/products/1/mini/file.png?1370533476",
          "small_url": "/spree/products/1/small/file.png?1370533476",
          "product_url": "/spree/products/1/product/file.png?1370533476",
          "large_url": "/spree/products/1/large/file.png?1370533476"
        }
      ]
    }
  ],
  "count": 25,
  "pages": 5,
  "current_page": 1
}

To search for a particular variant, make a request like this:

GET /api/variants?q[sku_cont]=foo

You can limit this to showing the variants for a particular product by passing through a product id:

GET /api/products/ruby-on-rails-tote/variants?q[sku_cont]=foo

or

GET /api/variants?product_id=ruby-on-rails-tote&q[sku_cont]=foo

The searching API is provided through the Ransack gem which Spree depends on. The sku_cont here is called a predicate, and you can learn more about them by reading about Predicates on the Ransack wiki.

The search results are paginated.

Response

Status: 200 OK
{
  "variants": [
    {
      "id": 1,
      "name": "Ruby on Rails Tote",
      "sku": "ROR-00011",
      "price": "15.99",
      "display_price": "$15.99",
      "weight": null,
      "height": null,
      "width": null,
      "depth": null,
      "is_master": true,
      "cost_price": "13.0",
      "permalink": "ruby-on-rails-tote",
      "description": "A text description of the product.",
      "options_text": "(Size: small, Colour: red)",
      "in_stock": true,
      "option_values": [
        {
          "id": 1,
          "name": "Small",
          "presentation": "S",
          "option_type_name": "tshirt-size",
          "option_type_id": 1
        }
      ],
      "images": [
        {
          "id": 1,
          "position": 1,
          "attachment_content_type": "image/jpg",
          "attachment_file_name": "ror_tote.jpeg",
          "type": "Spree::Image",
          "attachment_updated_at": null,
          "attachment_width": 360,
          "attachment_height": 360,
          "alt": null,
          "viewable_type": "Spree::Variant",
          "viewable_id": 1,
          "mini_url": "/spree/products/1/mini/file.png?1370533476",
          "small_url": "/spree/products/1/small/file.png?1370533476",
          "product_url": "/spree/products/1/product/file.png?1370533476",
          "large_url": "/spree/products/1/large/file.png?1370533476"
        }
      ]
    }
  ],
  "count": 25,
  "pages": 5,
  "current_page": 1
}

Sorting results

Results can be returned in a specific order by specifying which field to sort by when making a request.

GET /api/variants?q[s]=price%20asc

It is also possible to sort results using an associated object’s field.

GET /api/variants?q[s]=product_name%20asc

Show

To view the details for a single variant, make a request using that variant's id, along with the product’s permalink as its product_id:

GET /api/products/ruby-on-rails-tote/variants/1

Or:

GET /api/variants/1?product_id=ruby-on-rails-tote

Successful Response

Status: 200 OK
{
  "id": 1,
  "name": "Ruby on Rails Tote",
  "sku": "ROR-00011",
  "price": "15.99",
  "display_price": "$15.99",
  "weight": null,
  "height": null,
  "width": null,
  "depth": null,
  "is_master": true,
  "cost_price": "13.0",
  "permalink": "ruby-on-rails-tote",
  "description": "A text description of the product.",
  "options_text": "(Size: small, Colour: red)",
  "in_stock": true,
  "option_values": [
    {
      "id": 1,
      "name": "Small",
      "presentation": "S",
      "option_type_name": "tshirt-size",
      "option_type_id": 1
    }
  ],
  "images": [
    {
      "id": 1,
      "position": 1,
      "attachment_content_type": "image/jpg",
      "attachment_file_name": "ror_tote.jpeg",
      "type": "Spree::Image",
      "attachment_updated_at": null,
      "attachment_width": 360,
      "attachment_height": 360,
      "alt": null,
      "viewable_type": "Spree::Variant",
      "viewable_id": 1,
      "mini_url": "/spree/products/1/mini/file.png?1370533476",
      "small_url": "/spree/products/1/small/file.png?1370533476",
      "product_url": "/spree/products/1/product/file.png?1370533476",
      "large_url": "/spree/products/1/large/file.png?1370533476"
    }
  ]
}

Not Found Response

Status: 404 Not Found
{
  "error": "The resource you were looking for could not be found."
}

New

You can learn about the potential attributes (required and non-required) for a variant by making this request:

GET /api/products/ruby-on-rails-tote/variants/new

Response

Status: 200 OK
{
  "attributes": [
    "id",
    "name",
    "count_on_hand",
    "sku",
    "price",
    "weight",
    "height",
    "width",
    "depth",
    "is_master",
    "cost_price",
    "permalink"
  ],
  "required_attributes": [

  ]
}

Create

This action is only accessible by an admin user.

To create a new variant for a product, make this request with the necessary parameters:

POST /api/products/ruby-on-rails-tote/variants

For instance, a request to create a new variant with a SKU of 12345 and a price of 19.99 would look like this::

POST /api/products/ruby-on-rails-tote/variants/?variant[sku]=12345&variant[price]=19.99

Successful response

Status: 201 Created

Failed response

Status: 422 Unprocessable Entity
{
  "error": "Invalid resource. Please fix errors and try again.",
  "errors": {

  }
}

Update

This action is only accessible by an admin user.

To update a variant's details, make this request with the necessary parameters:

PUT /api/products/ruby-on-rails-tote/variants/2

For instance, to update a variant's SKU, send it through like this:

PUT /api/products/ruby-on-rails-tote/variants/2?variant[sku]=12345

Successful response

Status: 201 Created

Failed response

Status: 422 Unprocessable Entity
{
  "error": "Invalid resource. Please fix errors and try again.",
  "errors": {

  }
}

Delete

This action is only accessible by an admin user.

To delete a variant, make this request:

DELETE /api/products/ruby-on-rails-tote/variants/2

This request, much like a typical variant "deletion" through the admin interface, will not actually remove the record from the database. It simply sets the deleted_at field to the current time on the variant.

Status: 204 No Content