DEV Community

Cover image for OpenAI API Example in NodeJS
Jake Flavin
Jake Flavin

Posted on

OpenAI API Example in NodeJS

Back in November, OpenAI announced that developers can begin building apps with the DALL·E API. I wanted to play around with it, so I made a simple CLI for it and wanted to share how easy it is to get started using their API.

(If you don't want to read the article and just want to see the final code, you can view that here.)

Prerequisites

  • You must have NodeJS installed on your machine.
  • You should already have an OpenAI account. If you do not have an account, you can use this link to sign up.

Set Up

First lets set up a new NodeJS project for our CLI. OpenAI has conveniently made a library for us to use. We will be using that as well as Commander to help with making the CLI portion of that app. Our package.json looks like this:

  "name": "open-ai-example",
  "version": "1.0.0",
  "description": "An example application that integrates with OpenAI API.",
  "main": "generate.js",
  "keywords": [],
  "author": "Jake Flavin",
  "license": "ISC",
  "dependencies": {
    "commander": "10.0.0",
    "openai": "3.1.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

Using the OpenAI API

Next, lets create a folder called api and add a new file in it called openai.js. Your project structure should look like this:

open-ai-example/
├── api/
│   ├── openai.js
└── package.json
Enter fullscreen mode Exit fullscreen mode

The API for creating an image needs 5 params total, which includes your API key. You can find your API key in your OpenAI account settings. For this example, we will only be accepting 2 of the params as CLI argument and the rest will be hard coded.

First, lets create a shell of a method that accepts the API key and a prompt.

const { Configuration, OpenAIApi } = require("openai");

async function createImage (prompt, key) {
    return "";
}

module.exports = {
    createImage
}
Enter fullscreen mode Exit fullscreen mode

Next, we will need to create and configure our OpenAIApi object. There are a few different configuration options available, but the only required item is the API key. Our plan is to accept the API key as a CLI argument, so we just need to make sure we pass that value along here.

const { Configuration, OpenAIApi } = require("openai");

async function createImage (prompt, key) {
    const openai = new OpenAIApi(new Configuration({
        apiKey: key
    }));

    return "";
}

module.exports = {
    createImage
}
Enter fullscreen mode Exit fullscreen mode

Now that we have our configuration ready, we can start to make use of the createImage method given to us. This method accepts a createImageRequest object. We will set the prompt via the input param and hard code the rest.

const { Configuration, OpenAIApi } = require("openai");

async function createImage (prompt, key) {
    const openai = new OpenAIApi(new Configuration({
        apiKey: key
    }));

    const response = await openai.createImage({
        prompt: prompt,
        n: 1, // number of images to generate, OpenAI charges per image generated
        size: "256x256", // this is the cheapest image size
        response_format: "b64_json" // the format we want the data returned
    })

    return "";
}

module.exports = {
    createImage
}
Enter fullscreen mode Exit fullscreen mode

Finally, we want to return the image. We asked OpenAI for one image in base64 format, therefore we can pull out the base64 string and return it.

const { Configuration, OpenAIApi } = require("openai");

async function createImage (prompt, key) {
    const openai = new OpenAIApi(new Configuration({
        apiKey: key
    }));

    const response = await openai.createImage({
        prompt: prompt,
        n: 1, // number of images to generate, OpenAI charges per image generated
        size: "256x256", // this is the cheapest image size
        response_format: "b64_json" // the format we want the data returned
    })

    return response.data.data[0].b64_json;
}

module.exports = {
    createImage
}

Enter fullscreen mode Exit fullscreen mode

Putting the CLI Together

Next, we will create a new file that will be responsible for the CLI set up that will eventually call the createImage method we just created. Lets call that file generate.js.

Your project structure should not look like this:

open-ai-example/
├── api/
│   ├── openai.js
├── generate.js
└── package.json
Enter fullscreen mode Exit fullscreen mode

Now we can start setting up our CLI. We can name our program "generate".

const { Command } = require('commander');

const program = new Command();

program.name('generate')
    .description('CLI to generate products.')
    .version('1.0.0');

program.parse();
Enter fullscreen mode Exit fullscreen mode

We can add a new command that will call our new createImage method. We will need to tell commander what arguments and options we want to accept. We want the propmt as the first argument and the API key as a option.

const { Command } = require('commander');
const openai = require("./api/openai");

const program = new Command();

program.name('generate')
    .description('CLI to generate products.')
    .version('1.0.0');

program.command('image')
    .description('Generate an Image (using OpenAI\'s DALL·E API)')
    .argument('<prompt>', 'The prompt to generate the image from.')
    .option('--key <key>', 'Your OpenAI API key.')
    .action(async (prompt, options) => {
        const base64Image = await openai.createImage(prompt, options.key);
        console.log(base64Image)
    });

program.parse();

Enter fullscreen mode Exit fullscreen mode

We can now test by running our CLI. Make sure you use your own API key.

node generate.js image "cat in space" --key "<API_KEY>"

iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAAAaGVYSW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AAH/Af8AAAABBQQG/Pz6AQEBAgME//79/v8BAgMDAAD/AwIDAQMC/Pv6//8AFhUg7u/p/fz5AgEEBgcF+Pn2AQAA/fz8BPv7+AAAAfz8+v//BAQEBgD8/P///fz8+fLy7gAA/....
Enter fullscreen mode Exit fullscreen mode

If you see a long base64 string be printed to the console, congrats! You have successfully used OpenAI's DALL·E API.

Finally, lets write the base64 string to the file system so we can see the image that is generated. We will add a new option for the file location with a default value.

const { Command } = require('commander');
const openai = require("./api/openai");
const fs = require ("fs");

const program = new Command();

program.name('generate')
    .description('CLI to generate products.')
    .version('1.0.0');

program.command('image')
    .description('Generate an Image (using OpenAI\'s DALL·E API)')
    .argument('<prompt>', 'The prompt to generate the image from.')
    .option('--key <key>', 'Your OpenAI API key.')
    .option('--file', 'The file location you want the image to be placed.', 'image.png')
    .action(async (prompt, options) => {
        const base64Image = await openai.createImage(prompt, options.key);
        fs.writeFile(options.file, base64Image, {encoding: 'base64'}, function(err) {
            console.log('File created.');
        });
    });

program.parse();

Enter fullscreen mode Exit fullscreen mode

Now we can re-run the same command and we should see a new image be created on the file system.

node generate.js image "cat in space" --key "<API_KEY>"
File created.

Enter fullscreen mode Exit fullscreen mode

This is the image that was generated with this prompt. I might print it and frame it.

Image of a cat in space that was generated by Open AI

You can view the final result here in my github repo. Let me know what you think!

done;

Oldest comments (0)