Ghost is based on a RESTful JSON API. This is the method used for all data access in Ghost. This feature has been accessible since Ghost 0.7.2, and even though Ghost 1.0 has been launched, the Ghost Public API is still in beta. However, it is stable and utilized by the Ghost Admin Panel.

What is Ghost Public API?

Ghost is an independently consuming RESTful JSON API. It has separated frontend and administrative clients. In this type of API, a Handlebars.js frontend is utilized to ensure the smooth running of the website. Authors may generate powerful ghost articles, blogs, tags, and other material using the API’s extremely solid editor.

The API is cacheable. This indicates that end users are permitted to retrieve any data they desire. It is built in JavaScript. The Ghost Public API gives you access to all posts, tags, and users on your Ghost blog.

To enable the API (>Ghost 1.0), go to the Admin panel and select “Labs” from the options menu. Mark the Public API checkbox in the Beta Features section.

This article will look at how to use the ghost Public API to access posts. We have two choices: handlebars or javascript/jQuery.

A quick way to access all posts:

{{#get "posts" limit="all"}}
  {{#foreach posts}}
    // read something here
  {{/foreach}}
{{/get}}

If we adopt it in jQuery, it will look like this:

$.get(ghost.url.api('posts', {limit: 'all'})).done(function (data){
  // read something with the posts
}).fail(function (err){
  console.log(err);
});

This is a really easy issue; most of the time, we want to select posts based on certain criteria, such as featured posts or posts with a particular tag, and so on.

Fortunately, the ghost Public API provides many options to assist us in this regard. Here are the various attributes for retrieving posts to acquire the desired posts.

Query attributes

  • limit – Specifies the maximum number of things to return (the default is @posts-per-page specified in the theme’s package.json).
  • Page – The page parameter specifies which page of a paginated collection should be returned.
  • Order – The order option allows you to sort the data before returning it.
  • Include – The include argument allows you to extend the API request with additional related data (ex. authors, tags).
  • Fields allow you to specify which resource fields to obtain instead of the entire object.
  • Filter – The filter option allows you to filter the results from the various endpoints in various ways.

These characteristics provide the essential flexibility to attain our goals. For example, if we wanted to collect the three most recent featured posts, we could do so as follows:

{{#get "posts" limit="3" filter="featured:true" order="published_at desc"}}
  {{#foreach posts}}
    // read something with the featured posts
  {{/foreach}}
{{/get}}

As you may guess, combining multiple features provides us with virtually limitless options for picking posts and creating specific sections on the blog.

To achieve the same result with jQuery, write the following code:

$.get(ghost.url.api('posts', {limit: 3, filter: "featured: true", order: "published_at desc"})).done(function (data){
  // read something with the posts
}).fail(function (err){
  console.log(err);
});

We’ve spoken about getting posts, but we can also access the ghost Public API to get “tags” or “users.”

What can we acquire using the API?

Well, there are several usage scenarios, just to mention a few:

  • Section of Featured Posts
  • Section of recent postings
  • Related posts

Users page, Tags page, and so on…

Ghost Public API
Premium themes

To Sum Up

In this post, we will not go into the specifics of how to accomplish this, but future articles will cover some of these use cases in further depth. It is simple to publish material with access limitations using Ghost public API. This JavaScript API is ideal for delivering content in real-time. Besides, don’t forget to add sufficient API security when implementing this API.

Leave a Comment

No Comment