title: How to add to cart via Api Rest in woocommerce with javascript?
body: <p>How to add to cart via Api Rest in woocommerce with javascript?</p>
  <p>I'm adding this question to be able to help anyone who is looking for the answer with the new versions of Woocommerce Api Rest, after 7 hours I have achieved it and if I can help someone save this time a pleasure.</p>
  <p>IMPORTANT</p>
  <p>I suggest using tools such as postman or insomnia that are in the documentation of the woocommerce rest api that fully helped me to generate the correct code and do the tests.</p>
  <p>Very good here the code with which I can add a simple product via api rest is this.</p>
  <pre><code>fetch(&quot;https://yourdomain.com/wp-json/wc/store/cart/add-item&quot;, {
    &quot;method&quot;: &quot;POST&quot;,
    &quot;headers&quot;: {
      &quot;X-WC-Store-API-Nonce&quot;: &quot;here-your-nonce-woo-not-wp-nonce&quot;, // see how to create the nonce for woo that is not the same as wordpress, it is another
      &quot;Content-Type&quot;: &quot;application/json&quot;
    },
    &quot;body&quot;: &quot;{\&quot;id\&quot;:\&quot;3665\&quot;,\&quot;quantity\&quot;:\&quot;1\&quot;}&quot;
  })
  .then(response =&gt; {
    console.log(response);
  })
  .catch(err =&gt; {
    console.error(err);
  });
  </code></pre>
  <p>To see everything you can do with the cart, execute <a href="https://yourdomain.com/wp-json/wc/store/" rel="nofollow noreferrer">https://yourdomain.com/wp-json/wc/store/</a> and you will see all the possible routes, the most complex thing is perhaps to correctly assemble the so-called post with the correct nonce and others header that requests the documentation.</p>
  <p>For the Add to cart there is little documentation with rest api ajala this help someone and can increase the doc in woocommerce.</p>
  <p>A hug</p>