Skip to content

daniel-miller/ghost-integration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Ghost Integration

A small .NET console app that bulk-imports pages and members into a Ghost site via the Ghost Admin API.

The tool signs requests with a short-lived JWT generated from your Ghost Admin API key, paginates through existing content to skip duplicates, and shows progress with Spectre.Console.

Prerequisites

  • .NET 10 SDK on your PATH (dotnet --list-sdks should list a 10.0.x entry).
  • A Ghost site you control.
  • A Ghost Admin API key in the form <24-hex-id>:<64-hex-secret>. Generate one in Ghost Admin under Settings → Integrations → + Add custom integration, then copy the Admin API Key. The Content API key will not work.

Building

From the repository root:

dotnet build src/GhostIntegration.csproj

The solution file src/GhostIntegration.sln can also be opened in Visual Studio or Rider.

Usage

Both commands share the same three options:

Option Description
--url Base URL of your Ghost site (e.g. https://example.ghost.io — trailing slash optional).
--key Admin API key, format id:secret.
--input Path to the JSON file to import.

The importer is idempotent: it lists existing pages (by slug) or members (by email) and skips any item that already exists, so re-running after a partial failure is safe.

Import pages

dotnet run --project src -- import-pages --url <ghost-url> --key <admin-api-key> --input pages.json

pages.json is an array of objects with these properties (case-sensitive — they map to ExportedPage):

Property Type Notes
Path string Becomes the page slug.
Name string Used as the title when Title is empty.
Title string Page title.
Summary string Becomes the page excerpt.
Content string HTML body. Posted with ?source=html so Ghost converts it to Lexical.
Date ISO-8601 timestamp Used for created_at, updated_at, and published_at.
Day, Month string Accepted for compatibility but currently unused.

Imported pages are created with status: published and visibility: public.

Example:

[
  {
    "Path": "about",
    "Name": "about",
    "Title": "About Us",
    "Summary": "Who we are.",
    "Content": "<p>Hello, world.</p>",
    "Date": "2024-01-01T00:00:00Z"
  }
]

Import members

dotnet run --project src -- import-members --url <ghost-url> --key <admin-api-key> --input members.json

members.json is an array of objects matching ExportedMember:

Property Type Notes
Email string Required by Ghost; used as the dedup key.
Name string Display name.
CreatedAt ISO-8601 timestamp Optional sign-up date.

Example:

[
  {
    "Email": "member@example.com",
    "Name": "Member Name",
    "CreatedAt": "2024-01-01T00:00:00Z"
  }
]

How it works

  • GhostJwtHelper splits the Admin API key, base64url-encodes a HS256 JWT header/payload, and HMAC-signs the body. The token is valid for 5 minutes and scoped to audience /admin/.
  • GhostApiHelper wraps a single HttpClient plus the resolved base URL and token.
  • Each request sets Authorization: Ghost <jwt> and Accept-Version: v5.0, matching Ghost Admin API v5.
  • Listing endpoints (/ghost/api/admin/pages/ and /.../members/) are paged until an empty page is returned; results seed a HashSet used to skip already-present items.
  • Errors surface as HTTP <status>: <body> (pretty-printed when the body is JSON), and the progress task is stopped on first failure.

Project layout

src/
  Program.cs                       Spectre.Console.Cli entry point
  ImportPagesCommand.cs            "import-pages" command
  ImportMembersCommand.cs          "import-members" command
  ImportPagesCommandSettings.cs    CLI options for import-pages
  ImportMembersCommandSettings.cs  CLI options for import-members
  GhostApiHelper.cs                HttpClient + URL + token holder
  GhostJwtHelper.cs                Admin API key -> JWT
  GhostPageItem.cs / GhostMemberItem.cs   Ghost API DTOs
  ExportedPage.cs   / ExportedMember.cs   Input file DTOs

License

See LICENSE.

About

Sample project to demonstrate API integration with Ghost

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages