Overview
Get started
֍
Set up the Substrate SDK

Install the SDK

The official Substrate SDKs are the recommended way to interact with Substrate from TypeScript, JavaScript, or Python.

We plan to generate idiomatic SDKs for the Substrate protocol in every popular language. Let us know (opens in a new tab) which languages to prioritize next.

pip install substrate

Initialize Substrate

Substrate uses API keys to authenticate requests. Create an API key in your Substrate Dashboard. Then, import and initialize the Substrate client with your API key.

Python
TypeScript

from substrate import Substrate, ComputeText, sb
substrate = Substrate(api_key=YOUR_API_KEY)

⚠️

Your API keys carry the ability to make requests using your Substrate account, so be sure to keep them secure. Do not share your API keys in areas such as publicly-accessible client-side code, social media, or public GitHub repos.

Create a graph

Generate a story with a language model using ComputeText. Then, summarize the story by passing the future output of the story to a summary node.

Python
TypeScript

story = ComputeText(prompt="tell me a story")
summary = ComputeText(prompt=sb.concat("summarize this story in one sentence: ", story.future.text))

Because story has not been run yet, we must use sb.concat to create a prompt using a string from its future output.

Run the graph

We've created a simple graph chaining storysummary. Run the graph by calling substrate.run.

Python
TypeScript

response = substrate.run(summary);

Substrate automatically finds the upstream dependencies for nodes.

  • You must pass all terminal nodes to substrate.run.
  • Passing upstream nodes is optional.
  • You may pass disconnected nodes (or multiple graphs) to run them in parallel.

Get the output

After running the graph, use response.get to retrieve the output of any of the nodes in the graph.

Python
TypeScript

summary_out = response.get(summary)
print(summary_out.text)

If you want to combine the outputs of multiple nodes into a single response, you can use a Box:

Python
TypeScript

# ...
box = Box(
value={
"story": story.future.text,
"summary": summary.future.text,
}
)
res = substrate.run(box)

You can also stream outputs.