Drupal 9 GET articles rest API call using JSON:API

Reading time: 2 minutes

Problem

How to GET all Article content type nodes list via rest API call using JSON:API in Drupal 9 site.

Final output

By reading this article, we will be able to GET all nodes of a content type using JSON:API module by rest API.

Prerequisite

Get content type with JSON:API?
All requests use the ‘/jsonapi’ path prefix, which is the default value of the JSON:API module.

So, If you already did that then let’s Get article or any custom content type nodes list with rest API call in Drupal 9 🙂

Solution

1. After you have setup JSON:API using guideline mentioned above in Prerequisite section in your Drupal 9 admin panel.

Then we are going to test it through rest API using Postman testing tool.

GET article content type API

* Request:
Method:
GET


URL:
{{BASE_URL}}/jsonapi/node/article

NOTE: In URL instead or /article you can mention your desired content type to get all the nodes of that content type.

Get article API call postman
Get all nodes of article content type using rest API call request and response

* Response

{
    "jsonapi": {
        "version": "1.0",
        "meta": {
            "links": {
                "self": {
                    "href": "http://jsonapi.org/format/1.0/"
                }
            }
        }
    },
    "data": [
        {
            "type": "node--article",
            "id": "af9e669c-dffc-494f-ae75-3009aefad2c9",
            "links": {
                "self": {
                    "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/af9e669c-dffc-494f-ae75-3009aefad2c9?resourceVersion=id%3A2"
                }
            },
            "attributes": {
                "drupal_internal__nid": 2,
                "drupal_internal__vid": 2,
                "langcode": "en",
                "revision_timestamp": "2020-05-04T02:50:39+00:00",
                "revision_log": null,
                "status": true,
                "title": "Article with image",
                "created": "2020-05-04T02:50:39+00:00",
                "changed": "2020-05-04T02:50:39+00:00",
                "promote": true,
                "sticky": false,
                "default_langcode": true,
                "revision_translation_affected": true,
                "path": {
                    "alias": null,
                    "pid": null,
                    "langcode": "en"
                },
                "body": null,
                "comment": {
                    "status": 2,
                    "cid": 0,
                    "last_comment_timestamp": 1588560639,
                    "last_comment_name": null,
                    "last_comment_uid": 1,
                    "comment_count": 0
                }
            },
            "relationships": {
                "node_type": {
                    "data": {
                        "type": "node_type--node_type",
                        "id": "ee8be7d3-cc6a-4b3e-8bb2-3472077d2485"
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/af9e669c-dffc-494f-ae75-3009aefad2c9/node_type?resourceVersion=id%3A2"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/af9e669c-dffc-494f-ae75-3009aefad2c9/relationships/node_type?resourceVersion=id%3A2"
                        }
                    }
                },
                "revision_uid": {
                    "data": {
                        "type": "user--user",
                        "id": "f2433a47-8026-4811-860a-a8dd3e23b28b"
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/af9e669c-dffc-494f-ae75-3009aefad2c9/revision_uid?resourceVersion=id%3A2"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/af9e669c-dffc-494f-ae75-3009aefad2c9/relationships/revision_uid?resourceVersion=id%3A2"
                        }
                    }
                },
                "uid": {
                    "data": {
                        "type": "user--user",
                        "id": "f2433a47-8026-4811-860a-a8dd3e23b28b"
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/af9e669c-dffc-494f-ae75-3009aefad2c9/uid?resourceVersion=id%3A2"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/af9e669c-dffc-494f-ae75-3009aefad2c9/relationships/uid?resourceVersion=id%3A2"
                        }
                    }
                },
                "field_image": {
                    "data": {
                        "type": "file--image",
                        "id": "e44bd017-ed6a-40d4-933b-86793e9a6c46",
                        "meta": {
                            "alt": null,
                            "title": null,
                            "width": 61,
                            "height": 68
                        }
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/af9e669c-dffc-494f-ae75-3009aefad2c9/field_image?resourceVersion=id%3A2"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/af9e669c-dffc-494f-ae75-3009aefad2c9/relationships/field_image?resourceVersion=id%3A2"
                        }
                    }
                },
                "field_tags": {
                    "data": [],
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/af9e669c-dffc-494f-ae75-3009aefad2c9/field_tags?resourceVersion=id%3A2"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/af9e669c-dffc-494f-ae75-3009aefad2c9/relationships/field_tags?resourceVersion=id%3A2"
                        }
                    }
                }
            }
        },
        {
            "type": "node--article",
            "id": "72d497f7-9b58-46cf-b53f-ee13ce8d7471",
            "links": {
                "self": {
                    "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/72d497f7-9b58-46cf-b53f-ee13ce8d7471?resourceVersion=id%3A3"
                }
            },
            "attributes": {
                "drupal_internal__nid": 3,
                "drupal_internal__vid": 3,
                "langcode": "en",
                "revision_timestamp": "2020-05-08T01:15:59+00:00",
                "revision_log": null,
                "status": true,
                "title": "Article with image",
                "created": "2020-05-08T01:15:59+00:00",
                "changed": "2020-05-08T01:15:59+00:00",
                "promote": true,
                "sticky": false,
                "default_langcode": true,
                "revision_translation_affected": true,
                "path": {
                    "alias": null,
                    "pid": null,
                    "langcode": "en"
                },
                "body": null,
                "comment": {
                    "status": 2,
                    "cid": 0,
                    "last_comment_timestamp": 1588900559,
                    "last_comment_name": null,
                    "last_comment_uid": 1,
                    "comment_count": 0
                }
            },
            "relationships": {
                "node_type": {
                    "data": {
                        "type": "node_type--node_type",
                        "id": "ee8be7d3-cc6a-4b3e-8bb2-3472077d2485"
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/72d497f7-9b58-46cf-b53f-ee13ce8d7471/node_type?resourceVersion=id%3A3"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/72d497f7-9b58-46cf-b53f-ee13ce8d7471/relationships/node_type?resourceVersion=id%3A3"
                        }
                    }
                },
                "revision_uid": {
                    "data": {
                        "type": "user--user",
                        "id": "f2433a47-8026-4811-860a-a8dd3e23b28b"
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/72d497f7-9b58-46cf-b53f-ee13ce8d7471/revision_uid?resourceVersion=id%3A3"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/72d497f7-9b58-46cf-b53f-ee13ce8d7471/relationships/revision_uid?resourceVersion=id%3A3"
                        }
                    }
                },
                "uid": {
                    "data": {
                        "type": "user--user",
                        "id": "f2433a47-8026-4811-860a-a8dd3e23b28b"
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/72d497f7-9b58-46cf-b53f-ee13ce8d7471/uid?resourceVersion=id%3A3"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/72d497f7-9b58-46cf-b53f-ee13ce8d7471/relationships/uid?resourceVersion=id%3A3"
                        }
                    }
                },
                "field_image": {
                    "data": {
                        "type": "file--image",
                        "id": "e44bd017-ed6a-40d4-933b-86793e9a6c46",
                        "meta": {
                            "alt": null,
                            "title": null,
                            "width": 61,
                            "height": 68
                        }
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/72d497f7-9b58-46cf-b53f-ee13ce8d7471/field_image?resourceVersion=id%3A3"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/72d497f7-9b58-46cf-b53f-ee13ce8d7471/relationships/field_image?resourceVersion=id%3A3"
                        }
                    }
                },
                "field_tags": {
                    "data": [],
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/72d497f7-9b58-46cf-b53f-ee13ce8d7471/field_tags?resourceVersion=id%3A3"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/72d497f7-9b58-46cf-b53f-ee13ce8d7471/relationships/field_tags?resourceVersion=id%3A3"
                        }
                    }
                }
            }
        },
        {
            "type": "node--article",
            "id": "2d9e718e-2b6f-4e9e-9afd-3469fab491de",
            "links": {
                "self": {
                    "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/2d9e718e-2b6f-4e9e-9afd-3469fab491de?resourceVersion=id%3A4"
                }
            },
            "attributes": {
                "drupal_internal__nid": 4,
                "drupal_internal__vid": 4,
                "langcode": "en",
                "revision_timestamp": "2020-06-21T14:30:17+00:00",
                "revision_log": null,
                "status": true,
                "title": "Article with image",
                "created": "2020-06-21T14:30:17+00:00",
                "changed": "2020-06-21T14:30:17+00:00",
                "promote": true,
                "sticky": false,
                "default_langcode": true,
                "revision_translation_affected": true,
                "path": {
                    "alias": null,
                    "pid": null,
                    "langcode": "en"
                },
                "body": null,
                "comment": {
                    "status": 2,
                    "cid": 0,
                    "last_comment_timestamp": 1592749817,
                    "last_comment_name": null,
                    "last_comment_uid": 1,
                    "comment_count": 0
                }
            },
            "relationships": {
                "node_type": {
                    "data": {
                        "type": "node_type--node_type",
                        "id": "ee8be7d3-cc6a-4b3e-8bb2-3472077d2485"
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/2d9e718e-2b6f-4e9e-9afd-3469fab491de/node_type?resourceVersion=id%3A4"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/2d9e718e-2b6f-4e9e-9afd-3469fab491de/relationships/node_type?resourceVersion=id%3A4"
                        }
                    }
                },
                "revision_uid": {
                    "data": {
                        "type": "user--user",
                        "id": "f2433a47-8026-4811-860a-a8dd3e23b28b"
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/2d9e718e-2b6f-4e9e-9afd-3469fab491de/revision_uid?resourceVersion=id%3A4"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/2d9e718e-2b6f-4e9e-9afd-3469fab491de/relationships/revision_uid?resourceVersion=id%3A4"
                        }
                    }
                },
                "uid": {
                    "data": {
                        "type": "user--user",
                        "id": "f2433a47-8026-4811-860a-a8dd3e23b28b"
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/2d9e718e-2b6f-4e9e-9afd-3469fab491de/uid?resourceVersion=id%3A4"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/2d9e718e-2b6f-4e9e-9afd-3469fab491de/relationships/uid?resourceVersion=id%3A4"
                        }
                    }
                },
                "field_image": {
                    "data": {
                        "type": "file--image",
                        "id": "e44bd017-ed6a-40d4-933b-86793e9a6c46",
                        "meta": {
                            "alt": null,
                            "title": null,
                            "width": 61,
                            "height": 68
                        }
                    },
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/2d9e718e-2b6f-4e9e-9afd-3469fab491de/field_image?resourceVersion=id%3A4"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/2d9e718e-2b6f-4e9e-9afd-3469fab491de/relationships/field_image?resourceVersion=id%3A4"
                        }
                    }
                },
                "field_tags": {
                    "data": [],
                    "links": {
                        "related": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/2d9e718e-2b6f-4e9e-9afd-3469fab491de/field_tags?resourceVersion=id%3A4"
                        },
                        "self": {
                            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article/2d9e718e-2b6f-4e9e-9afd-3469fab491de/relationships/field_tags?resourceVersion=id%3A4"
                        }
                    }
                }
            }
        }
    ],
    "links": {
        "self": {
            "href": "https://dev-solution-spirit-cms.pantheonsite.io/jsonapi/node/article"
        }
    }
}

Congratulations we have successfully get all the nodes of article content type by rest API call using JSON:API in Drupal 9 site.

Share your love with us 😉

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply