Magento code cheatsheet
First, we need to make a POST request to the Magento API to obtain an access token for authorization:
const axios = require('axios');
// Set up the Axios config
const config = {
method: 'post',
url: 'https://your-magento-store-url.com/rest/V1/integration/admin/token',
headers: {
'Content-Type': 'application/json'
},
data: {
username: '<your-username>',
password: '<your-password>'
}
};
// Make the POST request using Axios
Once we get a response from the API, we're logging the access token to the console. This access token can then be used in subsequent requests to the Magento API in order to authorize those requests. Note that this access token is only valid for a limited time and needs to be regenerated periodically.
After we got the access token, it’s time to query the category for available products:
const axios = require('axios');
// Set up the Axios config
const config = {
method: 'post',
url: 'https://your-magento-store-url.com/rest/V1/products',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer <your-access-token>'
},
data: {
searchCriteria: {
filter_groups: [
{
filters: [
{
field: 'category_id',
value: '<your-category-id>',
condition_type: 'eq'
}
]
}
]
}
}
};
// Make the POST request using Axios
axios(config)
.then((response) => {
console.log(response.data.items);
})
.catch((error) => {
console.log(error);
});
In this example, we're making a POST request to the /rest/V1/products endpoint of the Magento API. We're passing in the Authorization header with a valid access token, and we're sending a JSON payload in the data field that specifies the search criteria for the products we want to list. In this case, we're filtering by category ID. Once we get a response from the API, we're logging the items array to the console. This array will contain the products that match the search criteria we specified.
After we found our product, it was time to add products to the cart. If we don't already have a cart ID, here is an example of how to create one:
const axios = require('axios');
// Set up the Axios config
const config = {
method: 'post',
url: 'https://your-magento-store-url.com/rest/V1/guest-carts',
headers: {
'Content-Type': 'application/json'
}
};
// Make the POST request using Axios
axios(config)
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.log(error);
});
After we successfully created the cart, we can finally add the product to the cart:
const axios = require('axios');
// Set up the Axios config
const config = {
method: 'post',
url: 'https://your-magento-store-url.com/rest/V1/carts/mine/items',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer <your-access-token>'
},
data: {
"cartItem": {
"sku": "<your-product-sku>",
"qty": <quantity>,
"quote_id": "<your-quote-id>"
}
}
};
// Make the POST request using Axios
axios(config)
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.log(error);
});
We can even apply the discount code, here is an example:
const axios = require('axios');
// Set up the Axios config
const config = {
method: 'post',
url: 'https://your-magento-store-url.com/rest/V1/carts/{cartId}/coupons',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer <your_access_token>'
},
data: {
"coupon_code": "YOUR_COUPON_CODE"
}
};
// Make the POST request using Axios
axios(config)
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.log(error);
});
We're including an Authorization header with a valid access token that has the apply permission for coupons. In the data field, we're specifying the coupon code we want to apply to the cart. Once we get a response from the API, we're logging that response data to the console. It will contain the coupon code that was applied to the cart.
$Note that if you want to apply a coupon to a cart for a customer who is logged in, you would
need to include the customer's ID in the URL (/rest/V1/customers/{customerId}/carts/{cartId}
/coupons) and include the customer's access token in the Authorization header.