SDKs overview

Ghost provides a publishing platform designed for independent creators and publishers, emphasizing content delivery through its robust APIs. The core of Ghost's extensibility relies on two primary APIs: the Ghost Content API and the Ghost Admin API. These APIs enable developers to programmatically interact with Ghost, fetch published content, and manage administrative tasks such as creating or updating posts, pages, and members.

To simplify interaction with these APIs, Ghost offers official SDKs, primarily focusing on JavaScript and Node.js environments. These SDKs abstract the underlying HTTP requests, authentication, and data parsing, allowing developers to integrate Ghost functionality into their applications with less boilerplate code. Beyond the official tools, a range of community-contributed libraries have emerged, supporting various programming languages and specialized use cases, from front-end frameworks to static site generators.

The Content API is designed for public consumption of published content. It's read-only and requires an API key for access. This API is commonly used in front-end applications, mobile apps, or static site generators to display posts, pages, and authors. The Admin API, conversely, provides full read/write access to all data within a Ghost instance, including content, members, themes, and settings. It is intended for secure backend integrations, custom admin panels, or automation scripts, and requires authentication via JSON Web Tokens (JWTs).

Developers working with Ghost can choose between direct HTTP calls to the APIs or leveraging SDKs for a more streamlined development experience. The choice often depends on the project's specific requirements, the developer's preferred language, and the level of abstraction desired.

Official SDKs by language

Ghost officially maintains SDKs primarily for JavaScript and Node.js, reflecting the platform's underlying technology stack. These SDKs are developed to provide a consistent and documented interface for interacting with the Ghost Content and Admin APIs. They handle API key management, request signing, and response parsing, reducing the burden on developers to manage these aspects manually.

The official SDKs are designed to be lightweight and performant, suitable for both server-side Node.js applications and client-side JavaScript environments. They are regularly updated to ensure compatibility with the latest Ghost API versions and to incorporate best practices for API integration.

The following table outlines the key official SDKs available:

Language Package Name Install Command (npm) Purpose Maturity
JavaScript / Node.js @tryghost/content-api npm install @tryghost/content-api Interact with the Ghost Content API (read-only) Stable
JavaScript / Node.js @tryghost/admin-api npm install @tryghost/admin-api Interact with the Ghost Admin API (read/write) Stable

Installation

Installation of the official Ghost SDKs is typically performed using Node Package Manager (npm) or Yarn, the standard package managers for JavaScript and Node.js projects. Before installation, ensure you have Node.js and npm (or Yarn) installed on your system. You can check Node.js installation instructions for your operating system.

Installing the Content API SDK

To install the official Ghost Content API SDK, navigate to your project directory in the terminal and execute the following command:

npm install @tryghost/content-api

Alternatively, using Yarn:

yarn add @tryghost/content-api

Installing the Admin API SDK

To install the official Ghost Admin API SDK, use the following command:

npm install @tryghost/admin-api

Or with Yarn:

yarn add @tryghost/admin-api

After installation, the packages will be added to your project's node_modules directory and listed in your package.json file, making them available for import and use within your JavaScript or Node.js application.

Quickstart example

This example demonstrates how to use the @tryghost/content-api SDK to fetch the latest posts from a Ghost instance. Ensure you have your Ghost site's URL and a Content API key ready. You can find your Content API key in your Ghost Admin panel under Integrations > Custom Integrations > Add custom integration.

JavaScript (Node.js) Example: Fetching Posts

First, create a new JavaScript file (e.g., get-posts.js) and add the following code:

const GhostContentAPI = require('@tryghost/content-api');

// Initialize the API client
const api = new GhostContentAPI({
  url: 'YOUR_GHOST_SITE_URL',
  key: 'YOUR_CONTENT_API_KEY',
  version: 'v5.0' // Or your specific API version (e.g., 'v4.0')
});

