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 the 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.

External providers

Substrate supports connecting to external providers like OpenAI and Anthropic. (Let us know (opens in a new tab) if you want us to support other providers). To use external providers, provide secrets when initializing the Substrate client:

Python
TypeScript

from substrate import Substrate, ComputeText, sb, Secrets
substrate = Substrate(
api_key=SUBSTRATE_API_KEY,
secrets=Secrets(
openai="OPENAI_API_KEY",
anthropic="ANTHROPIC_API_KEY"),
)

Connect tasks

When using Substrate, you can connect multiple tasks (called nodes) and then run the entire workload (a graph). Substrate comes with a large library of built-in nodes (opens in a new tab). It's easy to run individual nodes, or connect multiple nodes into chains or branching workflows.


In this starter example, we generate a story with a language model using ComputeText. Then, we 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.format("summarize: {story}", story=story.future.text))

We use sb functions to transform the future output of story (story.future.text)

Run the graph

We've created a simple two-node 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.
You can stream outputs using substrate.stream

Get the output

After running the graph, use response.get to retrieve the output of a node.

Python
TypeScript

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