If you use Magento 2, you heard about API. But what is it, how does it work, and why do you need it? In this article, we will try to explain API in simple terms and answer all these questions.
What is API?
API stands for as Application Program Interface. This is a set of protocols and tools for software applications creating. This technology is used to connect different software and set up how their features should interact with each other. In other words, API is a contract between programs that says what each of them can do and how to ask for these actions.
You actually deal with API almost every day. For example, if you log in to a website by using your Facebook or Google profile or if you pay via PayPal, you are using API.
How does the API work?
To make it clear, we will give you a very simple example.
Imagine you have a dog. This dog doesn’t understand the human language, but it knows some commands. It will understand you if you use the command it knows. For example, if it knows that the command “ball” means “bring me this yellow ball”, it will do it. But if you ask it: “would you be so kind as to bring me this yellow ball”, it won’t understand what you are asking for.
The same is with API. You can use some specific commands like: get, post, delete, put, etc. to create the connection between 2 programs that know this “language”.
How does it work in real life? Well, say, you want to let your customers log in to their accounts via Facebook. In this case, you will use Facebook API Key and API Secret to connect your Magento 2 store with Facebook. Magento will ask Facebook about customers’ data and, according to the response given, allow them to access or restrict.
How to use API for Magento 2 store?
As API is a multifunctional tool, there are 2 main use cases of it in Magento 2 stores:
- integrations with 3rd-party software
- mobile app development.
Let’s consider them closely.
There are many cases when you may need to integrate your Magento 2 with other software. Say, you have a CRM that already includes thousands of your clients, and you launch an online store. It will be convenient to exchange the data between Magento 2 and your CRM to not miss any customer. Or maybe, you want to provide customers with safe payment options and integrate Stripe to your store. Or if you work with a specific carrier, and you want to get the information about parcels from them. For all these cases, you will need to use API.
In our Stripe plugin, you can see the Publishable Key and Secret Key fields in the settings:
If you look in the user guide, you will see that these keys are located in the API Keys tab of your Stripe account. These keys are like login and password that permit programs to communicate with each other. And they are a part of API integration.
→ If you want to dive deeper into details and learn more about default Magento 2 functionality, API users, types of authentication, swagger, and so on, check our API guide for technical specialists.
Magento 2 API for mobile apps
Say, you want to offer users not only a mobile version of your website but also a quick and convenient application. Or maybe you want to manage your e-store on the go right from your smartphone. So you will need to develop an application and connect your website database with the app via API.
When talking about API, we can not but mention that there are different types of it. The most common are GraphQL and REST. They have many similarities, but they are usually used for different purposes.
What is the difference between GraphQL and REST?
GraphQL is used when you need to connect the frontend and backend. For example, when you are working on PWA. GraphQL often works faster and has improved targeting.
REST API is more common for complex integrations. Also, in some cases, the REST API may have much better performance than GraphQL. So we can not say that one of these languages is better than the other. It all depends on your goals.
→ For a more detailed comparison with code examples, check this article about GraphQL in Magento 2.3.
What Amasty plugins support API?
In our extensions, we use both APIs REST and GraphQL. Today, 30+ of our extensions support REST API. The list includes plugins for shipping, promotions, payments, and other features.
→ See it here: What Magento 2 plugins by Amasty support REST API?.
Speaking of GraphQL API, 15+ of our Magento 2 plugins like Automatic Related Products, Product Labels, Blog Pro, Custom Form, Shop by Brand, Mega Menu, etc. support it. And some of them have ready-made PWA add-ons that you can use to speed up the development process:
we continue to extend this list.
Finally, if you just realized after reading this post that you need a hand in custom integration, our development team is always ready to help you out and offer integration service. Note that this is not a ready-made solution, but custom development from scratch according to your business needs.
That’s it for today!
Hope now you have a better understanding of API in Magento 2 and its use in mobile applications and integrations.
And what do you prefer REST or GraphQL API? Leave your answer in the comment section!