Skip to content

SubstrateLabs/substrate-typescript

Repository files navigation

Substrate TypeScript SDK

NPM version

The Substrate TypeScript SDK is the recommended way to interact with the Substrate API from server-side TypeScript or JavaScript.

Documentation

If you're just getting started, head to guides.substrate.run.

For a detailed API reference covering the nodes available on Substrate, see substrate.run/nodes.

For an interactive reference, check out explore.substrate.run. You can call Substrate.visualize(...nodes...) to generate an interactive visualization of any graph.

Installation

npm install substrate

Usage

import { Substrate, GenerateText, sb } from "substrate";

Initialize the Substrate client.

const substrate = new Substrate({ apiKey: SUBSTRATE_API_KEY });

Generate a story using the GenerateText node.

const story = new GenerateText({ prompt: "tell me a story" });

Summarize the output of the story node using another GenerateText node. Because story has not yet been run, we use sb.interpolate to work with its future output.

const summary = new GenerateText({
  prompt: sb.interpolate`summarize this story in one sentence: ${story.future.text}`,
});

Run the graph chaining storysummary. This is a simple example, but you can easily build arbitrarily complex branching workflows.

const response = await substrate.run(story, summary);

Get the output of the summary node by passing it to response.get.

const summaryOut = response.get(summary);
console.log(summaryOut.text);
// Princess Lily, a kind-hearted young princess, discovers a book of spells and uses it to grant her family and kingdom happiness.