Categories: How ToMagento 2

How to Create, Activate and Authorize a ‘New Integration’ Programmatically in Magento 2

Third party API integration in Magento 2 is used to get benefitted with features and functionality that default Magento doesn’t provide. With the 3rd party API integrations, store admin can automate work to introduce flexibility and new perspectives. In Magento 2, API integrations is used to embed functionality and features from a system to guarantee smooth information delivery and integrated user experience.

An integration pays a vital role in calling Magento 2 web APIs to utilize third party services. Out of the box, Magento 2 APIs support many third-party systems like Analysis tracker, CRMs, ERP systems, order processing systems, PIMs, marketing automations and accounting. The integration is a bridge that communicate with the Magento systems to use web services.

To user 3rd party services in Magento 2, you need to create integration from Magento 2 backend, activate the integration object and use tokens to request access. All this can be done through Magento 2 backend which a bit time consuming task. Here, we have come up with custom code to create, activate and authorize a new integration programmatically in Magento 2.

use Magento\Framework\App\Bootstrap;
include_once('../app/bootstrap.php');

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

//Set your Data
$name = ‘Integration-Name';
$email = 'Set- EmailId';
$endpoint = 'Set- Url'; (e.g 'http://localhost/magento/')

// Code to check whether the Integration is already present or not
$integrationExists = $objectManager->get('Magento\Integration\Model\IntegrationFactory')->create()->load($name,'name')->getData();
if(empty($integrationExists)){
    $integrationData = array(
        'name' => $name,
        'email' => $email,
        'status' => '1',
        'endpoint' => $endpoint,
        'setup_type' => '0'
    );
    try{
        // Code to create Integration
        $integrationFactory = $objectManager->get('Magento\Integration\Model\IntegrationFactory')->create();
        $integration = $integrationFactory->setData($integrationData);
        $integration->save();
        $integrationId = $integration->getId();$consumerName = 'Integration' . $integrationId;


        // Code to create consumer
        $oauthService = $objectManager->get('Magento\Integration\Model\OauthService');
        $consumer = $oauthService->createConsumer(['name' => $consumerName]);
        $consumerId = $consumer->getId();
        $integration->setConsumerId($consumer->getId());
        $integration->save();
        // Code to grant permission
        $authrizeService = $objectManager->get('Magento\Integration\Model\AuthorizationService');
        $authrizeService->grantAllPermissions($integrationId);

        // Code to Activate and Authorize
        $token = $objectManager->get('Magento\Integration\Model\Oauth\Token');
        $uri = $token->createVerifierToken($consumerId);
        $token->setType('access');
        $token->save();

    }catch(Exception $e){
        echo 'Error : '.$e->getMessage();
    }
}

Simply customize and use above code to easily and quickly create new integration in Magento 2 and start using 3rd party web services seamlessly. Hope this code has enough helped you to serve the purpose, let me know for which web service, you implemented the code. Also appreciate our efforts of creating programmatic code by smashing 5 stars below. Comment down your questions, feedback and suggestions if any, we would be glad to hear from you.

Click to rate this post!
[Total: 4 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • Is it the right way to do it? When publishing the extension does it pass the quality test?

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago