charlie.robbins
asked this on December 14, 2010 04:32 am
Is there a way to create a product from the API? I can't seem to find it on this page:
http://docs.chargify.com/api-products
This is really integral to the project that I am working on and I really hope that it is possible.
Thanks,
Charlie
Comments
Hey,
This is possible but not documented. You can see examples of this in our Ruby wrapper that is on GitHub.
I plan to use this feature in my application so I have to know if there is a reason why it isn't documented yet. For instance, is it because there is a chance you will stop supporting it at some point in the future? Or I can count on it being available?
Thanks
Its safe to use and we'll get it documented
Hey, I wasn't sure when this was going to be added in, or documented so I forked the gem, and added it in.
https://github.com/jwoertink/chargify_api_ares
I started to write a test, but got some weird FakeWeb errors, so I'll mess with that later, but I've added a sample. So for anyone looking to add a product via API, can take a look through my version of the gem for now :)
It would be awesome to see this document, need this for PHP - If it is possible to add and delete products via the API, then a function can be created for a one time purchase of any variable amount for non-subscribers by creating a subscription and product and deleting both after the transaction is complete. Need this feature now, going to play around to see if I can figure this method out.
Documentation of this would just be helpful
I have just spent a bit of time doing this and here is my documentation for what it is worth:
Creating Products using json examples
You can test this in Firefox using Firebug and jQuery:
1. navigate to the site you want to post to: https://<your_api_key>:<password>@<your_subdomain>...
2. from the response pick the product_family_id you want to target, you will need this for step 4.
3. Open firebug and load jquery by executing this line:
var s=document.createElement("script");s.src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";document.head.appendChild(s);
4. using the firebug console in multiline mode, execute the following and but place the product_family_id from step 2 in to the url below and execute:
var product = "{\"product\":{\"name\":\"test\",\"return_params\":\"subscription_id={subscription_id}\",\"handle\":\"test\",\"price_in_cents
\":100,\"trial_interval\":14,\"expiration_interval_unit\":\"never\",\"expiration_interval\":null,\"accounting_code\":\"\",
\"initial_charge_in_cents\":null,\"require_credit_card\":false,\"trial_price_in_cents\":0,\"interval\":1,\"request_credit_card\":false,
\"description\":\"\",\"archived_at\":null,\"trial_interval_unit\":\"day\",\"interval_unit\":\"month\"}}";
$.ajax({
url:'/product_families/##insert product family id from step 3 above##/products.json',
dataType:'json',
type: "POST",
data: product,
contentType: "application/json",
success: function(d){
console.log(d);
}
});
Notes:
**You must post to the products collection of the product family not just the /products endpoint. ie: POST /product_families/{product_family_id}/products.json
**You do not need to post the product_family_id or the product_family object in the post data as it is ignored and infered from the URL.
**You cannot post a json array of products :( , you must post separate json product entities.
If you do post a json array of objects you get the following error:
"NetworkError: 422 Unprocessable Entity - https://xxxxx.chargify.com/product_families/####/products.json"
{"errors":["Name: cannot be blank.","Interval unit: is not one of the allowed values.","Recurring Interval: is not a number.","Price: cannot be
blank. Enter '0' if free."]}
**Product taxability is not returned with the product so you will have to manually update this in the UI if needed.
You can use these methods to fairly easily push products from one chargify site to another, by GETing them, switching subdomains and POSTing them to the new site.
Hope that helps