Technology Guides and Tutorials

How to Create and Download Image from OpenAI API in NodeJS Command Line Script

DALL-E: What Is It and How Does It Work?

Openai Image Generator in NodeJS

Learn how to use the OpenAI API to generate and download images from a NodeJS command line script with this step-by-step guide.


Artificial Intelligence is rapidly revolutionizing the world by offering new and innovative solutions to various problems. One such solution is the OpenAI API, which enables developers to create various applications and tools using machine learning models. In this article, we will explore how to generate and download images from the OpenAI API using a NodeJS command line script.

In today’s world, the generation of artificial intelligence has opened doors to limitless possibilities. One such AI-based technology that has gained immense popularity is the OpenAI image generation. In this article, we will discuss the OpenAI image generation in Node.js, its working, and how to use it to generate realistic and high-quality images.

Setting Up Your Environment

1. Install Node.js: The first step is to install Node.js on your system. You can download the latest version from the official nodejs website. You can read my guide how to install NodeJS and NPM.

2. Install a Text Editor: You will need a text editor to write your code. Popular choices include Sublime Text, Atom, and Visual Studio Code.

3. Install a Package Manager: A package manager is a tool that helps you install and manage third-party libraries and packages. Popular choices include npm and Yarn.

Installing Required Dependencies

npm install request

Retrieving Your API Key

Writing script in nodejs in your editor

const request = require('request');

const fs = require('fs');
const https = require('https');

const openai_api_key = 'your-openai-api-key-here';

const args = process.argv.slice(2);
const filename = args[0];
const prompt = args[1];
const size = args[2] || '1024x1024';

const options = {
  url: '',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + openai_api_key
  body: {
    'n': 1,
  json: true
};, (err, res, body) => {
  https.get([0].url, (response) => {
    response.pipe(fs.createWriteStream('./' + filename + '.png'));

save your nodejs script as: openai-gen-image.js

to run your script in command line use:

node openai-gen-image.js your_filename "example prompt: mountains, rivers"


  • OpenAI provides an easy-to-use API for generating images using machine learning models.
  • To use the OpenAI API, you will need to create an account and retrieve your API key.
  • To generate images from the OpenAI API using a NodeJS command line script, you will need to install the OpenAI SDK and dotenv.
  • You can use command line arguments to specify the parameters for your image generation, such as filename and the prompt and the image size
  • The generated images can be saved to your local disk for further use.
Explore the potential of OpenAI’s GPT-3 language model and how it can be used to enhance your natural language processing tasks.


In this article, we have explored how to create and download images from the OpenAI API using a NodeJS command line script. By following the steps outlined in this guide, you can leverage the power of artificial intelligence to generate high-quality images for your applications and projects. With the OpenAI API, the possibilities are endless, and we hope this guide has helped you take the first step towards unlocking its full potential.

Read more – if you want to read more about openai and how to use it with text, read our guide how to use openai api with nodejs


Leave a Reply

Your email address will not be published. Required fields are marked *