Out of the box, Magento 2 APIs currently supports marketing automation, accounting, CRM, ERP, and many other external systems.

If you want to enable your third-party services to call Magento 2 web APIs, you need to create an API integration.

The API integration establishes the location of OAuth credentials from the backend, redirects URL for third-party integrations, and identifies the available API resources that are needed for the integration.

In Magento 2, you can create an integration manually from the backend or programmatically by building a custom Magento 2 extension

In this article, we will provide you with a step-by-step guide to create an integration manually in Magento 2 backend.

Step 1: Add A New Integration

Log in to the admin panel, and navigate to System > Extensions > Integrations.

In the upper-right corner, click Add New Integration

API integration grid

In the "General and Current User Identity Verification", you can configure as follows:

Name - enter the integration name

Email - enter the contact email address

Callback URL - enter the URL where OAuth credentials can be sent when using OAuth for token exchange. It's strongly recommended to use a secure URL in this field (eg. https://.)

Identity link URL - enter the URL where the users to a third-party account with these Magento 2 integration credentials will be redirected.

Your Password - enter the current admin password to confirm your identity

API integration infos

Click the API tab in the left panel and configure as follows:

Resource Access - If "All" is selected, all access will be granted to the Grant access to the API resources. If "Custom" is selected, choose the checkbox of each resource that is needed.

API integration access

Click Save, a new consumer public and secret keys for the integration are generated

Step 2: Activate An Integration

By default, the saved integration is not active. In this step, we will activate the integration in order to create the access token for the external systems.

From the "System > Extensions > Integrations." grid, click Activate link.

In the upper-right corner, click Allow

API integration acl


Integration details appear: Consumer Key, Consumer Secret, Access Token, and Access Token Secret

Consumer Key: e357jwgsgdw3bvznay2j81g2a9lh4egf
Consumer Secret: evy8srnf5poxppbw1hpqyox2r9okpkob
Access Token: te30c2uu9k7hoauw8gn8renr2nhzjyk4
Access Token Secret : unm7bho0qz708b8owc9xoxheoqhs9l0u

Click Done in the upper-right corner.

API integration acl

The access token after generating can be used by any 3rd party systems, or API test tools (eg. as swagger or postman)

  • to delete the integration, go to System > Extensions > Integrations, find your integration, in the "Delete" column click on the delete icon, and click OK in the popup window to confirm your action
  • to edit the integration, go to System > Extensions > Integrations, find your integration, in the "Edit" column click on the edit icon, and click OK in the popup window to confirm your action

Step 3. Allows Guest Access (Optional)

Magento 2 by default does not allow anonymous API guests to access the CMS, catalog, and other store resources.
If you want to change this setting:

  • go to Stores > Settings > Configuration
  • Expand the Web API Security Setting section and set "Set Allow Anonymous Guest Access" to Yes
  • Click Save Config

API guest access

Step 4. Testing The Integration

We will make a simple API request for retrieving the info of a product. In this example, we will use:
- product SKU: 24-MB01
- access Token: te30c2uu9k7hoauw8gn8renr2nhzjyk4
- base URL: mydomain.com
- rest/V1/products/PRODUCT_SKU

The access Token is one from our "Integration details"

From Magento 2 root directory, we run this command:

curl -X GET "http://mydomain.com/index.php/rest/V1/products/24-MB01" -H "Authorization: Bearer te30c2uu9k7hoauw8gn8renr2nhzjyk4"

It returns this JSON response data:

