Stability Rest Client
This is a rest client to interface with Stability.ai's API.
As this requires an API key, server-side usage is recommended.
⚠️ This is a work in progress. Stability.ai Rest API is still in beta and is subject to change.
Installation
$ npm install stability-rest
Usage
Typescript
import {
ClipGuidancePreset,
GenerationSampler,
StabilityClient,
WellKnownEngine,
} from "stability-rest";
import * as fs from "fs";
const client = new StabilityClient(process.env.STABILITY_API_KEY);
client.generation
.textToImagePng(
WellKnownEngine["stable-diffusion-512-v2-1"],
"a pale blue planet in the depth of the space",
{
width: 512,
height: 512,
cfg_scale: 7.0,
sampler: GenerationSampler.K_EULER_ANCESTRAL,
clip_guidance_preset: ClipGuidancePreset.FAST_BLUE,
steps: 20,
samples: 1,
seed: 10,
}
)
.then((response) => {
fs.writeFileSync("out.png", response.data);
});
Javascript
const {
StabilityClient,
WellKnownEngine,
GenerationSampler,
ClipGuidancePreset,
} = require("stability-rest");
const fs = require("fs");
const client = new StabilityClient(process.env.STABILITY_API_KEY);
client.generation
.textToImagePng(
WellKnownEngine["stable-diffusion-512-v2-1"],
"a pale blue planet in the depth of the space",
{
width: 512,
height: 512,
seed: 123,
cfg_scale: 7.0,
sampler: GenerationSampler.K_EULER_ANCESTRAL,
clip_guidance_preset: ClipGuidancePreset.FAST_BLUE,
steps: 20,
samples: 1,
}
)
.then((response) => {
fs.writeFileSync("out.png", response.data);
});
API
This client maps 1-1 with the Stability API.
For more information, please refer to the API documentation.
const client = new StabilityClient(process.env.STABILITY_API_KEY);
client.user.account();
client.user.balance();
client.engine.list();
client.generation.textToImage(engineId, prompt, options);
client.generation.textToImagePng(engineId, prompt, options);
Helpers
getArtifactBuffer(artifact)
Convert an artifact base64 string to a buffer.
WellKnownEngine
A list of well-known engines. This might be outdated as it is
statically defined. You can also call StablityClient.engine.list()
to get a list of all engines.
Contributing
Contributions are welcome! Please open an issue or PR. There are no
guidelines yet, but please follow the existing code style.
TODO
List of things that need to be done:
Environment Setup
- Clone the repo
- Install dependencies 3. Create a
.env
file with your API key. Set the key as STABILITY_API_KEY
. - Make contributions
- Run tests [1]
$ npm run test
Then create a PR once all the tests pass.
Important Notes on Tests 1
We do not mock the API calls in the tests. This is to ensure that
the client is working as expected. However, this means that you
need to have a valid API key to run the tests.
⚠️ Please do not use your production API key to run the tests.
You can create a new API key in DreamStudio.
Running the tests might consume your API quota.
License
MIT © Aditya Purwa