In this article, we will be looking into how we can get the CMS Pages & CMS Blocks in Magento 2 using its API.
- Get CMS Page by page id
- Get CMS Block by block id
- Get multiple CMS Pages using SearchCriteria
- Get multiple CMS Blocks using SearchCriteria
Anonymouse APIs Restriction
The REST endpoints to get the stores’ information falls under the anonymous APIs category. However, in the recent Magento version, those API endpoints are restricted. They need “admin” or “integration” token to authenticate.
If you need/want to bypass the token authentication for those APIs, you can disable this feature by going to:
Magento Admin > STORES > Settings > Configuration > Services > Magento Web API > Web API Security > Allow Anonymous Guest Access = Yes.
NOTE:
- We will be passing the admin bearer token to authenticate the APIs.
- This article shows how we can get the admin token in Magento: Magento 2 API: Get Admin Token
Get Single CMS Page
Endpoint:
GET <host>/rest/V1/cmsPage/:pageId
Authentication Bearer Token:
<admin-token>
Response:
{
"id": 2,
"identifier": "home",
"title": "Home Page",
"page_layout": "1column",
"meta_keywords": "",
"meta_description": "",
"content_heading": "Home Page",
"content": "",
"creation_time": "2022-11-18 06:06:38",
"update_time": "2022-11-25 15:45:23",
"sort_order": "0",
"layout_update_xml": "",
"custom_theme": "",
"custom_root_template": "",
"active": true
}
Get Single CMS Page using cURL Request
curl --location --request GET 'https://app.your-project.test/rest/V1/cmsPage/2' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx'
Get Single CMS Block
Endpoint:
GET <host>/rest/V1/cmsBlock/:blockId
Authentication Bearer Token:
<admin-token>
Response:
{
"id": 2,
"identifier": "contact-us-info",
"title": "Contact us info",
"content": "",
"creation_time": "2022-11-25 15:45:23",
"update_time": "2022-11-25 15:45:23",
"active": true
}
Get Single CMS Block using cURL Request
curl --location --request GET 'https://app.your-project.test/rest/V1/cmsBlock/2' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx'
Get Multiple CMS Pages
Endpoint:
GET <host>/rest/V1/cmsPage/search?searchCriteria[pageSize]=:pageSize
Authentication Bearer Token:
<admin-token>
Response:
{
"items": [
{
"id": 1,
"identifier": "no-route",
"title": "404 Not Found",
"page_layout": "2columns-right",
"meta_keywords": "Page keywords",
"meta_description": "Page description",
"content_heading": "Whoops, our bad...",
"content": "",
"creation_time": "2022-11-18 06:06:37",
"update_time": "2022-11-18 06:06:37",
"sort_order": "0",
"active": true
},
{
"id": 2,
"identifier": "home",
"title": "Home Page",
"page_layout": "1column",
"meta_keywords": "",
"meta_description": "",
"content_heading": "Home Page",
"content": "",
"creation_time": "2022-11-18 06:06:38",
"update_time": "2022-11-25 15:45:23",
"sort_order": "0",
"layout_update_xml": "",
"custom_theme": "",
"custom_root_template": "",
"active": true
},
{
"id": 3,
"identifier": "enable-cookies",
"title": "Enable Cookies",
"page_layout": "1column",
"content_heading": "What are Cookies?",
"content": "",
"creation_time": "2022-11-18 06:06:38",
"update_time": "2022-11-18 06:06:38",
"sort_order": "0",
"active": true
}
],
"search_criteria": {
"filter_groups": [],
"page_size": 3
},
"total_count": 6
}
Get Multiple CMS Pages using cURL Request
curl --location --request GET 'https://app.your-project.test/rest/V1/cmsPage/search?searchCriteria[pageSize]=3' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx'
Get Multiple CMS Blocks
Endpoint:
GET <host>/rest/V1/cmsBlock/search?searchCriteria[pageSize]=:pageSize
Authentication Bearer Token:
<admin-token>
Response:
{
"items": [
{
"id": 1,
"identifier": "footer_links_block",
"title": "Footer Links Block",
"content": "",
"creation_time": "2022-11-25 15:45:23",
"update_time": "2022-11-25 15:45:23",
"active": true
},
{
"id": 2,
"identifier": "contact-us-info",
"title": "Contact us info",
"content": "",
"creation_time": "2022-11-25 15:45:23",
"update_time": "2022-11-25 15:45:23",
"active": true
},
{
"id": 3,
"identifier": "sale-left-menu-block",
"title": "Sale Left Menu Block",
"content": "",
"creation_time": "2022-11-25 15:45:23",
"update_time": "2022-11-25 15:45:23",
"active": true
}
],
"search_criteria": {
"filter_groups": [],
"page_size": 3
},
"total_count": 17
}
Get Multiple CMS Blocks using cURL Request
curl --location --request GET 'https://app.your-project.test/rest/V1/cmsBlock/search?searchCriteria[pageSize]=3' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx'
Hope this helps. Thanks.
References:
- https://developer.adobe.com/commerce/webapi/rest/quick-reference/
- https://adobe-commerce.redoc.ly/2.4.5-admin/tag/cmsPagepageId
- https://adobe-commerce.redoc.ly/2.4.5-admin/tag/cmsBlockblockId