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.
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:
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.
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 story
→ summary
. Run the graph by calling substrate.run
.
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.
substrate.stream
Get the output
After running the graph, use response.get
to retrieve the output of a node.