DALL-E, developed by OpenAI, is an AI model that can generate images from textual descriptions. It combines the power of deep learning and generative modeling to create unique and imaginative visual outputs based on the provided text prompts. This technology has sparked excitement among researchers, artists, and developers alike.


To make the capabilities of DALL-E accessible to developers, OpenAI has released the DALL-E API. This API allows developers to interact with the DALL-E model programmatically; by using this, developers can generate images by giving text prompts. By using Node.js and  DALL-E, we can generate images programmatically.

Note. If you want to achieve this using Python, Please read my previous article on how to Generate Image Using DALL-E API and Python.

Prerequisites
It is essential to have a basic understanding of the following concepts and technologies.

Node.js
Node.js is a JavaScript runtime environment that allows you to run JavaScript code outside of a web browser. To use Node.js, you'll need to install it on your machine. Install Node.js from the official website.

Basic API Concepts

A basic understanding of API (Application Programming Interface) concepts is crucial. APIs provide a way for different software applications to communicate and exchange data. Familiarize yourself with concepts like endpoints, HTTP methods (GET, POST, etc.), request/response structure, and how to handle API responses using JavaScript.

Setting Up the Project

Open your terminal or command prompt and follow these steps.
    Create a new directory for your Project.
    mkdir dalle-image
    cd dalle-image


Initialize a new Node.js project by running the following command.
npm init -y

This will create a package.json file, keeping track of your Project's dependencies.

Install Required Dependencies
Install the Axios library to make API requests to the DALL-E API. Axios is a popular JavaScript library for making HTTP requests. It simplifies the process of sending asynchronous requests and handling responses. To install it, use the following command
npm install axios

Now, Axios is added to your Project's dependencies, and you can start using it to interact with the DALL-E API.

Writing the Node.js Code

Write the code to interact with the DALL-E API using Node.js. Follow the steps below.

Setting up the API Endpoint and Authorization

Navigate to the dalle-image directory and create a new JavaScript file, for example, dalle-api.js.
Require the Axios module at the top.
const axios = require('axios');

Next, declare a constant variable to store your DALL-E API key.
const API_KEY = 'YOUR_API_KEY';

Implementing the API Call using Axios
Implement the API call using Axios.
const axios = require('axios');
const API_KEY = 'YOUR_API_KEY';   //replace with your key

const generateImages = async () => {
  try {
    const response = await axios.post(
      'https://api.openai.com/v1/images/generations',
      {
        prompt: 'A beautiful sunset over a serene lake',
        n: 1,                                //define the number of images
        size: '512x512',                     //define the resolution of image
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${API_KEY}`,
        },
      }
    );

    console.log(response.data);
    // Handle the response here, e.g., extract image data and display or save it.
  } catch (error) {
    console.error('Error:', error.response.data);
  }
};

generateImages();


In the code above, we use the Axios. post method to make a POST request to the API endpoint. We pass the request payload as the second argument to the method and include the API key in the request headers.

Now run the Project using the following command.
node dalle-api.js

Check the console output to see the API response or any errors that occurred during the process.

Output