šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

magento-api-rest

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magento-api-rest

A NodeJS wrapper to work with Magento REST APIs.

2.0.4
latest
Source
npm
Version published
Weekly downloads
379
-11.24%
Maintainers
1
Weekly downloads
Ā 
Created
Source

Magento API REST

Build Status dependencies Status npm version

A Node.js client wrapper to work with the Magento REST API.

Installation

npm i magento-api-rest

Getting started

Generate API credentials by following these instructions.

Make sure to check the resource access is as per your requirements to prevent misuse of the API Keys.

Check out the Magento API endpoints and data that can be manipulated in these docs.

Setup

Setup for the Magento REST API integration

ECMAScript Module Example

import Magento from "magento-api-rest";

const client = new Magento({
    'url': 'https://magento.dev',
    'consumerKey': '<OAuth 1.0a consumer key>',
    'consumerSecret': '<OAuth 1.0a consumer secret>',
    'accessToken': '<OAuth 1.0a access token>',
    'tokenSecret': '<OAuth 1.0a access token secret>',
});

CommonJS Module Example

const Magento = require('magento-api-rest').default;

const client = new Magento({
    'url': 'https://magento.dev',
    'consumerKey': '<OAuth 1.0a consumer key>',
    'consumerSecret': '<OAuth 1.0a consumer secret>',
    'accessToken': '<OAuth 1.0a access token>',
    'tokenSecret': '<OAuth 1.0a access token secret>',
});

Options

OptionTypeRequiredDescription
urlStringyesYour Store URL
consumerKeyStringyesYour API consumer key
consumerSecretStringyesYour API consumer secret
accessTokenStringyesYour API Access Token
tokenSecretStringyesYour API Access Token Secret
typeStringnoMagento endpoint type, default is 'V1'
shaNumbernoMagento SHA type, default is '1'
timeoutNumbernoRequest Timeout
axiosConfigObjectnoReference

If you want to use the Asynchronous Endpoints set type to async/V1.

If you want to use the Bulk Endpoints set type to async/bulk/V1.

If you want to change the sha version, values can be 1 or 256.

Methods

GET

.get(endpoint) .get(endpoint, params)

ParamsTypeDescription
endpointStringMagento API endpoint, example: orders
paramsObjectJSON object to be sent as params.

POST

.post(endpoint, data)

ParamsTypeDescription
endpointStringMagento API endpoint, example: shipments
dataObjectJSON object to be sent as body.

PUT

.put(endpoint, data)

ParamsTypeDescription
endpointStringMagento API endpoint, example: shipments/12
dataObjectJSON object to be sent as body.

DELETE

.delete(endpoint, data)

ParamsTypeDescription
endpointStringMagento API endpoint, example: orders/12
dataObjectJSON object to be sent as body.

API

Requests are made with Axios library with support to promises.

let params = {
     "filter_groups": [
        {
            "filters": [
                {
                    "field": "created_at",
                    "value": "2019-08-03 11:22:47",
                    "condition_type": "from"
                }
            ]
        },
        {
            "filters": [
                {
                    "field": "created_at",
                    "value": "2020-08-03 11:22:47",
                    "condition_type": "to"
                }
            ]
        }
    ],
    "sort_orders": [
        {
            "field": "created_at",
            "direction": "desc"
        }
    ],
    "page_size": 200,
    "current_page": 1
}

Or, you can use the parser to write the above query as:

let params = {
    $from: "2019-08-03 11:22:47",
    $to: "2020-08-03 11:22:47",
    $sort: {
        "created_at": "desc"
    },
    $perPage: 200,
    $page: 1
}

You cannot use both the param writing styles together. Parser is triggered automatically if you use any of the keys.

Parser Operators

OperatorDescriptionNotes
$orExecute OR queries.Syntax: $or:[ { condition1 }, { condition2 }]
$fromStarting point of search via ISO date.Requires $to.
$toEnding point of search via ISO date.Requires $from.
$afterSearch after a specific ISO date.Exclusive.
$beforeSearch before a specific ISO date.Exclusive.
$sortSort the data.
$perPageSpecifies the per page data.
$pageSpecifies the current page.

By default { key: value } translates to an "eq" operation where key = value.

To get more information as to how to form search queries, use the following reference.

If you want to use the above object in a request,

async function getOrders () {
    try {
        let { data } = await client.get('orders', params);
        // Response Handling
    } catch (err) {
        // Error Handling
    }
}

Error Handling is same as how Axios handles it.

Keywords

Magento

FAQs

Package last updated on 11 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts