What is Stability AI API?
The Stability AI API provides developers with programmatic access to Stability AI’s suite of powerful generative models, most notably the Stable Diffusion family for image generation. It allows for the integration of cutting-edge text-to-image, image-to-image, and other media generation capabilities directly into applications, services, and workflows. By offering a simple REST API, it abstracts the complexity of hosting and running these large-scale models, enabling developers to focus on creating innovative features.
Key Features
- Text-to-Image Generation: Create high-quality, detailed images from simple text prompts using models like Stable Diffusion 3.
- Image-to-Image Transformation: Modify existing images based on text prompts, allowing for powerful editing and style transfer.
- Inpainting & Outpainting: Edit specific parts of an image by masking an area to regenerate (inpainting) or extend the original canvas (outpainting).
- Image Upscaling: Increase the resolution of generated or existing images while enhancing detail.
- Access to Multiple Models: The API provides access to a variety of models, each with different strengths and artistic styles.
- SDKs and REST API: Offers official SDKs for Python and TypeScript/JavaScript, alongside a well-documented REST API for universal access.
Use Cases
- Creative Content Generation: Artists and designers can generate unique visuals, concept art, and illustrations.
- Marketing and Advertising: Create custom ad creatives, social media content, and product imagery at scale.
- Gaming and Entertainment: Generate in-game assets, character portraits, and environmental textures.
- Application Prototyping: Quickly visualize UI/UX concepts and application features.
- Personalized Media: Develop services that generate personalized images for users, such as avatars or custom artwork.
Getting Started
Here is a simple “Hello World” example using Python to generate an image from a text prompt. First, make sure you have the requests library installed (pip install requests) and have obtained an API key from the Stability AI Platform.
```python import requests import base64 import os
Your Stability AI API key
api_key = “YOUR_STABILITY_API_KEY”
API endpoint for text-to-image
url = “https://api.stability.ai/v1/generation/stable-diffusion-v1-6/text-to-image”
Request body
body = { “steps”: 40, “width”: 512, “height”: 512, “seed”: 0, “cfg_scale”: 5, “samples”: 1, “text_prompts”: [ { “text”: “A stunning photograph of a futuristic city at sunset, cinematic lighting”, “weight”: 1 }, { “text”: “blurry, bad art, ugly, text”, “weight”: -1 } ], }
Headers
headers = { “Accept”: “application/json”, “Content-Type”: “application/json”, “Authorization”: f”Bearer {api_key}”, }
Make the API request
response = requests.post( url, headers=headers, json=body, )
if response.status_code != 200: raise Exception(“Non-200 response: “ + str(response.text))
data = response.json()
Save the generated image
for i, image in enumerate(data[“artifacts”]): with open(f’./out/txt2img_{image[“seed”]}.png’, “wb”) as f: f.write(base64.b64decode(image[“base64”]))
print(“Image generated successfully!”)
Pricing
The Stability AI API operates on a pay-as-you-go model based on credits. Users purchase credits, and different API calls (depending on the model, image size, and number of steps) consume a certain number of credits. This model provides flexibility, as you only pay for what you use, making it suitable for both small-scale experiments and large-scale production applications.