28 Mar Spree resources and routes for API using CURL or Ajax Calls
Hey there, in this time I found the following resources and how to use it using curl or ajax calls for Spree:
The most of the calls need to use the “X-Spree-Token” you can generate it from admin section eg: http://localhost:3000/admin from user section and is the token you need and also you can use for example the order token, I had problems with the api using that token if you have problems let me know.
Products
index of products 25 first
curl -i -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/products.json
Show a product
curl -i -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/products/706676762.json
Modify a product
curl -i -X PUT -H "X-Spree-Token: YOUR_TOKEN_ID" -d "product[name]=Headphones" http://0.0.0.0:3000/api/products/706676762.json
Delete a product
curl -i -X DELETE -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/products/706676762.json
New product
curl -i -X GET -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/products/new.json
Create a product
curl -i -X POST -H "X-Spree-Token: YOUR_TOKEN_ID" -d "product[name]=Headphefsefones&product[price]=100" http://0.0.0.0:3000/api/products
Search products
curl -i -X GET -H "X-Spree-Token: YOUR_TOKEN_ID" -d "q[name_cont]=Rails" http://0.0.0.0:3000/api/products/search.json
Variants
Index of variants
curl -i -X GET -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/variants.json
New variant
curl -i -X GET -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/products/596960057/variants/new.json
*DOES NOT WORK : create a variant*
curl -i -X POST -H "X-Spree-Token: YOUR_TOKEN_ID" -d "variant[price]=100&product=596960057" http://0.0.0.0:3000/api/variants
curl -i -X POST -H "X-Spree-Token: YOUR_TOKEN_ID" -d "variant[price]=100" http://0.0.0.0:3000/api/products/596960057/variants
curl -i -X POST -H "X-Spree-Token: YOUR_TOKEN_ID" -d "variant[name]=100" http://0.0.0.0:3000/api/products/596960057/variants
Modify a variant
curl -i -X PUT -H "X-Spree-Token: YOUR_TOKEN_ID" -d "variant[price]=100" http://0.0.0.0:3000/api/products/596960057/variants/748173072
Delete a variant
curl -i -X DELETE -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/products/596960057/variants/748173072
All about ORDERS
Maybe before starting you need to know that you need an order token to interact with the orders using the API maybe you can use something like the following in order to get the token and use it in your front end code:
module Spree StoreController.class_eval do def mini_cart @order ||= current_order(create_order_if_necessary: true) render json: { order: @order.number, token: @order.token } end end end
Index of orders
curl -i -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/orders
Show an order using the user admin token
curl -i -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/orders/R764617307
Show an order using the order token
curl -i -H "X-Spree-Order-Token: THE_ORDER_TOKEN" http://0.0.0.0:3000/api/orders/R764617307
$.ajax({ url: Spree.routes.root + '/api/orders/R233541761?order_token=776b983f944b052a', async: false, success: function(data) { console.log(data); } }); OR $.ajax({ url: Spree.routes.root + '/api/orders/'+RevoEyewear.OrderNumber, headers: { 'X-Spree-Token': '4e512ffe96f43bb33b0ffac753ac02bf11a679cc6b66736d' }, success: function(data) { console.log(data); } });
Add a product to an existent order(for example if you use a minicart to add products)
POST /api/orders/ORDERID/line_items?line_item[variant_id]=1&line_item[quantity]=1&order_token=ORDERTOKEN
$.ajax url: "/api/orders/#{MyApp.OrderNumber}/line_items?line_item[variant_id]=VariantIdYouWanttoAdd&line_item[quantity]=quantityIntegerValue&order_token=#{MyApp.OrderToken}" type: 'POST' success: -> alert('Product added successfully to the cart.') error: -> alert('Product is out of stock.')
Create an order
curl -i -X POST -H "X-Spree-Token: YOUR_TOKEN_ID" -d "order[line_items][][variant_id]=748173072&order[line_items[][quantity]=5" http://0.0.0.0:3000/api/orders
Capture payment
curl -i -X PUT -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/orders/R570383062/payments/28/purchase -d ""
Update order status
curl -i -X PUT -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/orders/R243081821/shipments/H48741317988/ship -d ""
Search an order
curl -i -X GET -H "X-Spree-Token: YOUR_TOKEN_ID" -d "q[number_cont]=R243081821" http://0.0.0.0:3000/api/orders/search.json
Taxonomies
Index of taxonomies
curl -i -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/taxonomies.json
Show a taxonomy
curl -i -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/taxonomies/475199832.json
Modify a taxonomy
curl -i -X PUT -H "X-Spree-Token: YOUR_TOKEN_ID" -d "taxonomy[name]=Headphones" http://0.0.0.0:3000/api/taxonomies/854451430.json
Delete a taxonomy
curl -i -X DELETE -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/taxonomies/475199832.json
New taxonomy
curl -i -X GET -H "X-Spree-Token: YOUR_TOKEN_ID" http://0.0.0.0:3000/api/taxonomies/new.json
Create a taxonomy
curl -i -X POST -H "X-Spree-Token: YOUR_TOKEN_ID" -d "taxonomy[name]=Headphefsefones" http://0.0.0.0:3000/api/taxonomies
No Comments