---
title: "How to Create, Activate and Authorize a 'New Integration' Programmatically in Magento 2?"
url: "https://magecomp.com/blog/create-activate-authorize-a-new-integration-programmatically-in-magento-2/"
date: "2018-02-22T17:24:00+00:00"
modified: "2025-02-22T07:20:58+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 452
reading_time: "3 min read"
summary: "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 autom..."
description: "Detailed guide to create, activate and authorize new integration programmatically in Magento 2. Use the code and seamlessly take benefit of various 3rd party..."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to write a Delete SQL query using Standard way without Model File in Magento 2"
    url: "https://magecomp.com/blog/write-delete-sql-query-using-standard-way-without-model-file-magento-2/"
  - title: "How to Create and Manage Duplicate Categories in Magento 2?"
    url: "https://magecomp.com/blog/configure-duplicate-categories-in-magento-2/"
  - title: "How to Programmatically Export CSV File Category Name with Parent Category Name using Root Script in Magento 2?"
    url: "https://magecomp.com/blog/programmatically-export-csv-file-category-name-with-parent-category-name-using-root-script-magento-2/"
---

# How to Create, Activate and Authorize a 'New Integration' Programmatically in Magento 2?

_Published: February 22, 2018_  
_Author: Dhiren Vasoya_  

![img How to Create Activate and Authorize a New Integration Programmatically in Magento 2](https://magecomp.com/blog/wp-content/uploads/2018/02/img_How-to-Create-Activate-and-Authorize-a-New-Integration-Programmatically-in-Magento-2-1024x332.webp)

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.

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-3.webp)](https://magecomp.com/magento-malware-removal.html)

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.

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-3.webp)](https://magecomp.com/magento-performance-optimization.html)

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 MagentoFrameworkAppBootstrap;
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('MagentoIntegrationModelIntegrationFactory')->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('MagentoIntegrationModelIntegrationFactory')->create();
        $integration = $integrationFactory->setData($integrationData);
        $integration->save();
        $integrationId = $integration->getId();$consumerName = 'Integration' . $integrationId;

        // Code to create consumer
        $oauthService = $objectManager->get('MagentoIntegrationModelOauthService');
        $consumer = $oauthService->createConsumer(['name' => $consumerName]);
        $consumerId = $consumer->getId();
        $integration->setConsumerId($consumer->getId());
        $integration->save();
        // Code to grant permission
        $authrizeService = $objectManager->get('MagentoIntegrationModelAuthorizationService');
        $authrizeService->grantAllPermissions($integrationId);

        // Code to Activate and Authorize
        $token = $objectManager->get('MagentoIntegrationModelOauthToken');
        $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.

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-3.webp)](https://magecomp.com/services/hyva-theme-development/)


---

_View the original post at: [https://magecomp.com/blog/create-activate-authorize-a-new-integration-programmatically-in-magento-2/](https://magecomp.com/blog/create-activate-authorize-a-new-integration-programmatically-in-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-05-24 11:31:38 UTC_  
