Example: Magento API from Node.js with Axios

I feel like every time I post, the answer is simple, but the documentation is terrible. The task: Call Magento API from a JavaScript program in Node.js.

  • Login to your Magento Admin account at https://%5Byour-domain%5D/admin
  • Create an integration: System >> Integrations >> Add New Intgration
  • Copy the Access Token for use later in the header of your get request

Here is the code in a format you can cut and paste.

const axios = require('axios').default;

axios({
    method: 'get',
    url: 'https://[My URL Here]/rest/default/V1/products' +
            '?searchCriteria[currentPage]=1&searchCriteria[pageSize]=5',
    responseType: 'text/json',
    timeout: 1000,
    headers: {'authorization': 'Bearer [My Access Token Here]'}
  })
    .then(function (response) {
      console.log(JSON.stringify(response.data));
    })
    .catch(function(err) {
        console.log(err.message);
    })
    .finally(
        function() {
            console.log('finally');
        }
    )

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s