DALL-E API Hands-on guide with 5 practical code examples
OpenAI has made its text-to-image AI model DALL-E available via DALL-E API.
App Developers can start using DALL-E API to integrate text-to-image functionality in their existing or new web or mobile applications.
In this article, let’s take a look at how to call DALL-E API from your code.
Table of Contents
- Getting access to DALL-E API
- Using DALL-E API to generate a new image
- Using DALL-E API to edit an image
- Generate variations of an image using DALL-E API
- Calling DALL-E API from Python
- Calling DALL-E API from JavaScript (NodeJS)
- DALL-E API Pricing
Getting access to DALL-E API
In order to use DALL-E API you need to get an OpenAI API key. If you have an OpenAI account, you should already have access to YOUR API key.
If not, create a new OpenAI account.
Login and Visit the OpenAI API page.
You should see your API key listed there.
Using DALL-E API to generate a new image
In order to generate a new image using DALL-E API, you need to call the OpenAI images/generations endpoint.
Open command line program like Terminal(Mac, Linux) or PowerShell(Windows)
Setup your OpenAI API key as an environment variable OPENAI_API_KEY
Run the below command to create your first image using DALL API.
curl https://api.openai.com/v1/images/generations -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{ "prompt": "a photo of a happy corgi puppy sitting and facing forward, studio light, longshot","n":1, "size":"1024x1024", }'
Make sure to replace $OPENAI_API_KEY
it with your own API key or set up the environment variable.
DALL-E API response will turn the image in either of these two formats
- a temporary URL pointing to the image (expires in 1 hour)
- and a base64 string that you can use to display the image or do some processing on it.
A typical response will look like this.
{ "created": 1667535394, "data": [ { "url": "oaidalleapiprodscus.blob.core.windows.net/p.." } ] }
Since we didn’t specify whether we want the image in the URL or base64 string, the API returns the URL by default. Remember this is a temporary URL and expires soon.
In order to get the image in base64 string format, you need to mention another parameter to the API call called _response_format._ Let’s see an example.
curl https://api.openai.com/v1/images/generations -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{ "prompt": "a photo of a happy corgi puppy sitting and facing forward, studio light, longshot","n":1, "size":"1024x1024", "response_format":"b64_json" }'
This returns the image to us as a base64 string that you can use a lot of different to convert into an actual viewable image.
Note that we have a new parameter "response_format":"b64_json"
towards the end of the command
You can also control the number of images that you want by passing the number as “n”.
Also, controllable is the output image size by passing a parameter size with values as "1024x1024"
, "512x512"
or "256x256".
Do note though each API call costs you and that cost depend upon the number of images you want and the image size. Pricing is covered in detail later in the article.
Using DALL-E API to edit an image
OpenAI has also made available the edit endpoint for DALL-E. This is how you call it using CURL.
#edits curl api.openai.com/v1/images/edits \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -F image="@/Users/openai/happy_corgi.png" \ -F mask="@/Users/openai/mask.png" \ -F prompt="a photo of a happy corgi puppy with fancy sunglasses on sitting and facing forward, studio light, longshot" \ -F n=1 \ -F size="1024x1024"
Generate variations of an image using DALL-E API
One can also generate variations of an image using DALL-E API.
#variations curl api.openai.com/v1/images/variations \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -F image="@/Users/openai/corgi_with_sunglasses.png" \ -F n=4 \ -F size="1024x1024"
make sure to change the path to the image to the actual image you want to use from your computer.
Calling DALL-E API from Python
This is how you call DALL-E API in Python to generate a new image from a text prompt
response = openai.Image.create( prompt="a white siamese cat", n=1, size="1024x1024" ) image_url = response['data'][0]['url']
Calling DALL-E API from JavaScript (NodeJS)
Calling DALL-E API from JavaScript (NodeJS) works like this
const response = await openai.createImage({ prompt: "a white siamese cat", n: 1, size: "1024x1024", }); image_url = response.data.data[0].url;
DALL-E API Pricing
DALL-E API is charged to your account by OpenAI on a per-API call basis. That means each successful API call gets charged
This charge is based on two things:
- How many images you wanted to create in that API call, and
- what was the size of the image(s) you wanted?
Below is the pricing per image.
RESOLUTION
PRICE
1024×1024
$0.020 / image
512×512
$0.018 / image
256×256
$0.016 / image