Photon OS Docs
Creating Apps

Get Started Creating Apps

Build apps for Photon OS using the SDK

Photon OS apps are web applications that run in sandboxed iframes within the OS shell. Apps communicate with the OS through a message-passing API, abstracted by the SDK.

Getting Started

While you can use any web framework, React is recommended as it pairs well with the @photon-os/react package which provides useful hooks.

Create a New Project

npm create vite@latest my-photon-app -- --template react-ts
cd my-photon-app

Install the SDK

npm install @photon-os/sdk @photon-os/react

Using the SDK

Initializing

Create an instance of the OS class to access Photon OS features:

import { OS } from "@photon-os/sdk";

const os = new OS();

The SDK automatically connects to the parent Photon OS shell via PostMessage.

Available Managers

The OS instance provides access to several managers:

ManagerPurpose
os.userGet current user information
os.appsApp lifecycle (get installed, launch, install, uninstall)
os.prefsStore preferences (sandboxed per-app or shared)
os.accountsSecond Life account linking
os.devicesCommunicate with Second Life devices

Getting the Current User

const user = await os.user.getCurrentUser();
console.log(`Hello, ${user.displayName}!`);

Using Preferences

Store app-specific data that persists across sessions:

// Save a preference (sandboxed to your app)
await os.prefs.set("theme", "dark");

// Read it back
const theme = await os.prefs.get("theme");

// Delete it
await os.prefs.delete("theme");

For data that should be accessible to all apps:

// Shared preferences (all apps can read/write)
await os.prefs.setShared("globalSetting", true);
const value = await os.prefs.getShared("globalSetting");

Send the user back to the OS home screen:

await os.homeButton();

React Integration

The @photon-os/react package provides hooks for common operations.

useInstalledApps

Fetch and monitor installed apps:

import { useInstalledApps } from "@photon-os/react";

function MyComponent() {
  const { installedApps, loading, error, refresh } = useInstalledApps();

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error.message}</div>;

  return (
    <ul>
      {installedApps.map((app) => (
        <li key={app.bundleId}>{app.name}</li>
      ))}
    </ul>
  );
}

App Definition

When installing an app, you need to provide:

FieldDescriptionExample
bundleIdUnique identifier for your appcom.mycompany.myapp
nameDisplay name shown to usersMy App
authorDeveloper or company nameMy Company
urlURL where your app is hostedhttps://example.com/app

Installing Your App

To install your app in Photon OS:

  1. Deploy your app to a publicly accessible URL
  2. Open Settings in Photon OS
  3. Go to the Apps section
  4. Click Install App
  5. Fill in the app details:
    • App URL: The URL where your app is hosted
    • App Name: Display name for your app
    • Author: Your name or company
    • Bundle ID: A unique identifier (e.g., com.yourname.appname)
  6. Click Install

Your app will now appear in the Launcher.

Example App

Here's a minimal example that displays the current user and saves a preference:

import { useEffect, useState } from "react";
import { OS, type PhotonUser } from "@photon-os/sdk";

const os = new OS();

function App() {
  const [user, setUser] = useState<PhotonUser | null>(null);
  const [count, setCount] = useState(0);

  useEffect(() => {
    // Load user and saved count on mount
    async function init() {
      const currentUser = await os.user.getCurrentUser();
      setUser(currentUser);

      const savedCount = await os.prefs.get("count");
      if (typeof savedCount === "number") {
        setCount(savedCount);
      }
    }
    init();
  }, []);

  const increment = async () => {
    const newCount = count + 1;
    setCount(newCount);
    await os.prefs.set("count", newCount);
  };

  return (
    <div>
      <h1>Hello, {user?.displayName ?? "Loading..."}!</h1>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

export default App;

Type Definitions

The SDK exports TypeScript types for all data structures:

import type {
  PhotonUser,
  AppDefinition,
  SLDevice,
  DeviceMessage,
  PreferenceValue,
} from "@photon-os/sdk";

Next Steps