// Fetch posts
api.posts
  .browse({
    limit: 5,
    include: 'authors,tags'
  })
  .then((posts) => {
    posts.forEach((post) => {
      console.log(`Title: ${post.title}`);
      console.log(`URL: ${post.url}`);
      console.log(`Primary Author: ${post.primary_author.name}`);
      if (post.primary_tag) {
        console.log(`Primary Tag: ${post.primary_tag.name}`);
      }
      console.log('---');
    });
  })
  .catch((err) => {
    console.error(err);
  });

Replace 'YOUR_GHOST_SITE_URL' with your Ghost site's URL (e.g., 'https://yourdomain.com') and 'YOUR_CONTENT_API_KEY' with your actual Content API key. The version parameter should match your Ghost instance's API version, which is typically found in the Ghost documentation or your site's API settings.

To run this example, save the file and execute it from your terminal:

node get-posts.js

This script will output the titles, URLs, primary authors, and primary tags of the five most recent posts from your Ghost site. For more detailed usage of the Content API, refer to the Ghost Content API JavaScript client documentation.

Admin API Example: Creating a Post

Using the @tryghost/admin-api requires an Admin API key, which also comes from your Ghost Admin panel under Integrations > Custom Integrations. This key is used to generate a JWT for authentication.

const GhostAdminAPI = require('@tryghost/admin-api');

// Initialize the API client
const api = new GhostAdminAPI({
  url: 'YOUR_GHOST_SITE_URL',
  key: 'YOUR_ADMIN_API_KEY',
  version: 'v5.0' // Or your specific API version
});

// Create a new post
api.posts
  .add(
    {
      title: 'My New Post from SDK',
      html: '<p>This is a post created programmatically using the Ghost Admin API SDK.</p>',
      status: 'draft', // or 'published'
    },
    { source: 'html' }
  )
  .then((post) => {
    console.log(`Post created: ${post.title} (ID: ${post.id})`);
  })
  .catch((err) => {
    console.error(err);
  });

Remember to replace placeholders with your actual site URL and Admin API key. Running this script will create a new draft post on your Ghost instance. For more details on the Admin API, consult the Ghost Admin API JavaScript client documentation.

Community libraries

While Ghost provides official SDKs for Node.js/JavaScript, the open-source nature of the platform has fostered a community that contributes libraries and tools in various other programming languages and for specific use cases. These community-developed resources extend Ghost's reach and enable integrations with different tech stacks and frameworks.

Community libraries often focus on:

  • Client-side frameworks: Libraries that integrate Ghost content directly into React, Vue, Angular, or other front-end frameworks.
  • Static Site Generators (SSGs): Plugins or connectors for SSGs like Gatsby, Next.js, Eleventy, or Jekyll, allowing Ghost to serve as a headless CMS for static sites.
  • Backend languages: SDKs or wrappers for languages such as Python, PHP, Ruby, or Go, enabling server-side applications to interact with Ghost APIs.
  • Specialized tools: Integrations for specific services, data synchronization tools, or custom publishing workflows.

It's important to note that the stability, maintenance, and feature parity of community libraries can vary. Developers should review the repository's activity, documentation, and community support before integrating them into production environments. The Ghost Integrations page and GitHub are common places to discover these community projects.

Examples of common types of community integrations include:

  • Ghost source plugins for SSGs: For instance, gatsby-source-ghost allows Gatsby sites to pull content from a Ghost instance. Similarly, Next.js applications can fetch data directly from the Ghost Content API or use a dedicated utility library.
  • GraphQL layers: Some community projects provide a GraphQL endpoint on top of Ghost's REST APIs, offering a more flexible query language for content retrieval.
  • Webhooks and automation: Libraries or services that process Ghost webhooks for custom automation, such as triggering builds for static sites or sending notifications.

When using community libraries, always check for compatibility with your Ghost version and API version, and consult the project's documentation. The Ghost developer community forums are also a valuable resource for finding support and discovering new tools.