Getting Error: Must supply api_key
This is my config
import { v2 as cloudinary } from "cloudinary";
import fs from "fs";
cloudinary.config({ cloud_name: process.env.CLOUDINARY_CLOUD_NAME, api_key: process.env.CLOUDINARY_API_KEY, api_secret: process.env.CLOUDINARY_API_SECRET,});
console.log("CLOUDINARY_API_KEY", process.env.CLOUDINARY_API_KEY); This prints the expected value in the same js file
However, when i put the values(in string) directly to the object keys then it works fine
Answers
-
Hi @Himanshu ,
The 'must supply api_key' error implies that we're not receiving the correct cloud name and API key in the API call made by your app.
Are you sure that CLOUDINARY_URL is being set correctly and that the correct values are being used by the SDK?
We have a few guides that should help you get this set up that I will link below:
Documentation page:
Video guide here: https://training.cloudinary.com/learn/course/introduction-for-api-users-developers/lessons/environment-setup-0415?page=1Another option is to try to reinstall npm as follows:
npm uninstall node_modules
thennpm install
.
If the above didn't help, can you share what you are trying to achieve and what is the code that you are using?Best Regards,
Wissam
0 -
This is the log of the error
CLOUDINARY_API_KEY [expected value was printed]Error: Must supply api_key
at ensureOption (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/utils/ensureOption.js:19:13)
at Object.sign_request (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/utils/index.js:1145:16)
at Object.process_request_params (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/utils/index.js:1195:22)
at call_api (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/uploader.js:472:18)
at Object.upload (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/uploader.js:53:10)
at Object.upload (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/utils/index.js:1392:21)
at uploadOnCloudinary (file:///Users/himannshukumar/Desktop/mern%20apps/play-yu/server/src/utils/cloudinary.js:15:48)
at file:///Users/himannshukumar/Desktop/mern%20apps/play-yu/server/src/controllers/user.controller.js:40:38
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)CLOUDINARY_API_KEY [expected value was printed]
Error: Must supply api_key
at ensureOption (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/utils/ensureOption.js:19:13)
at Object.sign_request (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/utils/index.js:1145:16)
at Object.process_request_params (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/utils/index.js:1195:22)
at call_api (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/uploader.js:472:18)
at Object.upload (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/uploader.js:53:10)
at Object.upload (/Users/himannshukumar/Desktop/mern apps/play-yu/server/node_modules/cloudinary/lib/utils/index.js:1392:21)
at uploadOnCloudinary (file:///Users/himannshukumar/Desktop/mern%20apps/play-yu/server/src/utils/cloudinary.js:15:48)
at file:///Users/himannshukumar/Desktop/mern%20apps/play-yu/server/src/controllers/user.controller.js:42:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Error: Avatar image is required!
at file:///Users/himannshukumar/Desktop/mern%20apps/play-yu/server/src/controllers/user.controller.js:45:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)0 -
Hi Wissam,
I have the below in my .env file
CLOUDINARY_CLOUD_NAME='123'
CLOUDINARY_API_KEY='123'
CLOUDINARY_API_SECRET='123'
I have a utility file named "cloudinary.js" file to upload files —-
import { v2 as cloudinary } from"cloudinary";
import fs from"fs";
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
constuploadOnCloudinary=async (localFilePath) => {
console.log("CLOUDINARY_API_KEY", process.env.CLOUDINARY_API_KEY);
try {
if (!localFilePath) returnnull;
//upload the file on cloudinary
constresponse=awaitcloudinary.uploader.upload(localFilePath, {
resource_type: "auto",
});
// file has been uploaded successfull
//console.log("file is uploaded on cloudinary ", response.url);
fs.unlinkSync(localFilePath);
returnresponse;
} catch (error) {
console.log(error);
fs.unlinkSync(localFilePath); // remove the locally saved temporary file as the upload operation got failed
returnnull;
}
};
export { uploadOnCloudinary };
When this runs the file is able to read the values from .env file because console.log("CLOUDINARY_API_KEY", process.env.CLOUDINARY_API_KEY); prints the expected value. But somehow, I am getting that error.
This is not happening only with api_key. All three values are not being used by cloudinary.config method. However, those variables are accessible in cludinary.js file
0 -
Hi there,
Thank you for the additional information.
I would make sure that the env file is loaded properly:
require('dotenv').load();
Setting the configuration parameters can be done globally, using either an environment variable or the
config
method, or programmatically in each call to a Cloudinary method. Parameters set in a call to a Cloudinary method override globally set parameters.Here's an example of setting configuration parameters globally in your Node application:
Make sure you configure Cloudinary before using any of its methods.
In addition, ensure that you’re using the correct
.env
file in case you have multiple.env
files and that it’s being loaded properly.Hope it will help.
If still, not, please consider reinstalling npm as follows:
npm uninstall node_modules
thennpm install
.Please let me know your feedback.
Regards,
Wissam
0 -
Hey Wissam,
The issue is solved automatically. So what happened is I ran into some other errors (not related to cloudinary) and then I tried relaunching vs code and restarted the server. Then it worked without any changes.
Thanks for your prompt response!
1 -
The issue is getting solved if you move configuration codes after creating express app in main file. I myself struggled 2 day with same problem. i tried everything like restarting system and vs code but that error was coming again and again so i randomly moved configuration code after express app line and it worked.
1