Magento (Adobe Commerce) is one of the widely used E-Commerce platforms by more than 250,000 merchants worldwide. To manage your workflows and orders, prices, stocks, etc., one needs the help of 3rd party tools and platforms. Magento REST APIs allow developers to integrate Magento (Adobe Commerce) with other applications easily. If you are a developer and want to add more features to your application with Magento then you should use the Magento REST APIs.
The Magento REST APIs are the fast, efficient, and reliable way to integrate any app with Magento (Adobe Commerce). Software providers require Magento REST APIs for shipping management, order and inventory management, Marketing Automation, CRM, and ERP solutions.
Magento REST APIs Integration allows apps to access data from Magento stores to other apps and platforms. In this article, you will learn about Magento REST APIs, how to work with REST APIs using cURL. You will also read about the elements of the Magento REST APIs.
What is Magento (Adobe Commerce)?
Magento (Adobe Commerce) is an e-commerce platform that is entirely built on PHP frameworks. It allows developers to quickly set up an E-Commerce website using its pre-built components essential for the basic functioning of any E-Commerce website. Magento (Adobe Commerce) is an open-source platform, and anyone can access its source code and customize it according to their needs.
Magento (Adobe Commerce) is available in 2 versions or packages, i.e. Magento Open-Source and Magento Commerce, where both the packages contain a basic version of the Magneto site. Magento Open-Source is free to use, and the Magento Commerce version is paid and comes with additional advanced features to provide greater flexibility. It offers many themes, widgets, apps, extensions, and much more to help users start an online store quickly.
Key Features of Magento (Adobe Commerce)
Magento (Adobe Commerce) allows retail sellers to effortlessly set up an enterprise-level E-Commerce website. Many features attract people for using it. A few of them are listed below:
- Multi-Platform Integrations: Magento (Adobe Commerce) supports integrations with many apps, platforms, and services used in the industry to optimize and manage the workflow.
- SEO optimized: Magento (Adobe Commerce) takes your reach to customers and visibility on search engines. It is designed to render SEO (Search Engine Optimization) optimized content on your site, which will help you rank higher on search engines.
- Customizable Themes: It comes with a theme store that contains a collection of themes for every category and business. The themes fasten the process of building an E-Commerce website.
Efficiently migrate your data from Magento. Hevo elevates your data migration game with its no-code platform. Ensure seamless data migration using features like:
- Seamless integration with your desired data warehouse, such as BigQuery or Redshift.
- Transform and map data easily with drag-and-drop features.
- Real-time data migration to leverage AI/ML features of BigQuery and Synapse.
Still not sure? See how Postman, the world’s leading API platform, used Hevo to save 30-40 hours of developer efforts monthly and found a one-stop solution for all its data integration needs.
Get Started with Hevo for Free
Introduction to REST APIs
REST API, which stands for Representational State Transfer Application Programming Interface, is a software architecture style used to guide the design and development of the architecture for the World Wide Web. It defines some set of constraints on how the architecture of internet-scaled distributed systems should work. A typical 3-tier application stands between the User Interface (which allows end-users to access and alter data) and the Database (which stores the data).
An HTTP request is used to access and use the data with commands such as PUT, GET, DELETE, POST, etc. The components are loosely coupled which makes the data flow through HTTP fast and efficient. REST APIs are easier to create and manage because the data formats are not pre-defined and can be used for wider purposes. Read about the best practices of REST API to elevate your API writing skills.
Elements of a Magento REST APIs
The Magento REST APIs contain many functions that are used to send requests and receive a response. All the interactions or tasks of receiving and sending data are performed using the HTTP protocol. Magento REST APIs includes the elements listed below:
HTTP Headers
There are 3 HTTP headers in Magento REST APIs listed in the table below:
HTTP HEADER | DESCRIPTION | SYNTAX |
Authorization | It is a required field, except for calls made on behalf of a guest. This Magento REST APIs header specifies the authentication token for the Magento (Adobe Commerce) account. You specify the token in the Authorization request header with the Bearer HTTP authorization scheme. | Authorization: Bearer <TOKEN> <TOKEN> is the authentication token returned by the Magento (Adobe Commerce) token service. |
Accept | It is an optional field that specifies the format of the response body. The default format is JSON. | Accept: application/<FORMAT> <FORMAT> is either JSON or XML. |
Content-Type | It is a required field for operations with a request body that is used to specify the format of the request body. | Content-Type:application/<FORMAT> <FORMAT> is either JSON or XML. |
HTTP Verb
A Magento REST APIs has 4 HTTP verbs, and each has a different role. Users can call any of the following HTTP verbs in the requests. The HTTP verbs for Magento REST APIs are listed below:
- GET: GET is the default HTTP verb, and it requests the transfer of the current state of the target resource.
- PUT: It contains a message payload in the request and creates or replaces the state of the target resource with the state defined in the message payload.
- POST: It requests that the server accept the message enclosed in the request as data to be processed by the target.
- DELETE: It requests that the origin server delete the target resource.
Endpoint
An endpoint is a combination of the server that can perform actions on a request, the store code, the web service against the request, and any template parameters.
For example in the endpoint “http://magento.ll/index.php/rest/default/V1/customerGroups/:id”, the server is “magento.ll/index.php/”, the web service part is rest, the resource is “/V1/customerGroups” and the template parameter is “id”. The store code is by default is null and the value “all” only applies in the endpoints in the Product Modeles and CMS. The value affects all the merchant stores.
Call Payload
The Magento REST APIs have both required and optional inputs. The call payload is a set of parameters that one can ad with the request and send to the target resource.
A URI (Uniform Resource Identifier ) is present in the Request message header. You can input parameters in URI like “GET/V1/customers/:customerId”, where the “customerId” is the template parameter that will filter out the response by the specified customer ID.
You can provide the input attributes in JSON or XML format. An example code for the request body is shown below:
{
"customer": {
"email": "user@example.com",
"firstname": "John",
"lastname": "Doe"
},
"addresses": [
{
"defaultShipping": true,
"defaultBilling": true,
"firstname": "John",
"lastname": "Doe",
"region": {
"regionCode": "CA",
"region": "California",
"regionId": 12
},
"postcode": "90001",
"street": ["Zoe Ave"],
"city": "Los Angeles",
"telephone": "555-000-00-00",
"countryId": "US"
}
]
}
Steps to Run Magento REST APIs Via cURL
Now that you have understood Magento REST APIs and its basic structure and elements. In this section, you will learn the steps to run the requests using cURL. A cURL is a command-line tool that allows users to send HTTP requests and receive responses. The steps for using cURL requests in Magento REST APIs are listed below:
Calling a Magento REST APIs Request
You will now learn the steps to create an account using Magento REST APIs. The steps are listed below:
- Open the file “Magento/Customer/etc/webapi.xml” by clicking here.
- Now, search for the route element that defines the “createAccount” call, as shown in the code below:
<route url="/V1/customers" method="POST">
<service class="MagentoCustomerApiAccountManagementInterface" method="createAccount"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
- Now, construct the URI by using the values of “method” and “url” in the route element. For the above code, the URI will be POST /V1/customers.
- Next, use the class attribute in the service element for identifying the service interface. For this example, the service interface is the “AccountManagementInterface.php” file. Get the PHP file here. Open the file and search for the “createAccount” method as shown in the code below.
public function createAccount(
MagentoCustomerApiDataCustomerInterface $customer,
$password = null,
$redirectUrl = ''
)
- The “customer” data object is required to call “createAccount”. For passing the “customer” data request in the POST call payload, specify JSON or XML request body on the call.
- Magento (Adobe Commerce) offers its service wrapper to use cURL instead of using PHP cURL. To create an instance of the class MagentoFrameworkHTTPClientCurl. The code for reference is shown below:
/**
* Constructor.
*
* @param MagentoFrameworkHTTPClientCurl $curl
*/
public function __construct(
MagentoFrameworkHTTPClientCurl $curl
) {
$this->curl = $curl;
}
Load Data from Magento to any Data Warehouse
No credit card required
Magento REST APIs: GET Request
The GET request allows users to request a target resource. The code for making a GET request with Magento REST APIs using cURL is shown below:
// get method
$this->curl->get($url);
// output of curl request
$result = $this->curl->getBody();
Here, “$url” is the endpoint URL.
Magento REST APIs: POST Request
The POST request in Magento REST APIs allows users to send data in enclosed payload messages. The example code is shown below:
// post method
$this->curl->post($url, $params);
// output of curl requestt
$result = $this->curl->getBody();
Here “$url” is the endpoint URL. the “$params” attribute is an array of data to be sent using a POST request. An example of “$params” is shown below.
$params = [
'user[email]' => $user->getEmail(),
'user[cellphone]' => $providerInfo['phone_number'],
'user[country_code]' => $providerInfo['country_code'],
]
Conclusion
In this article, you learnt about the Magento REST APIs and the steps to implement the basic commands using cURL. You also read about the elements of Magento REST APIs. Magento REST APIs allow developers to integrate Magento (Adobe Commerce) with other platforms and tools to optimize the workflow and developing new applications. Magento (Adobe Commerce) is a versatile E-Commerce platform that offers all the features for enterprises.
Companies need E-Commerce data to analyze it and generate insights from it. The manual process of extracting data from Magento (Adobe Commerce) is tedious. Hevo Data is a No-code Data Pipeline that can help you transfer data from Magento (Adobe Commerce) to desired Data Warehouse. It fully automates the process to load and transform data from 150+ sources to a destination of your choice without writing a single line of code.
Want to take Hevo for a spin? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite firsthand.
Share your experience of learning about Magento REST APIs in the comments section below!
FAQs
1. Does Magento have an API?
Yes, Magento offers APIs (REST and GraphQL) for integrating with external systems, allowing data exchange like products, customers, and orders.
2. What is the difference between Magento REST API and GraphQL?
The REST API uses structured endpoints and returns large payloads, while GraphQL provides a flexible query mechanism, fetching only required data and improving performance and customization.
3. How to get data from API in Magento 2?
Authenticate via OAuth or token-based access, then use tools like Postman or cURL to call the desired REST or GraphQL endpoints, retrieving Magento 2 data.
Aditya Jadon is a data science enthusiast with a passion for decoding the complexities of data. He leverages his B. Tech degree, expertise in software architecture, and strong technical writing skills to craft informative and engaging content. Aditya has authored over 100 articles on data science, demonstrating his deep understanding of the field and his commitment to sharing knowledge with others.