Ghost Server API Client

7.0.0 · active · verified Tue Apr 21

The `castle-api-client` package functions as the official JavaScript/TypeScript client for interacting with the `ghost-server` API. It implements a custom JSON RPC-like protocol, making HTTP POST requests to a single endpoint (`/api`) with a JSON body containing `method` and `args` fields. This design enables a unified and simplified communication interface. The client is highly versatile, supporting a broad spectrum of JavaScript environments, including Node.js, modern web browsers, Electron applications, and React Native projects. Its primary function is centered around user identity and authentication, relying on deep integration with Expo user accounts. This strategic reliance streamlines authentication workflows for Expo-centric applications. A key differentiator for `castle-api-client` is its architectural independence: it was specifically developed to decouple `ghost-server` from larger, more monolithic web stacks, allowing for faster development cycles and greater agility in evolving its features. This client is currently at stable version 7.0.0. While no specific release cadence is publicly detailed, its design emphasizes responsiveness to evolving needs, offering a lightweight and efficient solution for secure, cross-platform communication within the Expo ecosystem, particularly beneficial for projects needing robust login and identity management without entanglement in complex legacy systems.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing the Ghost API client, performing a hypothetical login using an Expo token, and subsequently fetching user profile information via the generic `call` method, handling potential errors.

import { GhostApiClient } from 'castle-api-client';

async function runGhostClientExample() {
  const expoAuthToken = process.env.EXPO_AUTH_TOKEN ?? ''; // Securely retrieve your Expo auth token

  if (!expoAuthToken) {
    console.warn("EXPO_AUTH_TOKEN environment variable is not set. API calls requiring authentication may fail.");
  }

  // Initialize the client with the server URL
  const client = new GhostApiClient({
    serverUrl: 'https://ghost-server.app.render.com/api',
    // Further configuration options like custom fetch, timeouts, etc. can be added here
  });

  try {
    console.log('Attempting to log in with Expo token...');
    // The API uses a generic 'call' method with 'method' and 'args' fields
    const loginResponse = await client.call({
      method: 'auth.loginWithExpoToken', // Example: a hypothetical login method
      args: { token: expoAuthToken },
    });

    if (loginResponse.error) {
      console.error('Login failed:', loginResponse.error.message, loginResponse.clientError);
      return;
    }

    console.log('Login successful. Result:', loginResponse.result);
    const sessionToken = loginResponse.result?.sessionToken; // Assuming session token is returned

    if (sessionToken) {
      console.log('Fetching user profile...');
      // Make another call using the obtained session token
      const userProfile = await client.call({
        method: 'user.getProfile', // Example: a hypothetical method to get user profile
        args: { sessionToken: sessionToken },
      });

      if (userProfile.error) {
        console.error('Failed to get user profile:', userProfile.error.message);
        return;
      }
      console.log('User Profile:', userProfile.result);
    }

  } catch (error) {
    console.error('An unexpected client error occurred:', error instanceof Error ? error.message : String(error));
  }
}

runGhostClientExample();

view raw JSON →