Skip to main content
The gRPC API is in legacy maintenance mode and won’t receive new features. For new projects, use the REST v2beta API.
This code is for interacting with our gRPC API via TypeScript. We’ll be using Stability’s pre-generated client, which was created with protoc.

Note to Developers

We are aware that this is not a great experience for TypeScript developers at the moment. A real TypeScript SDK that will be far more robust, user-friendly, and feature-rich is in the works. These examples target the NodeJS environment, but they can be easily adapted to the browser. While complete examples for the browser are outside the scope of this documentation, any snippet of code performing a NodeJS-specific action will have a comment indicating how one might adapt it for use in the browser.

Getting an API Key

If you’re not signed up you’ll need to do that first, so please click the login button in the top right. Then you can find your API key on the account page.

1. Copy the generated client code into your repo…

We’ll start by directly copying Stability’s pre-generated .js & .ts files. Here’s what they look like in a typical repository… Here's what they look like in a typical repository...

2. Install the dependencies…

Install these using your package manager of choice:
  • @improbable-eng/grpc-web
  • @improbable-eng/grpc-web-node-http-transport (NodeJS only)
  • google-protobuf
  • grpc

3. Initialize the client…

The examples under the Features section assume the following is in scope:
The above client and metadata variables will need to be in scope for the examples under the Features section to work. Whether that means these variables are declared in the same file they’re used or exported in a separate file is up to you.

4. Add the helper functions…

Dealing with gRPC directly can be a bit cumbersome, so we’ve created a few helper functions to make things easier. The code samples in this documentation will use these helpers and will assume they reside in a file in the same directory called helpers.ts. Please paste the following into helpers.ts:
Please read through the above code and make sure you understand what it’s doing. The majority of the code is boilerplate that probably won’t change, but near the bottom there’s a function called onGenerationComplete that you’ll need to modify to suit your needs. It currently writes the resulting images to disk, for example.

You’re ready for generation!