Adapt an existing project
A project you already have can become a Ghost app, in the language and framework it uses today.
Turn this project into a Ghost app, and give me the install link when it’s ready.
Here’s what that takes.
Add a manifest
Write a manifest as ghost-app.json in the project’s folder. Give the app its own App ID, and point surfaces at the page Ghost should show, which doesn’t have to be your home page.
Ghost installs only a signed manifest, served at a fixed URL such as /ghost-app.json. Add @tryghost/app-sdk to the project first, such as with pnpm add @tryghost/app-sdk, and add .ghost-app/, where the keys live, and .dev.vars, where a Cloudflare Workers project keeps the development backend key, to .gitignore. The SDK only creates keys, and only writes .dev.vars, where Git ignores them. Then:
- A project built with Vite: Add
ghostApp()from@tryghost/app-sdk/viteto the start ofpluginsinvite.config. It creates your app’s keys, serves the signed manifest invite dev, and builds it forGHOST_APP_URL, your app’s public address such ashttps://calendar.example.com, invite build. - Any other project: Create keys with
pnpm exec ghost-app keys, sign the manifest for the address it will be served from withpnpm exec ghost-app sign --url <manifest URL> --out <file>, and serve that file. Sign it again for each address, such as a new tunnel.
Run ghost-app from the SDK you installed, as pnpm exec ghost-app or your package manager’s equivalent. Outside a project with the SDK, use npx -p @tryghost/app-sdk ghost-app: A bare npx ghost-app runs an unrelated package with the same name.
Answer Ghost’s challenge
Ghost asks the app’s server to prove the app is yours, by posting a challenge to /.well-known/ghost-app/challenge. Wrap your server’s request handler with withGhostApp() from @tryghost/app-sdk/backend. It answers the challenge, and passes every other request to the fetch you give it. It works in any server with the web’s standard Request and Response. A Cloudflare Worker exports it as it is, and reads the key from cloudflare:workers:
import {env} from 'cloudflare:workers';import {withGhostApp} from '@tryghost/app-sdk/backend';
export default withGhostApp({ backendKey: env.GHOST_APP_BACKEND_KEY, fetch: (request) => env.ASSETS.fetch(request),});In a framework that routes requests itself, such as Hono, route POST /.well-known/ghost-app/challenge to handleChallenge(request, {backendKey}) instead.
Give the server the private backend key as GHOST_APP_BACKEND_KEY, without showing it on screen:
-
On your computer: In a Cloudflare Workers project, the Vite plugin puts the development key in
.dev.varsfor you. If the project has a.envfile and no.dev.vars, Wrangler reads.envinstead, so the plugin leaves it to you and says so whenvite devstarts. Check.envis git-ignored, then add the key to it from your app’s folder, in a macOS or Linux shell, or inside WSL on Windows:Terminal window pnpm exec ghost-app backend-key --check && printf "GHOST_APP_BACKEND_KEY='%s'\n" "$(pnpm exec ghost-app backend-key)" >> .envThe plugin stays quiet once
.envhas the right key. Any other server can read the key from.ghost-app/development/backend.json, ashandleChallenge()takes the key as text or as an object. -
On your host: Pipe the production key straight into the host’s command for secrets, such as
pnpm exec ghost-app backend-key --production | vercel env add GHOST_APP_BACKEND_KEY production. Never runghost-app backend-keyon its own, since it prints the private key.
Pass the request as your server receives it, so the challenge matches the address Ghost used.
Connect with the SDK
Add @tryghost/app-sdk and connect as the page loads:
import {connect, GhostAppError} from '@tryghost/app-sdk';
try { const ghost = await connect(); document.title = ghost.site.title;} catch (error) { if (error instanceof GhostAppError && error.code === 'not_framed') { // Opened outside Ghost Admin. Show the page without Ghost's data. }}Ghost gives the page 10 seconds to connect, so call connect() as soon as it loads. Start fetches, analytics, and anything else that has an effect once it has resolved, as Ghost Admin loads your page a second time for dialogs. Keep your content clear of Ghost Admin’s navigation and your app’s footer, too. Around your page covers both. Everything else the SDK offers is in App SDK, and every function and error code is in the SDK reference.
Your page runs in a sandbox inside Ghost, so check it against what the sandbox allows. A page that uses the browser’s confirm() dialog, for example, needs a dialog of its own.
Allow it in a frame
Ghost shows your page in an iframe. Browsers refuse if your page sends an X-Frame-Options header, or a Content-Security-Policy header with frame-ancestors 'self', and Ghost then says your app isn’t responding.
To see what a page sends, with your own address:
curl -s -D - -o /dev/null http://localhost:3000/admin | grep -iE '^HTTP/|x-frame-options|content-security-policy'The first line should say 200. Remove X-Frame-Options from the response for the page Ghost shows. If the response has frame-ancestors, make sure it allows your site’s Ghost Admin address.
Sign people in without cookies
Inside Ghost, your app’s cookies count as third-party, and browsers block or partition those. A sign-in that relies on a cookie stops working there, and a page that redirects to a sign-in screen never connects. Show the page without the redirect, and keep people signed in another way, such as a token.
Check your server and domain
- If your project has a server, see Apps with their own server, including what it takes for that server to call Ghost
- Ghost treats every page on your app’s domain as your app, so if your project shares a domain with anything else, give it its own domain
Run and install it
Start your project with its own dev command and port, then follow Manual setup, using your port and manifest URL. If Ghost refuses the app, see Troubleshooting.