Getting Error: Must supply api_key

Options
Himanshu
Himanshu Member Posts: 4

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

  • Wissam
    Wissam Member, Cloudinary Staff Posts: 72
    Options

    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: https://cloudinary.com/documentation/node_quickstart
    Video guide here: https://training.cloudinary.com/learn/course/introduction-for-api-users-developers/lessons/environment-setup-0415?page=1

    Another option is to try to reinstall npm as follows:  npm uninstall node_modules then npm 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

  • Himanshu
    Himanshu Member Posts: 4
    Options

    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)

  • Himanshu
    Himanshu Member Posts: 4
    Options

    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

  • Wissam
    Wissam Member, Cloudinary Staff Posts: 72
    Options

    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:
    https://cloudinary.com/documentation/node_integration#setting_configuration_parameters_globally

    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 then npm install.

    Please let me know your feedback.

    Regards,

    Wissam

  • Himanshu
    Himanshu Member Posts: 4
    Options

    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!