How Many Drafts Are in Your Ghost Site — Count Them with Node.js —(updated)
Count draft posts in Ghost CMS with a secure Node.js script. This guide uses Ghost’s Admin API, stores the API key in environment variables, and paginates results for Ghost 5 and Ghost 6.
Wondering how many draft posts are sitting in your Ghost CMS? If you have hundreds or thousands, counting them by hand is tedious. A short Node.js script can query Ghost’s Admin API and report the total in seconds.
This guide walks through the setup and provides a script that works with Ghost 5 and Ghost 6. It uses the Admin API because draft posts are private and are not available through the public Content API. The script also paginates its requests, so it can count more than one page of results on Ghost 6.
What you’ll need
- A Ghost site running Ghost 5 or Ghost 6.
- Node.js installed on the computer or server where you’ll run the script.
- An Admin API key from a custom integration in Ghost Admin.
Run this script on a trusted server or computer. Never put an Admin API key in browser-side JavaScript or commit it to a public repository. The key can authorize administrative API requests.
1. Install Node.js
Use your preferred Node.js version manager or your operating system’s package source. For Debian or Ubuntu, NodeSource documents this setup for Node.js 22:
$ sudo apt install -y curl
$ curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
$ sudo -E bash nodesource_setup.sh
$ sudo apt install -y nodejs
Check that Node.js and npm are available:
$ node --version
$ npm --version
Ghost 6 itself requires Node.js 22. If you are using Ghost 5, check your Ghost installation’s supported Node.js version and choose a compatible release.
2. Create a Ghost Admin API key
- Sign in to Ghost Admin, usually at
https://your-site.example/ghost/. - Open Settings → Integrations.
- Select Add custom integration and give it a name, such as
Draft Counter. - Copy the Admin API key shown for the integration.
An Admin API key contains an ID and a secret separated by a colon. The @tryghost/admin-api client uses the full key to create short-lived authentication tokens for API requests.
Keep the key secret. Store it in an environment variable rather than hard-coding it into the script. If a key is exposed, regenerate it in Ghost Admin and update any scripts that use it.
3. Create a project and install the Ghost client
Create a directory for the script, enter it, initialize a Node.js project, and install Ghost’s Admin API client:
$ mkdir ghost-draft-counter
$ cd ghost-draft-counter
$ npm init -y
$ npm install @tryghost/admin-api
Set the site URL and Admin API key as environment variables for the current terminal session. Replace the example URL and key with your own values:
$ export GHOST_URL='https://your-site.example'
$ export GHOST_ADMIN_API_KEY='YOUR_ADMIN_API_KEY_ID:YOUR_ADMIN_API_SECRET'
Use your Ghost admin domain as the URL, without a trailing slash. For Ghost(Pro), this is normally the site’s *.ghost.io domain. If your Ghost site is served from a subdirectory or uses a different admin domain, use the exact URL configured for that Ghost installation.
These export commands apply to the current terminal session. For a more persistent setup, use your server’s secret-management system or a protected environment file that is excluded from version control. Do not publish the key.
4. Create the draft-counting script
Create a file named draft_counter.js in the project directory:
const GhostAdminAPI = require('@tryghost/admin-api');
const url = process.env.GHOST_URL;
const key = process.env.GHOST_ADMIN_API_KEY;
if (!url || !key) {
console.error('Set GHOST_URL and GHOST_ADMIN_API_KEY before running this script.');
process.exit(1);
}
const api = new GhostAdminAPI({
url,
key,
version: 'v5.0'
});
async function countDrafts() {
let page = 1;
let totalDrafts = 0;
let totalPages;
do {
const posts = await api.posts.browse({
filter: 'status:draft',
limit: 100,
page
});
totalDrafts += posts.length;
totalPages = posts.meta.pagination.pages;
page += 1;
} while (page <= totalPages);
console.log(`Draft posts count: ${totalDrafts}`);
}
countDrafts().catch((error) => {
console.error('Could not count draft posts:', error.message);
process.exitCode = 1;
});
The Admin API JavaScript client is server-side software. Its posts.browse() method returns an array of posts with pagination metadata. The script requests drafts in batches of 100, adds the number returned on each page, and continues through the number of pages reported by Ghost.
The API version in the client configuration is a compatibility version, not the installed Ghost major version. v5.0 is used for this example and is compatible with Ghost 5 and Ghost 6 API versioning. Ghost 6 limits each request to 100 results and no longer returns every result for limit: 'all', so this script explicitly paginates rather than relying on limit: 'all'.
5. Run the script
From the project directory, run:
$ node draft_counter.js
Example output:
Draft posts count: 98
The number will depend on your site. The script only reads posts and prints the number of drafts; it does not edit or publish anything.
Troubleshooting
Missing environment-variable message
Set both GHOST_URL and GHOST_ADMIN_API_KEY in the terminal where you run Node.js. Environment variables set in another terminal session will not automatically be available.
Authentication error
Check that you copied the Admin API key, not the Content API key. Confirm that the key belongs to an integration and has not been regenerated, and verify that GHOST_URL is the correct admin domain.
Connection or URL error
Make sure the URL uses https:// for a Ghost(Pro) site and does not end with a slash. For self-hosted sites, verify the domain, any subdirectory, and that the server is reachable from the computer running the script.
Unexpected count
This script counts posts whose status is exactly draft. It does not count scheduled, published, or other post statuses. It retrieves results page by page, using Ghost’s pagination metadata to determine when it has reached the end.
Documentation references
- Ghost Admin API overview and authentication
- Ghost Admin API JavaScript client
- Ghost breaking changes: Ghost 6 pagination and
limit=all - NodeSource installation instructions for Node.js 22
A small Node.js script makes it easy to check how many drafts are in a Ghost publication without opening the admin panel or counting posts manually. Because it uses the Admin API, it can access private draft content; keep the API key on the server, and never expose it in browser code. The pagination loop supports large collections on both Ghost 5 and Ghost 6.
Keep learning. Keep exploring. Stay secure.