
Prerequisites
This configuration of a Ghost publication requires existing moderate knowledge of JavaScript. You’ll need an active Ghost account to get started, which can either be self-hosted or using a Ghost(Pro) account. Additionally, you’ll need to install Hexo via the command line:hexo server
and navigating to http://localhost:4000/
in a web browser.
More information on setting up and creating a Hexo site can be found on the official Hexo site.
Getting started
Firstly, create a new JavaScript file within ascripts
folder at the root of the project directory, for example ./scripts/ghost.js
. Any script placed in the scripts folder acts like a Hexo script plugin, you can find out more about the Plugins API in the Hexo documentation.
Next, install the official JavaScript Ghost Content API helper using:
ghost.js
Hexo script:
url
value to the URL of the Ghost site. For Ghost(Pro) customers, this is the Ghost URL ending in .ghost.io, and for people using the self-hosted version of Ghost, it’s the same URL used to view the admin panel.
Create a custom integration within Ghost Admin to generate a key and change the key
value.

The code
Once the API integration has been setup, content can be pulled from your Ghost site. To get all posts, use theapi.posts.browse()
endpoint:
hexo.post.create()
function. The instance of hexo
is already globally available inside of Hexo script files.
Promise based API
The Ghost Content API is ‘Promised based’ meaning the JavaScript library will wait for all the content to be retrieved before it fully completes. Due to this the whole script needs to be wrapped in anasync
function. Here’s a full example:
hexo server
in the command line and navigate to http://localhost:4000/
in a web browser.
Next steps
The example code above is the most straightforward approach to using Ghost with Hexo. To use other content such as pages, authors and site data check out the JavaScript Content API documentation. As well as our documentation there’s the official Hexo documentation which explains other ways Hexo can accept data.Examples
The flexibility of the Ghost Content API allows you to generate posts, pages and any other pieces of content from a Ghost site and send it to a front-end built with the Node.js based static site generator, Hexo. Below are a few examples of how various types of content can be sent to your Hexo front-end. All examples assume that the API has already been setup, see the Working with Hexo page for more information.Generate pages
Pages require a slightly different approach to generating posts as they need to be placed at root level. Use the following code in conjunction with the JavaScript Ghost Content API:hexo.extend.generator.register
, which is how scripts inside of a Hexo can generate files alongside the build process.
Generate author pages
Author pages can also be generated using the following method. This also uses thegenerator
extension in Hexo that was used in the pages example above. To prevent URL collisions these author pages are being created under an /authors/
path.
Adding post meta
All the metadata that is exposed by the Ghost Content API is available to use inside of a Hexo site. That includes post meta like authors and tags. In the example below theposts.browse()
API options have been changed to include tags and authors which will be attached to each post object when it is returned. More information on the include
API option can be found in our Content API Endpoints documentation.
author.slug
includes /authors/
in the string so it correlates with the previous author pages example. Note as well that some manipulation has been performed on tags so it matches the expected format for Hexo (comma separated tags).