A TypeScript library to easily access the Stability AI REST API.
yarn add stability-ai
npm i stability-ai
import StabilityAI from 'stability-ai';
const stability = new StabilityAI(process.env.STABILITY_AI_API_KEY);
- Generate - Core
- Generate - SD3
- Upscale - Creative
- Edit - Inpaint
- Edit - Outpaint
- Edit - Search and Replace
- Edit - Remove Background
const { email, id, organizations, profile_picture } = await stability.v1.user.account()
console.log('User email:', email);
console.log('User id:', id);
console.log('User organizations:', organizations);
if (profile_picture) console.log('User profile picture:', profile_picture);
const { credits } = await stability.v1.user.balance()
console.log('User credits balance:', credits);
const engines = await stability.v1.engines.list()
console.log('Engine list:', engines);
const results = await stability.v1.generation.textToImage(
'stable-diffusion-xl-beta-v2-2-2',
[
{ text: 'a man on a horse', weight: 0.5 }
]
)
for (const result of results) {
console.log('Text to image result filepath:', result.filepath);
}
const results = await stability.v1.generation.imageToImage(
'stable-diffusion-xl-beta-v2-2-2',
[
{ text: 'crazy techincolor surprise', weight: 0.5 }
],
'https://www.example.com/images/your-image.jpg'
)
for (const result of results) {
console.log('Image to image result filepath:', result.filepath);
}
const results = await stability.v1.generation.imageToImageUpscale(
'https://www.example.com/images/your-image.jpg',
{
type: 'esrgan'
}
)
for (const result of results) {
console.log('Image to image upscale result filepath:', result.filepath);
}
const results = await stability.v1.generation.imageToImageMasking(
'stable-diffusion-xl-beta-v2-2-2',
[
{ text: 'a beautiful ocean', weight: 0.5 }
],
'https://www.example.com/images/your-image-with-alpha-channel.png',
{
mask_source: 'INIT_IMAGE_ALPHA'
}
)
for (const result of results) {
console.log('Image to image masking result filepath:', result.filepath);
}
const result = await stability.v2beta.stableVideo.imageToVideo(
'https://www.example.com/images/photo-you-want-to-move.png'
);
let filepath: string | undefined = undefined;
while (!filepath) {
const videoResult = await stability.v2beta.stableVideo.imageToVideoResult(
result.id,
);
if ('filepath' in videoResult) {
filepath = videoResult.filepath;
} else if (
'status' in videoResult &&
videoResult.status === 'in-progress'
) {
await new Promise((resolve) => setTimeout(resolve, 2500));
}
}
console.log('Stable Video Image to Video result filepath:', filepath);
const result = await stability.v2beta.stableImage.generate.core('a beautiful ocean');
console.log('Stable Image Generate Core result filepath:', result.filepath);
const result = await stability.v2beta.stableImage.generate.sd3('a very beautiful ocean');
console.log('Stable Image Generate SD3 result filepath:', result.filepath);
const result = await stability.v2beta.stableImage.upscale.creative(
'https://www.example.com/images/photo-you-to-4k-upscale.png',
'UHD 4k',
);
let filepath: string | undefined = undefined;
while (!filepath) {
const upscaleResult = await stability.v2beta.stableImage.upscale.creativeResult(
result.id,
result.outputFormat,
);
if ('filepath' in upscaleResult) {
filepath = upscaleResult.filepath;
} else if (
'status' in upscaleResult &&
upscaleResult.status === 'in-progress'
) {
await new Promise((resolve) => setTimeout(resolve, 2500));
}
}
console.log('Stable Image Upscale Creative result filepath:', filepath);
const result = await stability.v2beta.stableImage.edit.inpaint(
'https://www.example.com/images/your-image-of-the-earth.png',
'disco ball',
);
console.log('Stable Image Edit Inpaint result filepath:', result.filepath);
const result = await stability.v2beta.stableImage.edit.outpaint(
'https://www.example.com/images/your-image-of-the-earth.png',
{
prompt: 'outer space',
left: 100
}
);
console.log('Stable Image Edit Outpaint result filepath:', result.filepath);
const result = await stability.v2beta.stableImage.edit.searchAndReplace(
'https://www.example.com/images/your-image-of-the-earth.png',
'a disco ball',
'the earth'
);
console.log('Stable Image Edit Search And Replace result filepath:', result.filepath);
const result = await stability.v2beta.stableImage.edit.removeBackground(
'https://www.example.com/images/your-image-of-the-earth.png',
);
console.log('Stable Image Edit Remove Background result filepath:', result.filepath);
Built in TypeScript, tested with Jest.
$ yarn install
$ yarn test
Road Map
- Support local files
- Support output to S3/GCS bucket
- Wrap job/result methods into one async task w/ internal polling