Ghost is an open-source platform for building and running a modern online publication. It powers blogs, magazines, and journalists from Zappos to Sky News.
In this tutorial, you'll learn how setup Ghost on your local machine and use its API to build a blog in Nuxt.js.
Why should you use Ghost
Now the question remains why you should use the Ghost in the first place. Here are some benefits it has over other Content Management Systems:
Extensible and rich text editor:
Ghost provides a familiar editing environment that enables a seamless authoring experience that allows writers to focus on creating content. You can imagine it as a mix of Mediums editor and Slack's integration which makes it very easy to edit your articles while providing a lot of great functionality. The editor is also fully extensible thanks to MobileDoc.
Free and open-source:
Ghost is an entirely open-source application that can be used with nearly any modern technology stack. It provides a local installation setup for both Windows and Linux and even an own Docker image. You can also pay for it if you would like a Ghost Pro cloud solution.
Optimized distribution channels:
Publish once, distribute everywhere. This principle is a core building block of Ghost and enables the user to easily control SEO (Search Engine Optimisation), define meta tags and even provides integration for tools like RSS readers, Email and Slack.
Complete JSON API:
Ghost's JSON API gives you full control over what data to get, set and display. It gives you the opportunity to build entire blogs or other publishing apps on top of it, and completely customize the reading experience. You will use the API in this tutorial to get the data into your custom Nuxt.js blog.
Now that you have an overview of why you should use Ghost and where it can be useful. Let's take a look at how you can install Ghost on your local machine. The installation is split into two parts:
Installing Ghost on your local machine (Two different ways)
Basic Ghost configuration so you can use the API
The installation process on Windows is straight forward and can be done using the Ghost-CLI (Command Line Interface) which can be downloaded using the node package manager (npm) or yarn.
Once installed, use the following command to verify your installation.
After successfully installing the CLI you can continue by installing a local version of Ghost using the following command in an empty directory.
ghost install local
Your ghost site should now be visible on http://localhost:2368. You can also verify that it is running using the ghost ls command.
That is it for the windows installation. Here are some useful commands to control your Ghost instances.
ghost stop: to stop Ghost
ghost start: to start Ghost
ghost log: views logs
Another way to install Ghost on your local machine is through the official docker image. If you have no previous experience with Docker I would recommend using the installation guide above because it can be harder to understand at first.
Here is the command to run a basic Ghost installation:
docker run -d --name ghost -p 2368:2368 ghost
This command runs a docker container with the official ghost image and publishes the port 2368 so that you can use the container on your host system without having to type the IP address of the container before the port.
Your instance should now run on http://localhost:2368 and should be identical to that of the previous installation.
You need to keep one more thing in mind when you install Ghost using Docker. And that is how you can preserve the state of your application when your container lifecycle changes. This is done by defining volumes.
docker run -d --name ghost -p 2368:2368 -v local/path/data:/var/lib/ghost/content ghost
If you want more information on how to setup Ghost using Docker I would recommend checking out the documentation for the official image.
I never personally installed Ghost on Ubuntu that is why I will just link to the official documentation instead.
Now that the installation is complete and you have Ghost running on your local machine let's continue with the basic configuration of your Ghost project.
The first screen you see when you visit http://localhost:2368 should look something like this.
Here you just need to click create an account and continue with the setup.
Next up you need to create a user. Just fill in the form and then continue.
The next step is to add other writers which we will skip for now. After clicking continue you should see a window that looks similar to this one.
The next step is to enable the API so you can start developing your frontend in Nuxt.js. You can do so by navigating to integrations and click on "Add custom integration".
Now you just need to give it a name in my case API.
That is it, you should see your Content and Admin API Keys which you will use to get the data from the API into your Frontend.
Perfect, now that the setup is completed let's take a look at the Ghost Content API and all the possibilities and endpoints that are available.
The Ghost RESTful API delivers published content to users and can be accessed in a read-only manner by any client to render the website. Access control is managed via an API key and provides an SDK which makes it easy to filter and query your data.
All endpoints of the Content API use the GET keyword and return JSON data. Here is a list of the most important Endpoints:
/posts - Get posts
/authors - Get authors
/tags - Get tags
/pages - Get pages
/settings - Get settings
Responses of these endpoints are lists that contain objects. Here is a sample response of the /posts endpoint.
"html": "<p>👋 Welcome, it's great to have you here.</p>",
"custom_excerpt": "Welcome, it's great to have you here.",
"excerpt": "Welcome, it's great to have you here."
The sdk also let's you define the spezific fields you want to query and other filter options.
Now that you have Ghost setup property and know the basics about the API let's start diving into your Nuxt.js frontend.
First, you need to create a new Nuxt.js project and install the needed dependencies using the following commands.
npx create-nuxt-app nuxt-ghost
cd nuxt-ghost && npm install @tryghost/content-api --save
These commands will create a basic Nuxt project and install the needed dependency. Now you only need to create the needed files and folders before you can start coding. Here is a screenshot of my folder structure:
Implementing the Ghost API:
Now that you have completed the setup process, you can go ahead and start implementing the actual functionality. Let's start by copying your API key into the constant.js file you created above.
export const API_KEY = "YOUR-KEY"
The next step is to implement some utilities that will make using the Ghost content API library a lot easier. For that, you need to add some simple code to your ghost.js file.
Here, you get all post you have published in Ghost and save them in a variable by defining the endpoint you want to send the request to (In this case posts) and define the fields that you want to get (If you don't define the fields you will get all by standard).
Then you return the first article of the array which you will display in your layout.
All you need to do now is render the markup and add some style.
There you go. You've successfully fetched a blog post from the Ghost API.
If you open your app, it will look something like this.
Voilà! You have a modern server-side rendered blog, build with Ghost.
We barely scratched the surface of what is possible with the Ghost API but got a great overview of how to use it. The other requests function very similar and can be added by you with very small time investment.
The whole code of this tutorial is also available on my Github.
You made it all the way until the end! I hope that this article helped you understand the basics of Ghost and how you can use the API to build custom applications.
If you have found this useful, please consider recommending and sharing it with other fellow developers and joining my email list. If you have any questions or feedback, let me know on twitter or over my contact form.