{
   "id":1,
   "sku":"24-MB01",
   "name":"Joust Duffle Bag",
   "attribute_set_id":15,
   "price":34,
   "status":1,
   "visibility":4,
   "type_id":"simple",
   "created_at":"2019-07-16 08:22:25",
   "updated_at":"2021-07-22 06:47:22",
   "extension_attributes":{
      "website_ids":[
         1
      ],
      "category_links":[
         {
            "position":0,
            "category_id":"3"
         },
         {
            "position":0,
            "category_id":"4"
         },
         {
            "position":0,
            "category_id":"161"
         },
         {
            "position":0,
            "category_id":"173"
         }
      ],
      "stock_item":{
         "item_id":1,
         "product_id":1,
         "stock_id":1,
         "qty":100,
         "is_in_stock":true,
         "is_qty_decimal":false,
         "show_default_notification_message":false,
         "use_config_min_qty":true,
         "min_qty":0,
         "use_config_min_sale_qty":1,
         "min_sale_qty":1,
         "use_config_max_sale_qty":true,
         "max_sale_qty":10000,
         "use_config_backorders":true,
         "backorders":0,
         "use_config_notify_stock_qty":true,
         "notify_stock_qty":1,
         "use_config_qty_increments":true,
         "qty_increments":0,
         "use_config_enable_qty_inc":true,
         "enable_qty_increments":false,
         "use_config_manage_stock":true,
         "manage_stock":true,
         "low_stock_date":null,
         "is_decimal_divided":false,
         "stock_status_changed_auto":0
      }
   },
   "product_links":[
      {
         "sku":"24-MB01",
         "link_type":"related",
         "linked_product_sku":"24-MB04",
         "linked_product_type":"simple",
         "position":1
      },
      {
         "sku":"24-MB01",
         "link_type":"crosssell",
         "linked_product_sku":"24-UG01",
         "linked_product_type":"simple",
         "position":1
      },
      {
         "sku":"24-MB01",
         "link_type":"upsell",
         "linked_product_sku":"24-MB05",
         "linked_product_type":"simple",
         "position":1
      }
   ],
   "options":[
      
   ],
   "media_gallery_entries":[
      {
         "id":1,
         "media_type":"image",
         "label":"Image",
         "position":1,
         "disabled":false,
         "types":[
            "image",
            "small_image",
            "thumbnail"
         ],
         "file":"\/m\/b\/mb01-blue-0.jpg"
      }
   ],
   "tier_prices":[
      
   ],
   "custom_attributes":[
      {
         "attribute_code":"description",
         "value":"<p>The sporty Joust Duffle Bag can't be beat - not in the gym, not on the luggage carousel, not anywhere. Big enough to haul a basketball or soccer ball and some sneakers with plenty of room to spare, it's ideal for athletes with places to go.<p>\r\n<ul>\r\n<li>Dual top handles.<\/li>\r\n<li>Adjustable shoulder strap.<\/li>\r\n<li>Full-length zipper.<\/li>\r\n<li>L 29\" x W 13\" x H 11\".<\/li>\r\n<\/ul>"
      },
      {
         "attribute_code":"image",
         "value":"\/m\/b\/mb01-blue-0.jpg"
      },
      {
         "attribute_code":"url_key",
         "value":"joust-duffle-bag"
      },
      {
         "attribute_code":"gift_message_available",
         "value":"0"
      },
      {
         "attribute_code":"small_image",
         "value":"\/m\/b\/mb01-blue-0.jpg"
      },
      {
         "attribute_code":"options_container",
         "value":"container1"
      },
      {
         "attribute_code":"thumbnail",
         "value":"\/m\/b\/mb01-blue-0.jpg"
      },
      {
         "attribute_code":"swatch_image",
         "value":"no_selection"
      },
      {
         "attribute_code":"msrp_display_actual_price_type",
         "value":"0"
      },
      {
         "attribute_code":"tax_class_id",
         "value":"0"
      },
      {
         "attribute_code":"category_ids",
         "value":[
            "3",
            "4",
            "161",
            "173"
         ]
      },
      {
         "attribute_code":"required_options",
         "value":"0"
      },
      {
         "attribute_code":"has_options",
         "value":"0"
      },
      {
         "attribute_code":"image_label",
         "value":"Image"
      },
      {
         "attribute_code":"small_image_label",
         "value":"Image"
      },
      {
         "attribute_code":"thumbnail_label",
         "value":"Image"
      },
      {
         "attribute_code":"color",
         "value":"49"
      },
      {
         "attribute_code":"eco_collection",
         "value":"0"
      },
      {
         "attribute_code":"performance_fabric",
         "value":"0"
      },
      {
         "attribute_code":"erin_recommends",
         "value":"0"
      },
      {
         "attribute_code":"new",
         "value":"0"
      },
      {
         "attribute_code":"sale",
         "value":"0"
      },
      {
         "attribute_code":"override_fee",
         "value":"0"
      },
      {
         "attribute_code":"mp_sku_type",
         "value":"3"
      }
   ]
}

Conclusion

We hope you find this Magento 2 API guide helpful. Feel free to share this article.

If you prefer to create an integration programmatically, check the complete step-by-step guide to create an API integration programmatically in Magento2

If you have any issues in creating an API integration in Magento 2 or integrating your Magento 2 store with your external application/system, feel free to let us know in the comment field below or contact us

Tags: magento 2 api api