Support the ongoing development of Laravel.io →
Article Hero Image

Using Stability AI in Laravel

22 Nov, 2023 2 min read

Photo by Paulius Dragunas on Unsplash

Using Stability AI in Laravel

Stability AI is an AI company that develops open-source models, such as Stability Diffusion XL, which is capable of generating high-quality, realistic images from text descriptions.

In addition to Stability Diffusion XL, Stability AI also develops other open-source models, such as Stable LM, a suite of language models that can generate text and code.

As of writing this article, they offer an API for their image generation models. In this post, we’ll explore how to seamlessly interact with their API in Laravel using a custom package that I developed.

Before we get started, I encourage you to have a look at their documentation and get an API key.

To get started you have to install the package, you can do so using composer:

composer require thehocinesaad/stability-laravel

Then you will need to add your Stability AI API key to the .env file:

STABILIY_API_KEY=sk-fuYQ3VpweDSPu...

After that, you can start interacting with the API:

$response = Stability::generations()->textToImage(
    'stable-diffusion-xl-1024-v1-0',
    [
        'text_prompts' => [
            [
                'text' => 'A lighthouse on a cliff',
                'weight' => 0.5
            ],
        ],
        'samples' => 1,
    ]
);

dd($response['artifacts'][0]['base64']);
// "iVBORw0KGgoAAAANSUhEUgAABAAAAQACAIAAADwf7zUAAM8MmNhQlgAAzwya..."
//// An image encoded in base64.

Here is the resulting image:

If it is necessary, you can add additional HTTP headers (which the API supports) to requests by adding them to the .env file:

STABILIY_ACCEPT_HEADER=image/png
STABILIY_ORGANIZATION=org-123456
STABILIY_CLIENT_ID=my-great-plugin
STABILIY_CLIENT_VERSION=1.2.1

As of the current writing, the package supports all existing Stability AI API endpoints:

  • User Account and Balance
  • Engines List
  • Text-to-Image
  • Image-to-Image
  • Image-to-Image Upscale
  • Image-to-Image Masking

For more usage examples, please visit the GitHub repository of the package.

Feel free to give it a shot and let me know your thoughts, Stability AI gives some free credits for new users so you will have a chance to try it out without worrying about paying any money.

I also developed a version of this package for PHP, here is its GitHub repository..

Last updated 5 months ago.

driesvints, jocelinkisenga liked this article

2
Like this article? Let the author know and give them a clap!

Other articles you might like

Article Hero Image April 10th 2025 Sponsored

LarAgent: An Open-source package to Build & Manage AI Agents in Laravel

Laravel has all the right ingredients to become a strong candidate for AI development. With its eleg...

Read article
Article Hero Image April 8th 2025

Covariance and Contravariance in PHP

Introduction "Covariance" and "contravariance" are two terms I didn't know exist...

Read article
Article Hero Image April 2nd 2025

Human-Readable File Sizes in Laravel (KB, MB, GB)

Introduction There may be times when you want to display file sizes to your users in a human-readabl...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.