How to Count Your Ghost CMS Drafts with Node.js
Track your Ghost drafts in seconds with Node.js and the Admin API. No more scrolling or manual counting.
Have you ever wondered how many drafts are waiting in your Ghost CMS?
Imagine having 1,000 drafts and counting them one by one… no way!
That's just inefficient, and let's face it – just a waste of time.
In this guide, we'll show you how to automate the process with a simple Node.js script using the Ghost Admin API, so you can see all your draft posts instantly.
What is an API?
API stands for Application Programming Interface, it is a set of rules and tools that lets one program talk to another.
- Think of it as a bridge or messenger between apps.
- It allows a program to request data or actions from another system without needing a user to interact manually.
- ie: Your Node.js script uses the Ghost Admin API to get a list of draft posts without opening the Ghost admin panel.
What is the Admin API Key?
The Admin API Key is a special password that allows a program to access all administrative functions of your Ghost site through the Admin API.
1. What You'll Need
- A Ghost CMS instance (v5 or v6).
- An Admin API key from Ghost Admin.
- Node.js installed on your server.
# Add NodeSource repo for Node.js 22.x
$ curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
# Install Node.js 22 + npm
$ sudo apt install -y nodejsVerify
$ node -v
$ npm -v
2. Get Your Admin API Key
- Log in to your Ghost Admin panel →
https://yoursite.com/ghost/. - Go to Settings → Integrations → Add custom integration.
- Copy your 89-character Admin API Key (e.g.,
5f1e7d1b3cd6d93e1c1f1234:48b1d7…).
Keep it private — anyone with it can fully control your Ghost site.
Let's break that down a little 👇
The Ghost Admin API key always has two parts separated by a colon:
<id>:<secret>
a. ID
- Typically 24 hexadecimal characters (0–9, a–f).
- ie:
5f1e7d1b3cd6d93e1c1f1234
b. Secret
- Typically 64 hexadecimal characters.
- ie:
48b1d7bfb9e555f9beeb80ee9b301f1b27d247451abcdef1234567890abcdef
Full key
- Total length = 24 + 1 + 64 = 89 characters (the colon counts as 1).
3. Install the Admin API Client
You install the Admin API Client so your Node.js script can easily talk to Ghost's Admin API—handling authentication, requests, and responses without manually crafting HTTP calls.
$ npm install @tryghost/admin-api
4. Create the Script
Make a file called draft_counter.js:
const GhostAdminAPI = require('@tryghost/admin-api');
const api = new GhostAdminAPI({
url: 'https://yoursite.com',
key: 'YOUR_ADMIN_KEY_HERE', // full id:secret
version: 'v5.0' // use 'v6.0' if on Ghost v6
});
(async () => {
try {
const posts = await api.posts.browse({ filter: 'status:draft', limit: 'all' });
console.log(`Draft posts count: ${posts.length}`);
} catch (err) {
console.error('Error:', err.message);
}
})();
Let's break this down step by step 👇
a. Import the Ghost Admin API library
const GhostAdminAPI = require('@tryghost/admin-api');
This pulls in Ghost's official Admin API client for Node.js.
It's what lets your script talk to Ghost Admin (to read/write posts, tags, settings, etc.).
b. Create an API client instance
const api = new GhostAdminAPI({
url: 'https://yoursite.com',
key: 'YOUR_ADMIN_KEY_HERE', // full id:secret
version: 'v5.0' // use 'v6.0' if on Ghost v6
});url→ your Ghost blog’s base URL.key→ Admin API Key you copied from Ghost Admin → Settings → Integrations.
c. Async function wrapper
(async () => {
try {
const posts = await api.posts.browse({ filter: 'status:draft', limit: 'all' });
console.log(`Draft posts count: ${posts.length}`);
} catch (err) {
console.error('Error:', err.message);
}
})();
- Wraps everything in an immediately-invoked async function → so you can use
await. api.posts.browse()queries all posts.filter: 'status:draft'= only drafts.limit: 'all'= return all results, not just the default 15.- Logs the number of drafts.
- If something fails (e.g., bad key), it logs the error message.
5. Run It
$ node draft_counter.js
Output example:
Draft posts count: 98With just a few lines of JavaScript, you now have a quick way to monitor all your Ghost CMS drafts. This can be extended further to send alerts, integrate with dashboards, or even automate publishing workflows.
Snubmonkey™ always finds smarter ways to keep your content in check.