MagentoApiClient is a third party Magento API client.
Add to project's package.json:
npm install @rantalainen/magento-api-client
const { MagentoApiClient } = require('@rantalainen/magento-api-client');
import { MagentoApiClient } from '@rantalainen/magento-api-client';
const magento = new MagentoApiClient({
// Required options:
url: 'https://example.test',
accessToken: 'access_token',
// Optional options (and default values):
consumerKey: undefined,
consumerSecret: undefined,
accessTokenSecret: undefined,
apiVersion: 'V1',
timeout: 120000
});
The following API methods have been implemented:
customers
invoices
orders
products
stockItems
Retrieves customers which match a specified criteria. This call returns an array of objects, but detailed information about each object’s attributes might not be included.
required:
searchCriteria.filter_groups
Specifies filters
optional:
searchCriteria.sort_orders
Specifies sortingsearchCriteria.page_size
Specifies the maximum number of items to return. If not specified, returns all matches.searchCriteria.current_page
Specifies the page to return.
// Gets customers updated after 2022-12-01, sorted by email in ascending order
const customers = await magento.customers.get({
filter_groups: [
{
filters: [
{
field: 'updated_at',
value: '2022-12-01',
condition_type: 'gt'
}
]
}
],
sort_orders: [
{
field: 'email',
direction: 'ASC'
}
]
});
console.log(customers);
Retrieves all customers. This call returns an array of objects, but detailed information about each object’s attributes might not be included.
// Gets all customers, sorted by customer id in ascending order
const customers = await magento.customers.getAll();
console.log(customers);
Gets customer by customer ID.
required:
customerId
Customer ID
// Gets customer 123
const customer = await magento.customers.getById(123);
console.log(customer);
Lists invoices that match specified search criteria. This call returns an array of objects, but detailed information about each object’s attributes might not be included.
required:
searchCriteria.filter_groups
Specifies filters
optional:
searchCriteria.sort_orders
Specifies sortingsearchCriteria.page_size
Specifies the maximum number of items to return. If not specified, returns all matches.searchCriteria.current_page
Specifies the page to return.
// Gets max 2 invoices created after 2022-12-08
const invoices = await magento.invoices.get({
filter_groups: [
{
filters: [
{
field: 'created_at',
value: '2022-12-08',
condition_type: 'gt'
}
]
}
],
page_size: 2
});
console.log(invoices);
Lists all invoices. This call returns an array of objects, but detailed information about each object’s attributes might not be included.
// Gets all invoices, sorted by order id in ascending order
const invoices = await magento.invoices.getAll();
console.log(invoices);
Gets invoice by invoice ID.
required:
invoiceId
Invoice ID
// Gets invoice 123
const invoice = await magento.invoices.getById(123);
console.log(invoice);
Lists orders that match specified search criteria. This call returns an array of objects, but detailed information about each object’s attributes might not be included.
required:
searchCriteria.filter_groups
Specifies filters
optional:
searchCriteria.sort_orders
Specifies sortingsearchCriteria.page_size
Specifies the maximum number of items to return. If not specified, returns all matches.searchCriteria.current_page
Specifies the page to return.
// Gets orders created after 2022-12-01
const orders = await magento.orders.get({
filter_groups: [
{
filters: [
{
field: 'created_at',
value: '2022-12-01',
condition_type: 'gt'
}
]
}
]
});
console.log(orders);
Loads a specified order.
// Gets order 123
const order = await magento.orders.getById(123);
console.log(order);
Lists products that match specified search criteria.
required:
searchCriteria.filter_groups
Specifies filters
optional:
searchCriteria.sort_orders
Specifies sortingsearchCriteria.page_size
Specifies the maximum number of items to return. If not specified, returns all matches.searchCriteria.current_page
Specifies the page to return.
// Gets products created after 2022-12-01
const products = await magento.products.get({
filter_groups: [
{
filters: [
{
field: 'created_at',
value: '2022-12-01',
condition_type: 'gt'
}
]
}
]
});
console.log(products);
Lists all products.
// Gets all products, sorted by product id in ascending order
const products = await magento.products.getAll();
console.log(products);
Gets info about product by product SKU.
required:
productSku
Product SKU
// Gets product A123
const product = await magento.products.getBySku('A123');
console.log(product);
magento.products.putStockItem(itemId: string, productSku: string, stockItem: IStockItem): Promise<number>
Updates stock item.
required:
itemId
Item IDproductSku
Product SKUstockItem
Stock item
Gets stock item by product SKU.
required:
productSku
Product SKU
// Gets stock item for product A123
const stockItem = await magento.stockItems.getBySku('A123');
console.log(stockItem);
- Magento REST documentation: https://developer.adobe.com/commerce/webapi/rest/
- 0.0.1 First release
- 0.0.2 Added support for
orders
andstockItems
methods - 0.0.3 Minor fixes in
putStockItem
function