Skip to main content
torch.js has not been released yet.
torch.js logotorch.js logotorch.js
PlaygroundContact
Login
Documentation
IntroductionType SafetyTensor ExpressionsTensor IndexingEinsumEinopsAutogradTraining a ModelProfiling & MemoryPyTorch MigrationBest PracticesRuntimesPerformancePyTorch CompatibilityBenchmarksDType Coverage
torch.js· 2026
LegalTerms of UsePrivacy Policy
/
/
  1. docs
  2. Spark
  3. spark
  4. SparkClientApi

spark.SparkClientApi

export interface SparkClientApi {
  /**
   * Connect to a worker and get reactive bindings.
   *
   * Only the first call matters - subsequent calls return the same worker.
   *
   * @template T - Type of bindings exposed by torch function
   * @param torchFn - The torch function to run in the worker
   * @returns Proxy for accessing exposed values and calling functions
   */
  use<T extends Record<string, unknown>>(torchFn: () => void | Promise<void>): SparkProxy<T>;

  /**
   * Load a file from torchjs.org as ArrayBuffer.
   *
   * Path format: "username/project/filename"
   * No authentication required.
   *
   * @param path - File path
   * @returns File contents as ArrayBuffer
   * @throws If file not found
   */
  load(path: string): Promise<ArrayBuffer>;

  /**
   * Load a JSON file from torchjs.org.
   *
   * Path format: "username/project/filename"
   *
   * @template T - Type of parsed JSON
   * @param path - File path
   * @returns Parsed JSON data
   * @throws If file not found or invalid JSON
   */
  loadJSON<T = unknown>(path: string): Promise<T>;

  /**
   * Load a text file from torchjs.org.
   *
   * Path format: "username/project/filename"
   *
   * @param path - File path
   * @returns File contents as string
   * @throws If file not found
   */
  loadText(path: string): Promise<string>;

  /**
   * Save a file to torchjs.org.
   *
   * Path format: "username/project/filename"
   * Requires authentication (proxied to parent window).
   *
   * @param path - File path
   * @param data - Data to save (ArrayBuffer, Blob, or string)
   * @param options - Save options (progress callback, etc.)
   * @throws If not authenticated or save fails
   */
  save(path: string, data: ArrayBuffer | Blob | string, options?: SparkSaveOptions): Promise<void>;

  /**
   * Save JSON data to a file on torchjs.org.
   *
   * Path format: "username/project/filename"
   * Requires authentication.
   *
   * @param path - File path (should end with .json)
   * @param data - Object to serialize as JSON
   * @throws If not authenticated or save fails
   */
  saveJSON(path: string, data: unknown): Promise<void>;

  /**
   * Check if a file exists on torchjs.org.
   *
   * Path format: "username/project/filename"
   *
   * @param path - File path
   * @returns True if file exists
   */
  exists(path: string): Promise<boolean>;

  /**
   * Delete a file from torchjs.org.
   *
   * Path format: "username/project/filename"
   * Requires authentication.
   *
   * @param path - File path
   * @throws If not authenticated or delete fails
   */
  delete(path: string): Promise<void>;

  /**
   * Save data to local IndexedDB storage.
   *
   * Persists across page reloads. Useful for checkpoints and models.
   *
   * @param key - Unique key for this data
   * @param data - Data to save
   * @throws If IndexedDB not available or quota exceeded
   */
  saveLocal(key: string, data: ArrayBuffer): Promise<void>;

  /**
   * Load data from local IndexedDB storage.
   *
   * @param key - Key to load
   * @returns Data or null if not found
   * @throws If IndexedDB not available
   */
  loadLocal(key: string): Promise<ArrayBuffer | null>;

  /**
   * Delete data from local IndexedDB storage.
   *
   * @param key - Key to delete
   * @throws If IndexedDB not available
   */
  deleteLocal(key: string): Promise<void>;

  /**
   * List all keys in local IndexedDB storage.
   *
   * @returns Array of keys
   * @throws If IndexedDB not available
   */
  listLocal(): Promise<string[]>;
}
use((torchFn: () => void | Promise<void>) => SparkProxy<T>)
– Connect to a worker and get reactive bindings. Only the first call matters - subsequent calls return the same worker.
load((path: string) => Promise<ArrayBuffer>)
– Load a file from torchjs.org as ArrayBuffer. Path format: "username/project/filename" No authentication required.
loadJSON((path: string) => Promise<T>)
– Load a JSON file from torchjs.org. Path format: "username/project/filename"
loadText((path: string) => Promise<string>)
– Load a text file from torchjs.org. Path format: "username/project/filename"
save((path: string, data: ArrayBuffer | Blob | string, options?: SparkSaveOptions) => Promise<void>)
– Save a file to torchjs.org. Path format: "username/project/filename" Requires authentication (proxied to parent window).
saveJSON((path: string, data: unknown) => Promise<void>)
– Save JSON data to a file on torchjs.org. Path format: "username/project/filename" Requires authentication.
exists((path: string) => Promise<boolean>)
– Check if a file exists on torchjs.org. Path format: "username/project/filename"
delete((path: string) => Promise<void>)
– Delete a file from torchjs.org. Path format: "username/project/filename" Requires authentication.
saveLocal((key: string, data: ArrayBuffer) => Promise<void>)
– Save data to local IndexedDB storage. Persists across page reloads. Useful for checkpoints and models.
loadLocal((key: string) => Promise<ArrayBuffer | null>)
– Load data from local IndexedDB storage.
deleteLocal((key: string) => Promise<void>)
– Delete data from local IndexedDB storage.
listLocal(() => Promise<string[]>)
– List all keys in local IndexedDB storage.

Spark API for React components in the iframe.

These are the functions available to React components that use the spark.use() hook. They provide file I/O and local storage access.

Previous
SparkCallMessage
Next
SparkController