1.2.4 • Published 4 years ago

magento2-client-nodejs v1.2.4

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

magento2-client-nodejs

Easily access Magento2 API

Install with npm

npm install magento2-client-nodejs

Usage

Parameters

You can create a magento2-client-nodejs with these parameters:

ParameterData TypeDefinition
baseUrlStringThis is the URL of the front end of the Magento installation. Example: 'https://www.example.com'. This can optionally contain a base path like: 'https://www.example.com/rel-5.3.2'
usernameStringThis is the username to login to the Magento2 admin console.
passwordStringThis is the password for the supplied Magento2 admin username.
tokenStringThis is the tokem for the supplied Magento2 to access based on token.
optionsJSON ObjectOptional Contains any optional connection parameters.

Options

These options are allowed in the options object parameter

NameData TypeDefaultDefinition
versionStringV1Magento API version to use when getting the authorization token.
rejectUnauthorizedbooleantrueIf set to false, self signed or bad SSL certificates will be allowed

Example, create magento-client

const Magento = require('magento2-client-nodejs');

var magento = new Magento('https://www.example.com', 'username', 'password', {version: "v1", rejectUnauthorized: true});
const Magento = require('magento2-client-nodejs');

var magento = Magento.withToken('https://www.example.com', 'access token', {version: "v1", rejectUnauthorized: true});

Magento API documentation: http://devdocs.magento.com/guides/v2.0/rest/list.html.

To use the magento client created above to make requests, use the request interface:

magento.request(method, url, urlParams, data, callback);

where the parameters are defined as follows:

ParameterData TypeDefinition
methodStringHTTP method to use, 'POST', 'GET', 'PUT', 'DELETE'
urlStringThe REST endpoint. Example: '/V1/products'.
urlParamsJSON ObjectKey value pairs representing URL parameters. Example: { 'searchCriteria[pageSize]': 10, 'searchCriteria[currentPage]': 1 }
dataJSON ObjectThe body to send to the request. Example: {"attributeSet":{"attribute_set_name":"Pants","entity_type_id":4},"skeletonId":4}
callbackfunctionThe callback function to trigger after completing the request. Follows the standard (err, data) model

Examples, call request method

With callback:

magento.request('GET',                      //method
                '/V1/categories',           //url
                { searchCriteria: "\'\'" }, //urlParams
                {},                         //data
                function(err, data) {       //callback
  if (err) {
    // TODO handle errors
  }
  console.log('categories = ' + JSON.stringify(data, null, 2));
});

magento.request('GET',                      //method
                '/V1/orders',               //url
                {                           //urlParams
                  'searchCriteria[pageSize]': 10,
                  'searchCriteria[currentPage]': 1
                },
                {},                         //data
                function(err, data) {       //callback
  if (err) {
    // TODO handle errors
  }
  console.log('orders = ' + JSON.stringify(data, null, 2));
});

With promises:

magento.request('GET',                    //method
                '/V1/orders',             //url
                {                         //urlParams
                  'searchCriteria[pageSize]': 10,
                  'searchCriteria[currentPage]': 1
                },
                {})                       //data
  .then((data) => {
    console.log('orders = ' + JSON.stringify(data, null, 2));
  })
  .catch((err) => {
    // TODO handle errors
    console.log(err);
  });

magento.request('GET',                    //method
                '/V1/products',           //url
                {                         //urlParams
                  'searchCriteria[pageSize]': 10,
                  'searchCriteria[currentPage]': 1
                },
                {})                       //data
  .then((data) => {
    console.log('products = ' + JSON.stringify(data, null, 2));
  })
  .catch((err) => {
    // TODO handle errors
  });

Notes:

  1. A real Magento install should use https protocol. This module will work with an http site with a warning... Sometimes, you just need to test things out...
  2. An error with a message 'unable to verify the first certificate' indicates that the server is using a self signed certificate. Setting the rejectUnauthorized option to false will bypass this and display a warning, but this should only be used in test environments.
var magento = new Magento("https://www.example.com", "username", "password", {"rejectUnauthorized": false});
  1. There is a DEFAULT_VERSION set at 'V1'. This is used to get the authorization token. If you need to overide this, you can set it in the options of the new parameters. For example, if you want to use 'V2', you could do this. This is only for getting the Authorization token with the username and password. All the other routes are sent in the url parameter of the request method.
var magento = new Magento("https://www.example.com", "username", "password", {"options": "V2"});
  1. For convenience, there is a getBaseUrl function:
let baseUrl = magento.getBaseUrl();