Error: Could not load data:image/png;base64,undefined: undefined
at chunk-NSXAVJG4.js?v=007676ee:17070:39
at HTMLImageElement.onImageError (chunk-7Z2GYTU5.js?v=007676ee:26069:20)
Code
// this is created using v4 of OpenAI API
import express from "express";
import * as dotenv from "dotenv";
import OpenAI from "openai";
dotenv.config();
const router = express.Router();
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
router.route("/").get((req, res) => {
res.status(200).json({ message: "Hello from DALL-E routes" });
});
// posting image to front-end app
router.route("/").post(async (req, res) => {
try {
const { prompt } = req.body;
const aiResponse = await openai.images.generate({
model: "dall-e-3",
prompt,
n: 1,
size: "1024x1024",
response_format: "b64_json",
});
const image = aiResponse.data.data[0].b64_json;
res.status(200).json({ photo: image });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Something went wrong" });
}
});
export default router;
It sounds like you might be discussing a node.js SDK “version” of OpenAI’s, but then you talk about a URL, which is not employed by calls to the library.
A “user” API key is the classic standard API key that has no limitations on use when it is used on your default organization or one of which you are a member by invitation. It should be called “superuser” api key or “do anything” key, not one you would give to a user.
Projects was introduced a few months ago, a poor name for “scoped keys with limits”. You cannot force an organization member to use them, so inviting people to your organization is still a big problem.
You can generate API keys in projects you create, which can be limited to endpoints, and projects limited by models and usage amounts. That is a good idea for new use, to shut off anything not used to limit the exposure if a key were to be leaked or abused.
If you generate a project key, you should not need to employ an organization ID nor project ID in API calls. The API key is your authentication method, and should be loaded from an environment variable, not hard-coded or in config files.
To demonstrate you have it working, you can make API calls to one of the simpler endpoints, like models, to list the models you have access to, and then can orient yourself to parsing JSON you’ll get back or which SDKs return.
I have created new account of openai and created apikey using that account, and its neither expired nor reached the credit limit, but why I am still getting 401 unauthorized error at front-end and the error below at the node back-end
code: 'billing_hard_limit_reached',
message: 'Billing hard limit has been reached',
param: null,
type: 'invalid_request_error'
You will need to navigate to limits of your projects yourself and see what you have set.
A truly new account must be funded with prepayment of credits, waiting a bit for them to take effect, and you should generate your first API key under “user API keys” to see if you will be required to do phone verification.