SDKs overview

Ghost CMS offers a set of Application Programming Interfaces (APIs) designed for managing content and administrative tasks programmatically. These APIs include the Content API, for reading published content, and the Admin API, for creating, updating, and deleting posts, pages, and managing users and settings Ghost API documentation. To simplify interactions with these RESTful endpoints, Ghost provides official Software Development Kits (SDKs).

The SDKs are primarily developed for Node.js environments, reflecting Ghost's own technology stack. These libraries abstract the underlying HTTP requests, authentication mechanisms, and JSON parsing, allowing developers to interact with the Ghost API using native JavaScript objects and promises. This approach reduces boilerplate code and potential errors, streamlining the integration of Ghost into custom applications, static site generators, or other backend services.

While the official support focuses on Node.js, the RESTful nature of the Ghost API enables integration from any programming language capable of making HTTP requests. Community-contributed libraries and examples exist for other languages, often leveraging standard HTTP client libraries, as described by MDN Web Docs on Fetch API for web-based interactions or similar patterns for server-side languages.

Official SDKs by language

Ghost maintains two primary official SDKs for Node.js, each tailored to specific API functionalities:

  • Ghost Content API Client: This SDK is designed exclusively for interacting with the Ghost Content API. It provides methods for fetching posts, pages, authors, and tags, adhering to read-only operations. It is suitable for front-end applications, static site generation, or any scenario where only published content needs to be retrieved.
  • Ghost Admin API Client: This SDK facilitates interaction with the Ghost Admin API. It enables full CRUD (Create, Read, Update, Delete) operations on posts, pages, users, and various site settings. This client requires authentication using an Admin API key and is intended for backend services, content management tools, or custom publishing workflows.

Both SDKs are published as npm packages, making them accessible via the standard Node.js package manager.

Language Package Name Install Command Purpose
Node.js @tryghost/content-api npm install @tryghost/content-api Read published content (Posts, Pages, Authors, Tags)
Node.js @tryghost/admin-api npm install @tryghost/admin-api Create, Read, Update, Delete content & manage settings

Installation

Installing Ghost's official Node.js SDKs involves using the npm package manager, which is included with Node.js installations. Before proceeding, ensure that Node.js and npm are installed on your system. You can verify their presence by running node -v and npm -v in your terminal.

Prerequisites

  • Node.js (LTS version recommended)
  • npm (Node Package Manager)

Steps for installation

  1. Navigate to your project directory: Open your terminal or command prompt and change to the root directory of your Node.js project. If you don't have a project yet, create a new directory and initialize a Node.js project:
    mkdir my-ghost-app
    cd my-ghost-app
    npm init -y
  2. Install the desired SDK:
    • For the Content API Client:
      npm install @tryghost/content-api
    • For the Admin API Client:
      npm install @tryghost/admin-api
  3. Verify installation: After installation, the package will be listed in your package.json file under dependencies.

Once installed, you can import and use the SDK in your JavaScript or TypeScript files. API keys are required for both SDKs and can be generated within your Ghost admin interface under Integrations Ghost Integrations guide. The Content API key is public and included in the site URL, while the Admin API key is secret and should be handled securely, for example, using environment variables.

Quickstart example

This quickstart demonstrates how to fetch posts using the Ghost Content API Client in a Node.js environment. This example assumes you have already installed the @tryghost/content-api package and have your Ghost site URL and Content API key ready.

Fetching posts with the Content API Client

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

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

// Configure the Ghost API client
const api = new GhostContentAPI({
    url: 'YOUR_GHOST_SITE_URL', // e.g., 'https://your-blog.ghost.io'
    key: 'YOUR_CONTENT_API_KEY', // e.g., 'a1b2c3d4e5f6g7h8i9j0k1l2'
    version: 'v5.0' // Specify the API version
});

// Fetch all posts
async function fetchPosts() {
    try {
        const posts = await api.posts
            .browse({
                limit: 'all',
                include: 'tags,authors'
            })
            .catch(err => {
                console.error(err);
            });

        console.log('Fetched posts:');
        posts.forEach(post => {
            console.log(`- ${post.title} by ${post.primary_author.name}`);
        });
    } catch (error) {
        console.error('Error fetching posts:', error);
    }
}

fetchPosts();

Before running:

  1. Replace 'YOUR_GHOST_SITE_URL' with the URL of your Ghost site.
  2. Replace 'YOUR_CONTENT_API_KEY' with a valid Content API key generated from your Ghost admin panel.

To run this example:

Execute the file from your terminal:

node getPosts.js

This script will connect to your Ghost instance, fetch all available posts including their tags and authors, and print their titles and primary author names to the console. For more advanced operations, such as creating or updating content, you would use the Ghost Admin API Client with appropriate authentication.

Community libraries

While Ghost provides official SDKs for Node.js, the open and RESTful nature of its APIs has led to the development of community-contributed libraries and tools in various other programming languages and frameworks. These libraries are typically maintained by individual developers or communities and may offer different levels of support and feature parity compared to the official SDKs.

Developers often create their own wrappers or helper functions when official SDKs are not available for their preferred language. This usually involves leveraging standard HTTP client libraries available in languages like Python (e.g., requests), PHP (e.g., Guzzle), Ruby (e.g., httparty), or Go (e.g., net/http). These custom implementations handle constructing API requests, managing authentication headers, and parsing JSON responses, following the specifications outlined in the Ghost API documentation.

For front-end development, especially with modern JavaScript frameworks, it is common to directly use the native fetch API or libraries like Axios to interact with the Ghost Content API. This approach minimizes dependencies and provides granular control over data fetching and caching strategies. Examples of this pattern are frequently found in projects building static sites with frameworks like React, Vue, or Next.js that consume content from a headless Ghost instance.

When considering community-contributed libraries, it is advisable to evaluate their active maintenance, community support, and alignment with the latest Ghost API versions. Checking the project's GitHub repository for recent commits, issues, and pull requests can provide insight into its current state and reliability. For critical applications, developers may opt to build their own lightweight client or use a mature, well-supported HTTP client library to ensure full control and stability.

The flexibility of Ghost's API design, which adheres to standard web protocols, allows for this broad range of integration methods, empowering developers to choose the tools best suited for their specific project requirements and technology stack.