Quickstart

These are the basic steps to follow to interact with the Ghost Content API in your TypeScript project.

Get your Ghost Admin API Key and Ghost version number

Admin API keys can be obtained by creating a new Custom Integration under the Integrations screen in Ghost Admin. Keys for individual users can be found on their respective settings page.

My advice would be to connect to your Ghost Admin panel and go to https://{your-ghost-blog-domain}/ghost/#/settings/integrations and create a new integration, choose an appropriate Name and Descriptions.

ts-ghost-api-key

For all operations You will need:

  • the Admin API Key
  • and the API URL to instantiate the client.

If you plan on creating webhooks you will also need to provide the integration_id, this is the auto-generated ID of the integration you just created. To visualize that id, on your integration page, look at the url:

  • https://{your-ghost-blog-domain}/ghost/#/settings/integrations/63887c187f2cf32001fec9a8

The last part 63887c187f2cf32001fec9a8 is the integration_id.

To know which Ghost Version you are using go in the Settings and click on top right button "About Ghost":

Ghost Version

Here the version is "v5.47.0"

Installation

Terminal
pnpm add @ts-ghost/admin-api

(Optional) Create .env variable

.env
GHOST_URL="https://myblog.com"
GHOST_ADMIN_API_KEY="1efedd9db174adee2d23d982:4b74dca0219bad629852191af326a45037346c2231240e0f7aec1f9371cc14e8"

Use in your TypeScript file

ghost-queries.ts
import { TSGhostAdminAPI } from "@ts-ghost/admin-api";
 
const api = new TSGhostAdminAPI(
  process.env.GHOST_URL || "",
  process.env.GHOST_ADMIN_API_KEY || "",
  "v5.47.0"
);
 
export async function getBlogPosts() {
  const response = await api.posts
    .browse({
      limit: 10,
    })
    .fields({
      title: true,
      slug: true,
      id: true,
      html: true,
      plaintext: true,
    })
    .formats({
      html: true,
      plaintext: true,
    })
    .fetch();
  if (!response.success) {
    throw new Error(response.errors.join(", "));
  }
  // Response data is typed correctly with only the requested fields
  // {
  //   title: string;
  //   slug: string;
  //   id: string;
  //   html: string;
  //   plaintext: string;
  // }[]
  return response.data;
}