Skip to content
Ghost Apps Preview

SDK reference

Generated from @tryghost/app-sdk 0.1.0-alpha.28. To learn what the SDK is for and how to use it, read App SDK.

Functions

connect()

function connect(options?): Promise<Ghost>;

Connects to Ghost Admin, which must frame the page, and resolves once Admin has started the app. Safe to call as soon as the page loads.

Every call shares one connection, and the first call’s options apply. Only one copy of the SDK in a page can connect. Any other copy rejects with duplicate_sdk. Admin answers each document only once, so after it answers, that connection is the page’s only one. Once it fails or its session ends, every call rejects with the error it ended with, and only a new document (after a reload, for example) can connect again. A failure before Admin answered, such as a bad option or a timeout, can be retried.

In the app’s overlay, the copy of its page that its dialogs render into, it never resolves.

Parameters

Parameter Type
options? ConnectOptions

Returns

Promise<Ghost>

Interfaces

ConnectOptions

Properties

Property Type Description
timeout? number How long to wait for Admin to start the app, in milliseconds. Defaults to 10 seconds.

GhostFetchInit

Like RequestInit, for the requests Admin can make on the app’s behalf.

Properties

Property Type Description
method? string Any case, sent in uppercase. Defaults to GET.
headers? HeadersInit Anything Headers accepts, except the headers only Admin sets. The SDK refuses those, such as authorization, cookie and content-type.
body? string | JsonBody | null Always JSON: text is sent as it is, and an object or array is sent as JSON.

GhostPath

The app’s route, mirrored in Admin’s URL. The SDK keeps it in step with the page’s own history: the app routes with history.pushState and links as it would anywhere, and the SDK reports each move to Admin, which keeps the history.

Properties

Property Modifier Type Description
current readonly string The app’s route: the path, query and fragment of the page’s URL, such as /episodes/12, in the URL parser’s normalized form, as Admin stores it (/search?q=a%20b, .. resolved).

OverlayHold

A dialog or sheet’s hold on the overlay, from when it mounts until it unmounts.

Methods

setOpen()
setOpen(open): void;

True from when the dialog opens, false once it starts to close.

Parameters
Parameter Type
open boolean
Returns

void

release()
release(): void;

Called once the dialog has unmounted, after its closing animation.

Returns

void


GhostOverlay

Where the app’s dialogs and sheets render to cover all of Admin: the app’s page, loaded a second time over the whole window. In it, --ghost-safe-area-inset-* are measured from the window’s edges.

That copy never connects, but code that runs before connect() resolves runs there too, so start fetches, analytics and the like once connect() has resolved.

Methods

container()
container(): HTMLElement | undefined;

The element to render a dialog or sheet into, or undefined to render it in the page while the overlay is still loading.

Returns

HTMLElement | undefined

hold()
hold(tint, element): OverlayHold;

Called by a dialog or sheet rendered into container() when it mounts, with its role="dialog" element.

Parameters
Parameter Type
tint "dialog" | "sheet"
element HTMLElement
Returns

OverlayHold


Ghost

The connection to Ghost, once Admin has started the app.

Admin’s chrome floats over the edges of the page. The SDK sets how far in to keep content, in pixels, as --ghost-safe-area-inset-top, -right, -bottom and -left on the page’s root element, and keeps them up to date. Shade’s ShadeApp pads the app by them already.

Properties

Property Modifier Type Description
site readonly Site The site the app is running in: its URL, title, timezone and locale.
user readonly User The staff user using the app, by ID and role.
theme readonly Readonly<Theme> The theme Admin shows now. mode is always light or dark.
session readonly Session The installation’s revision when this session started.
path readonly GhostPath The app’s route, kept in step with Admin’s URL.
connected readonly boolean False once the session has ended. Every call then rejects with session_ended.
overlay readonly GhostOverlay Where dialogs and sheets render to cover all of Admin. Pass it to ShadeApp.

Methods

onThemeChange()
onThemeChange(callback): Unsubscribe;

Registers a callback that runs when the person using the app switches Admin’s theme.

Parameters
Parameter Type
callback (theme) => void
Returns

Unsubscribe

onPathChange()
onPathChange(callback): Unsubscribe;

Registers a callback that runs when Admin moves the app to another of its routes: Back or forward in Admin’s history, or one of Admin’s own links. The SDK has already put the page’s URL there and dispatched popstate, which a router follows. A multi-page app, with no router to render the new address, loads it: location.replace(path). It doesn’t run for the app’s own moves.

Parameters
Parameter Type
callback (path) => void
Returns

Unsubscribe

onSessionEnd()
onSessionEnd(callback): Unsubscribe;

Registers a callback that runs once when this document leaves the app session, because the page reloads, posts a form or follows a link, or when the app session itself ends. It runs straight away if that has already happened.

