The Most Popular Extension Builder for Magento 2

With a big catalog of 224+ extensions for your online store

How to Create Magento 2 Integration Manually and Programmatically

Today, my topic will point out the steps you need to follow to Create new API Integration both manually and programmatically, which means integrating your Magento store with a third-party API.

Please follow the instructions below to understand specifically what you should do to create a Magento 2 Integration for your e-commerce store.

Magento API Integration Service by Mageplaza

Connect your store with any 3rd-party software and boost customer experience quickly and efficiently.

Learn more
Magento API Integration service

Create Magento 2 Integration Manually

3 Steps to Add a new integration in Magento 2

Step 1: Open your Magento 2 backend

  • On the Admin panel, click System. In the Extensions section, select Integrations.

Step 2: Add a new integration

  • In the upper-right corner, click the Add New Integration button and complete all information
  • In the ` Integration Info` section, do the following

How to Create new API Integration Magento 2

  • In the Name field, enter the name of the integration
  • In the Email field, enter the contact email address
  • In the Callback URL field, enter the Callback URL where OAuth credentials can be sent when using OAuth for token exchange. We strongly recommend using https://.
  • In the Identity Link URL field, enter the Identity Link URL to redirect the users to a third-party account with these Magento integration credentials.
  • In the panel on the left, choose API, and do the following:

How to Create new API Integration Magento 2

  • In the Resource Access field, choose one of the following
    • All
    • Custom
  • If you choose Custom, mark the checkbox of each resource that is needed

Step 3: Save the configuration and check the result

  • When complete, click the Save button.

To change the API guest access security setting

You can enable anonymous guest access in the backend:

  • On the Admin panel, click Stores. In the Settings section, select Configuration.
  • Select Magento Web API under Services in the panel on the left
  • Open the Web API Security Setting section, and continue with the following:

How to Change the API guest access security setting

  • In the Allow Anonymous Guest Access field, select Yes
  • When complete, click Save Config.

Read more: How to Create Integration Module in Magento 2

Create Magento 2 Integration Programmatically

Magento 2 handles integrations within the Magento_Integration module. To create a Magento 2 Integration using XML configuration files, please follow these three key steps.

Step 1. Create a skeleton Module

Create the module file structure Similar to other custom modules in Magento 2, the integration module should be placed under /app/code// or under the /vendor/ folder. For instance, we will store the source code under. To keep things simple, let’s run the following commands at /app/code//.

cd 
mkdir -p app/code////etc/integration
mkdir -p app/code////Setup

Specify the configuration file for your module as etc/module.xml The etc/module.xml file contains essential details about the module. Navigate to the etc directory and create the module.xml file, where you need to provide the following information:

  • Module name
  • Module version
  • Dependencies

Since we will be using the MagentoIntegrationModelConfigBasedIntegrationManager class in the installation script, we need to add Magento_Integration as a dependency for our custom integration creator.

Add your composer.json file Composer is a PHP dependency management. To ensure that the libraries required by your module can be installed and updated by Composer, you need to create a composer.json file. Simply place this file in the root directory of your module.

{
    "name": "Mageplaza/integrationmanager",
    "description": "",
    "type": "magento2-module",
    "version": "1.0.1",
    "license": [
      "MIT"
    ],
    "autoload": {
      "psr-4": {
        "Mageplaza\IntegrationManager\": ""
      },
      "files": [
        "registration.php"
      ]
    }
  }

Create a registration.php file The registration.php file is responsible for registering your module with the Magento system. Make sure to place this file in the root directory of your module.

MagentoFrameworkComponentComponentRegistrar::register(
    MagentoFrameworkComponentComponentRegistrar::MODULE,
    'Mageplaza_IntegrationManager',
    __DIR__
);

Create an installation class that will generate an integration during the module installation process. Navigate to the Setup directory and generate a file named InstallData.php, which will handle the installation of integration configuration data into the Magento integration table.

namespace MageplazaIntegrationManagerSetup;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoIntegrationModelConfigBasedIntegrationManager;
use MagentoFrameworkSetupInstallDataInterface;
class InstallData implements InstallDataInterface
{
    /**
     * @var ConfigBasedIntegrationManager
     */
    private $integrationManager;
    /**
     * @param ConfigBasedIntegrationManager $integrationManager
     */
    public function __construct(ConfigBasedIntegrationManager $integrationManager)
    {
        $this->integrationManager = $integrationManager;
    }
    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $this->integrationManager->processIntegrationConfig(['mp_Integration']);
    }
}

The InstallData script will extract data from different XML configuration files before integrating it into Magento.

Step 2. Add Integration xml setting files

The Magento Integration module simplifies the process of defining an integration. This module is in charge of managing third-party accounts, OAuth authorizations and user data, and security tokens and requests.

To configure integrations programmatically, you can utilize two files:

  • api.xml in the etc/integration directory to define the required resources
  • config.xml in the etc/integration directory to define an integration

Step 3. Define the required resources

The file etc/integration/api.xml indicates which API resources the integration can access. Evaluate the permissions defined in each module’s etc/acl.xml file to identify which resources an integration needs access to. The test integration in the below example needs access to the essential resources in the Catalog and Sales modules:

<integration name="mp_Integration">
	<resources>
		<!-- To grant permission to Magento_Log::online, its parent Magento_Customer::customer needs to be declared as well--><resource name="Magento_Customer::customer" ?-->
		<!-- To grant permission to Magento_Sales::reorder, all its parent resources need to be declared-->
		<!-- to grant permission to Magento_Catalog::products, all its parent resources need to be declared -->

Step 4. Define the integration

In order to automatically pre-configure the integration with default values, your module has the option to include values in the configuration file. To enable this functionality, make the necessary updates to the config.xml file located in the etc/integration directory. [email protected]

Step 5. Install the module

Once you have completed the previous steps, run the familiar commands for installing the module. This will trigger the setup script to create an integration.

bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:clean

Now, go to Systems -> Extensions -> Integrations Click on the integration you need to see the result.

The bottom line

And that’s how you can create Magento 2 Integration. You are welcome to write down any concerns you have, and we will respond to support you soon.

And don’t forget that our experienced and dedicated API specialists are always here to help you! Our Magento API/GraphQL integration helps connect your store with any third-party software, uncover new business opportunities, and boost customer experience.

All you need to do is tell us your specific requirements, and we’ll handle the rest for you. Contact us for free consultations now!

EXPLORE OUR API INTEGRATION SERVICE

Related Post

Image Description
Hello, I'm the Chief Technology Officer of Mageplaza, and I am thrilled to share my story with you. My deep love and passion for technology have fueled my journey as a professional coder and an ultra-marathon runner. Over the past decade, I have accumulated extensive experience and honed my expertise in PHP development.

Looking for
Customization & Development Services?

8+ years of experiences in e-commerce & Magento has prepared us for any challenges, so that we can lead you to your success.

Get free consultant
development service
x

    Explore Our Products:

    People also searched for

    Subscribe

    Stay in the know

    Get special offers on the latest news from Mageplaza.

    Earn $10 in reward now!

    Earn $10 in reward now!

    comment
    iphone
    go up