GPT-3 API example without framework using vanilla Express

Hello, I was able to download and run the example. However, now I would like to implement using GPT-3 in my existing Node/Express app, and didn’t find an example on the tutorial page: OpenAI API

I have a form from a page that calls an endpoint when I hit submit, which goes to ‘api.js’ in my routes. From there, I want to call the OpenAI API. However, I am not sure where to put the example on the documentation page to get it to work.


If I use the example:
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({
    organization: "org-ZZkon15AYML0UypxazbjcHui",
    apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.listEngines();

I get a SyntaxError: Cannot use import statement outside a module

I typically only do front-end coding, so having a newbie explanation for how to get it working would be greatly appreciated.

Thanks!

It probably has to do with the JS version and module import/export as it’s done differently.

Try using this:

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