Parameters
Parameter Type
callback (error) => void
Returns

Unsubscribe

fetch()
fetch(path, init?): Promise<Response>;

Calls the Admin API as the signed-in staff user. path is relative to the Admin API root, such as /posts/?limit=5, never a URL. Resolves with a real Response whatever its status, and rejects only when Admin refuses to make the request.

Parameters
Parameter Type
path string
init? GhostFetchInit
Returns

Promise<Response>

open()
open(place): Promise<void>;

Sends the person using the app to a place in Admin, such as { post: { id } }.

Parameters
Parameter Type
place Place
Returns

Promise<void>

Classes

GhostAppError

The only error type the SDK rejects with, so an app can branch on code.

Extends

  • Error

Properties

Property Modifier Type
code readonly GhostAppErrorCode

Constructors

Constructor
new GhostAppError(
code,
message,
options?
): GhostAppError;
Parameters
Parameter Type
code GhostAppErrorCode
message string
options? ErrorOptions
Returns

GhostAppError

Overrides

Error.constructor

Type Aliases

OverlayTint

type OverlayTint = typeof OVERLAY_TINTS[number];

Theme

type Theme = {
mode: typeof THEME_MODES[number];
accent_color: string;
};

The theme Admin is showing, so the page can match it. mode is light or dark, never system.

Properties

Property Type
mode typeof THEME_MODES[number]
accent_color string

StartContext

type StartContext = {
site: {
url: string;
title: string;
timezone: string;
locale: string;
};
user: {
id: string;
role: string;
};
theme: Theme;
session: {
revision: number;
};
path: string;
layout: Layout;
};

What Admin sends an app’s page when the session starts. An older SDK ignores fields a newer Admin adds rather than refusing them, so they don’t stop it from starting.

Properties

Property Type Description
site { url: string; title: string; timezone: string; locale: string; } -
site.url string -
site.title string -
site.timezone string -
site.locale string -
user { id: string; role: string; } Who is using the app, by ID and role only. Name and email are left out on purpose: an app that needs more about a person reads it from the API, through /users/?filter=id:…, where the app’s permissions will apply.
user.id string -
user.role string -
theme Theme -
session { revision: number; } The installation’s revision when this session started. Admin ends the session when the installation’s revision changes, so an app never runs against an unapproved revision.
session.revision number -
path string The app’s route, taken from Admin’s URL, so a reload or a shared link opens the same page: the path, query and fragment of the frame’s URL on the app’s origin. Admin loads a new frame at it, so it’s where the page already is, unless the page’s server redirected it.
layout Layout Sent again with layoutChanged whenever it changes.

Place

type Place =
| {
post: {
id: string;
};
}
| {
page: {
id: string;
};
}
| {
member: {
id: string;
};
}
| {
apps: true;
};

A place in Admin an app can send the publisher to: a post, a page or a member by its ID, or the apps list.


GhostAppErrorCode

type GhostAppErrorCode =
| "not_framed"
| "not_connected"
| "duplicate_sdk"
| "unsupported"
| BridgeErrorCode;

Why an SDK call failed:

  • not_framed: The page isn’t framed in Admin, so there’s nothing to connect to.
  • not_connected: Admin didn’t start the app in time.
  • duplicate_sdk: Another copy of the SDK in the page has already connected, or tried to, because the page loads the SDK more than once. Load a single copy for the whole page.
  • invalid_context: Admin started the app with a context this SDK can’t read. Updating the SDK usually fixes it.
  • session_ended: This document has left the app session, usually because the page reloaded or navigated away, or the session has ended. Every call fails after that.
  • invalid_argument: The app passed something that can’t be sent.
  • invalid_path: A path that isn’t an Admin API path or a route within the app, or one the Admin API only redirects, such as a path without its trailing /.
  • unsupported: This site’s version of Ghost doesn’t have the call the app made yet. The SDK knows this from the calls Admin listed when it connected.
  • blocked: Admin refused by policy, such as a request the allowlist doesn’t allow.
  • network_error: Admin couldn’t reach its own API.
  • rejected: Admin refused for another reason. The message says why.

not_framed, not_connected, duplicate_sdk and unsupported are the SDK’s own codes. The rest are bridge codes, which cross the channel.


Site

type Site = Readonly<StartContext["site"]>;

The site the app is running in.


User

type User = Readonly<StartContext["user"]>;

Who is using the app, by ID and role. Read anything more, such as their name, through ghost.fetch(`/users/?filter=id:${ghost.user.id}`): /users/:id/ isn’t allowed.


Session

type Session = Readonly<StartContext["session"]>;

The session with Admin: the installation’s revision when the session started.


JsonBody

type JsonBody = Record<string, unknown> | readonly unknown[];

A body sent as JSON: a plain object or an array.


Unsubscribe

type Unsubscribe = () => void;

Returns

void