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
1 | curl -i -H "X-Spree-Token: YOUR_TOKEN_ID" http: //0 .0.0.0:3000 /api/products .json |
Show a product
1 | curl -i -H "X-Spree-Token: YOUR_TOKEN_ID" http: //0 .0.0.0:3000 /api/products/706676762 .json |
Modify a product
1 | 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
1 | curl -i -X DELETE -H "X-Spree-Token: YOUR_TOKEN_ID" http: //0 .0.0.0:3000 /api/products/706676762 .json |
New product
1 | curl -i -X GET -H "X-Spree-Token: YOUR_TOKEN_ID" http: //0 .0.0.0:3000 /api/products/new .json |
Create a product
1 | 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
1 | 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
1 | curl -i -X GET -H "X-Spree-Token: YOUR_TOKEN_ID" http: //0 .0.0.0:3000 /api/variants .json |
New variant
1 | 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*
1 | 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 |
1 | 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 |
1 | 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
1 | 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
1 | 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:
1 2 3 4 5 6 7 8 9 | 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
1 | 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
1 | 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
1 | curl -i -H "X-Spree-Order-Token: THE_ORDER_TOKEN" http: //0 .0.0.0:3000 /api/orders/R764617307 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $.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)
1 | POST /api/orders/ORDERID/line_items ?line_item[variant_id]=1&line_item[quantity]=1&order_token=ORDERTOKEN |
1 2 3 4 5 6 7 | $.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
1 | 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
1 | 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
1 | 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
1 | 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
1 | curl -i -H "X-Spree-Token: YOUR_TOKEN_ID" http: //0 .0.0.0:3000 /api/taxonomies .json |
Show a taxonomy
1 | curl -i -H "X-Spree-Token: YOUR_TOKEN_ID" http: //0 .0.0.0:3000 /api/taxonomies/475199832 .json |
Modify a taxonomy
1 | 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
1 | curl -i -X DELETE -H "X-Spree-Token: YOUR_TOKEN_ID" http: //0 .0.0.0:3000 /api/taxonomies/475199832 .json |
New taxonomy
1 | curl -i -X GET -H "X-Spree-Token: YOUR_TOKEN_ID" http: //0 .0.0.0:3000 /api/taxonomies/new .json |
Create a taxonomy
1 | 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