CMS Api In Mage2

Published: 03-01-2017 Author: David Manners

Magento 2 like Magento 1 comes with an inbuilt rest api system. This system will allow you to do a multitude of things including, but not limited to:

For the purpose of this article I will focus on the cms part and in particular creating and editing cms pages. Though more detailed information on all the rest api calls can be found on DevDocs.

What are we aiming to do?

The outcome of this article is to demonstrate how it would be possible to add and edit cms pages via a rest api built into Magento 2.

I am making the assumption that you have the following:

NOTE: to access the rest api you need to call your local url, in our case http://www.mage2.devbox, with the url /rest

Get authenticated

First thing we will need to do is create a request to get the authentication key for Magento2's rest api. For this example I will simply be using the admin user. To get the authentication token for the admin we will call the api /V1/integration/admin/token with the correct posted variables.

For using SoapUI we set the following:

{
    "username": "admin",
    "password": "test1234"
}

The result of this will be a token that we can use in all our other requests.

Create Page

Now that we have our token we can create a page. Again we can see the details in the Magento2 swagger page

For using SoapUI we set the following:

{
    "page": {
        "identifier": "mage-2-blog",
        "title": "Mage 2 Blog Example",
        "page_layout": "2columns-right",
        "meta_keywords": "Page keywords",
        "meta_description": "Page description",
        "content_heading": "A New Page for the blog",
        "content": "A New Page for the blog - with some new content",
        "sort_order": "0",
        "active": true
    }
}

On success the response given back should be the CMS page's data in the Magento 2 system including the page id.

{
   "id": 6,
   "identifier": "mage-2-blog",
   "title": "Mage 2 Blog Example",
   "page_layout": "2columns-right",
   "meta_keywords": "Page keywords",
   "meta_description": "Page description",
   "content_heading": "A New Page for the blog",
   "content": "A New Page for the blog - with some new content",
   "sort_order": "0",
   "active": true
}

Edit Page

If we would now like to edit this page we can do a similar thing but using a different route. For this we will use the route PUT /V1/cmsPage/{ID} where the id is the page we would like to update.

For using SoapUI we set the following:

{
    "page": {
        "id": 6,
        "identifier": "mage-2-blog",
        "title": "Mage 2 Blog Example Update",
        "page_layout": "2columns-right",
        "meta_keywords": "Page keywords",
        "meta_description": "Page description",
        "content_heading": "A New Page for the blog",
        "content": "A New Page for the blog - with some new content",
        "sort_order": "0",
        "active": true
    }
}

On success the response given back should be the CMS page's data in the Magento 2 system including the page id with the updated information.

{
   "id": 6,
   "identifier": "mage-2-blog",
   "title": "Mage 2 Blog Example Update",
   "page_layout": "2columns-right",
   "meta_keywords": "Page keywords",
   "meta_description": "Page description",
   "content_heading": "A New Page for the blog",
   "content": "A New Page for the blog - with some new content",
   "sort_order": "0",
   "active": true
}

Get Page

We can of course get the page information from Magento in a similar fashion. Using the route GET /V1/cmsPage/{ID}

For using SoapUI we set the following:

The response here will be the page information.

{
   "id": 6,
   "identifier": "mage-2-blog",
   "title": "Mage 2 Blog Example Update",
   "page_layout": "2columns-right",
   "meta_keywords": "Page keywords",
   "meta_description": "Page description",
   "content_heading": "A New Page for the blog",
   "content": "A New Page for the blog - with some new content",
   "creation_time": "2016-11-01 10:30:54",
   "update_time": "2016-11-01 10:37:53",
   "sort_order": "0",
   "active": true
}

Delete Page

And of course finally we can also delete the page information from Magento. Using the route DELETE /V1/cmsPage/{ID}

For using SoapUI we set the following:

For a success here you will simply get true as your response otherwise you will see the appropriate error message.

Overview

As was the case with Magento 1, Magento 2 comes with a large selection of options for the API. For me I can see a couple of real use cases here:

Firstly I can see the option of building a bridge between the CMS system and an external translation system. This would allow you to send your content directly to a translation service and update it based on their response. Having this sort of tool would be very useful for a lot of clients who are dealing with external translation of content.

The second use case I can see would be building a tool that would use both the Magento 1 and Magento 2 APIs. This system could be then used to migrate from an old system based on Magento 1 into a new system based on Magento 2.