App SDK
The app SDK connects your app’s page to Ghost Admin. It also signs your app’s manifest and answers Ghost’s challenge on your server (see Signing and your server). Ghost’s examples already include it. Every function, type, and error code for the page is in the SDK reference, generated from the SDK itself.
What’s in it
| Part | What it does |
|---|---|
connect() |
Connects your page to Ghost. Call it once, when the page loads. |
site |
Your site’s URL, title, timezone, and language. |
user |
The signed-in staff member’s ID and role. |
theme, onThemeChange() |
Light or dark mode, and Admin’s accent color. |
fetch(path) |
Reads or changes content through Ghost’s Admin API, as the signed-in staff member. Takes a path like /posts/?limit=5, never a full URL. |
open(place) |
Sends the person to a post, page, member, or the apps list in Ghost Admin. |
path.current, onPathChange() |
The screen your app is on, kept in sync with Ghost Admin’s URL. |
session |
The installation’s revision when this session started. See Session. |
connected, onSessionEnd() |
Whether the connection to Ghost is still open, and a way to hear when it closes. |
overlay |
Where your dialogs and sheets render to cover all of Ghost Admin. Pass it to Shade’s ShadeApp. See Around your page. |
Around your page
Ghost Admin’s navigation and your app’s footer float over the edges of your page. The SDK sets how far in to keep content clear of them as CSS variables on <html>, in pixels: --ghost-safe-area-inset-top, --ghost-safe-area-inset-right, --ghost-safe-area-inset-bottom, and --ghost-safe-area-inset-left. It keeps them up to date as Ghost Admin’s layout changes. Shade’s ShadeApp pads your app by them. Without Shade, pad your page by them yourself, with 0px for when the page opens outside Ghost, as Hello world does.
To let your dialogs and sheets cover all of Ghost Admin, rather than only your page, pass the SDK’s overlay to ShadeApp, as in <ShadeApp overlay={ghost.overlay}>.
Ghost Admin loads your page a second time, over the whole window, to show those dialogs. That copy never connects, but code that runs before connect() resolves runs there too. So start fetches, analytics, and anything else that has an effect once connect() has resolved.
What apps can reach
| Area | Read | Change |
|---|---|---|
| Posts | Yes | Yes |
| Pages | Yes | Yes |
| Tags | Yes | Yes |
| Staff users | Yes | No |
| Tiers | Yes | No |
| Newsletters | Yes | No |
| Site details | Yes | No |
Your app declares the scopes it uses in its manifest, and Ghost refuses any call outside them. Which scope a call needs lists the scope for each path.
Site details are what /site/ returns: The site’s title, description, logo, icon, cover image, and accent color. Use them to match the site’s branding.
Changing code injection, and reaching settings, members, or anything that hands out keys are off limits.
Everything outside the table is out of reach, including:
- Web analytics, such as visitors, pageviews, and traffic sources
- Numbers about members, such as signups over time
- Comments
- Uploading images and files
For how each post performed, such as email opens, clicks, and signups, declare post:read_stats and use /posts/?include=email,count.clicks,count.signups (see Which scope a call needs).
When a call fails
When a call can’t go through, it rejects with a GhostAppError. Its code says why, such as not_framed when someone opens your app outside Ghost Admin. The SDK catches some problems before anything reaches Ghost, such as a full URL passed to fetch.
fetch resolves with Ghost’s response whatever its status, so a missing post or a failed save comes back as a response with response.ok set to false. It rejects only when the request can’t be made.
Troubleshooting says what to do about the common codes, and the SDK reference lists them all.
Signing and your server
The same package has parts for the rest of your app. Import each from its own path, so your page never loads them:
| Part | What it does |
|---|---|
@tryghost/app-sdk/vite |
ghostApp(), a Vite plugin that creates your app’s keys and signs ghost-app.json in vite dev and vite build. |
@tryghost/app-sdk/backend |
For your app’s server: withGhostApp() answers Ghost’s challenge and passes everything else to your own fetch (or handleChallenge() answers it from a framework’s own route), and allowFraming() lets Ghost Admin show your page. |
ghost-app |
A command for your app’s folder: keys creates keys, sign signs the manifest, and backend-key pipes the backend key into your host’s secrets. Run it as pnpm exec ghost-app from a project with the SDK installed. |
@tryghost/app-sdk/tooling |
What the plugin and the command are built on, for your own scripts. |
Manifest explains the keys, and Deployment how to use them on a host. The SDK’s README covers every option.