Missing API Key

avidevcreator
avidevcreator Member Posts: 8
edited August 28 in Developer APIs

This is my cloudinary config file and when i am trying to upload something i am getting error.

Code —

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,
});

const uploadOnCloudinary = async (localFilePath) => {
  console.log("CLOUDINARY_API_SECRET", process.env.CLOUDINARY_API_SECRET);
  try {
    if (!localFilePath) return null;
    //upload the file on cloudinary
    const response = await cloudinary.uploader.upload(localFilePath, {
      resource_type: "auto",
    });
    // file has been uploaded successfull
    console.log("file is uploaded on cloudinary ", response.url);
    fs.unlinkSync(localFilePath);
    return response;
  } catch (error) {
    console.log(error);
    fs.unlinkSync(localFilePath); // remove the locally saved temporary file as the upload operation got failed
    return null;
  }
};

const deleteOnCloudinary = async (public_id, resource_type = "image") => {
  try {
    if (!public_id) return null;

    //delete file from cloudinary
    const result = await cloudinary.uploader.destroy(public_id, {
      resource_type: `${resource_type}`,
    });
  } catch (error) {
    console.log("delete on cloudinary failed", error);
    return error;
  }
};

export { uploadOnCloudinary, deleteOnCloudinary };

This is the error —

[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node src/server.js`
apidb-shard-00-00.2yyfq.mongodb.net
Server Listening on 3000
test2@gmail.com
test account
test2
test123
public/temp/avatar.jpeg
CLOUDINARY_API_KEY 143121853167695
Error: Must supply api_key
    at ensureOption (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/utils/en
sureOption.js:19:13)
    at Object.sign_request (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/u
tils/index.js:1150:16)
    at Object.process_request_params (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudi
nary/lib/utils/index.js:1200:22)
    at call_api (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/uploader.js:
474:18)
    at Object.upload (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/uploade
r.js:53:10)
    at Object.upload (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/utils/i
ndex.js:1397:21)
    at uploadOnCloudinary (file:///Users/avinashdubey/Developer/Learning/Mernbackend/api/src/utils/cloudinary.js
:15:48)
    at file:///Users/avinashdubey/Developer/Learning/Mernbackend/api/src/controllers/user.controller.js:74:24
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
CLOUDINARY_API_KEY 143121853167695
Error: Must supply api_key
    at ensureOption (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/utils/en
sureOption.js:19:13)
    at Object.sign_request (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/utils/index.js:1150:16)
    at Object.process_request_params (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/utils/index.js:1200:22)
    at call_api (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/uploader.js:474:18)
    at Object.upload (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/uploader.js:53:10)
    at Object.upload (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/utils/index.js:1397:21)
    at uploadOnCloudinary (file:///Users/avinashdubey/Developer/Learning/Mernbackend/api/src/utils/cloudinary.js:15:48)
    at file:///Users/avinashdubey/Developer/Learning/Mernbackend/api/src/controllers/user.controller.js:76:28
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Error: Avatar File Required
    at file:///Users/avinashdubey/Developer/Learning/Mernbackend/api/src/controllers/user.controller.js:79:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Here you can see that when i do the console log i can see the variable data in console log but somehow its not going inside the cloudinary.config()

Tagged:

Answers

  • Cloudinary Team
    Cloudinary Team Administrator, Cloudinary Staff Posts: 176 admin

    Hi there,

    Thanks for reaching out. I'm not seeing failed uploads in the logs on our end. Can you please confirm your cloud name for me?

    Kind regards,

    Tia
    Developer Support Engineer
    Cloudinary

    --
    💡 Improve your site's performance by applying optimization features when delivering assets


    💬 Community Forums | 🧑‍💻 Discord Server
    🧑‍🎓 Academy Training | 📖 Documentation | 📰 Blog

  • avidevcreator
    avidevcreator Member Posts: 8

    cloud name = avidev

  • atdatu
    atdatu Member, Cloudinary Staff Posts: 20

    Hi @avidevcreator ,
    Thanks for providing the cloud name. I was able to locate your account but there are only two uploads entries on our logs and both are successful. Perhaps move configuration into its function then call it every time that an upload is being called?

    const config = ()⇒{
      cloudinary.config({  
      cloud_name: process.env.CLOUDINARY_CLOUD_NAME,  
      api_key: process.env.CLOUDINARY_API_KEY,  
      api_secret: process.env.CLOUDINARY_API_SECRET,});
    }
    
    const uploadOnCloudinary = async (localFilePath) => {  
      console.log("CLOUDINARY_API_SECRET", process.env.CLOUDINARY_API_SECRET);  
    
      try {    
        if (!localFilePath) return null;    
        //call config
        config();
        //upload the file on cloudinary    
        const response = await cloudinary.uploader.upload(localFilePath, {      
        resource_type: "auto",});
    …
    
    

    let me know how it goes.

  • avidevcreator
    avidevcreator Member Posts: 8

    I tried to create an iffee —

    (()=>(
      cloudinary.config({
      cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
      api_key: process.env.CLOUDINARY_API_KEY,
      api_secret: process.env.CLOUDINARY_API_SECRET,
    })
    
    ))();
    
    
    Error — 
    
    
    $ nodemon src/server.js
    [nodemon] 3.1.4
    [nodemon] to restart at any time, enter `rs`
    [nodemon] watching path(s): *.*
    [nodemon] watching extensions: js,mjs,cjs,json
    [nodemon] starting `node src/server.js`
    apidb-shard-00-00.2yyfq.mongodb.net
    Server Listening on 3000
    test1@gmail.com
    test
    test
    test123
    public/temp/avatar.jpeg
    Error: Must supply api_key
        at ensureOption (/Users/avinashdubey/Developer/Lear
        at Object.sign_request (/Users/avinashdubey/Develop
        at Object.process_request_params (/Users/avinashdub
        at call_api (/Users/avinashdubey/Developer/Learning
        at Object.upload (/Users/avinashdubey/Developer/Lea
        at Object.upload (/Users/avinashdubey/Developer/Lea
        at uploadOnCloudinary (file:///Users/avinashdubey/D
        at file:///Users/avinashdubey/Developer/Learning/Me
        at process.processTicksAndRejections (node:internal
    Error: Must supply api_key
        at ensureOption (/Users/avinashdubey/Developer/Lear
        at Object.sign_request (/Users/avinashdubey/Develop
        at Object.process_request_params (/Users/avinashdub
        at call_api (/Users/avinashdubey/Developer/Learning
        at Object.upload (/Users/avinashdubey/Developer/Leader.js:53:10)
        at Object.upload (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/utils/index.js:1397:21)
        at uploadOnCloudinary (file:///Users/avinashdubey/Developer/Learning/Mernbackend/api/src/utils/cloudinary.js:18:48)
        at file:///Users/avinashdubey/Developer/Learning/Mernbackend/api/src/controllers/user.controller.js:76:28
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    Error: Avatar File Required
        at file:///Users/avinashdubey/Developer/Learning/Mernbackend/api/src/controllers/user.controller.js:79:11
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    
    

  • avidevcreator
    avidevcreator Member Posts: 8

    You can see the console log is showing the file path as well—

    Server Listening on 3000
    test1@gmail.com
    test
    test
    test123
    public/temp/avatar.jpeg
    

  • avidevcreator
    avidevcreator Member Posts: 8

    i created a new key. Deleted old one. Updated that in my project and still the same error.

  • avidevcreator
    avidevcreator Member Posts: 8

    I created this IIFE and still same error —

    (()=>(
      cloudinary.config({
      cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
      api_key: process.env.CLOUDINARY_API_KEY,
      api_secret: process.env.CLOUDINARY_API_SECRET,
    })
    
    ))();
    

    Error —

    Error: Must supply api_key
        at ensureOption (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/utils/ensureOption.js:19:13)
        at Object.sign_request (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/utils/index.js:1150:16)
    

  • avidevcreator
    avidevcreator Member Posts: 8

    I created this IIFE and still same error —

    (()=>(
      cloudinary.config({
      cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
      api_key: process.env.CLOUDINARY_API_KEY,
      api_secret: process.env.CLOUDINARY_API_SECRET,
    })
    
    ))();
    

    Error —

    Error: Must supply api_key
        at ensureOption (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/utils/ensureOption.js:19:13)
        at Object.sign_request (/Users/avinashdubey/Developer/Learning/Mernbackend/api/node_modules/cloudinary/lib/utils/index.js:1150:16)
    

  • epasos
    epasos Member, Cloudinary Staff Posts: 26

    Hi @avidevcreator

    Thanks for the additional information.

    Kindly ensure that the .env file is in the same location where you launch your application and also to add the following in your code:

    require('dotenv').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,
    });
    
    // …
    

  • avidevcreator
    avidevcreator Member Posts: 8

    Somehow i fixed it

    just by adding this in my cloudinary.js file —

    import dotenv from "dotenv";
    
    console.log(dotenv.config());
    
    cloudinary.config({
      cloud_name: "avidev",
      api_key: process.env.CLOUDINARY_API_KEY,
      api_secret: process.env.CLOUDINARY_API_SECRET,
    });
    

  • SreeCloudinary
    SreeCloudinary Member, Cloudinary Staff Posts: 45

    Hi @avidevcreator ,

    Thanks for your response. Glad it is working now.

    My typical configuration is in .env file with:

    CLOUDINARY_NAME="XX"
    CLOUDINARY_API_KEY="XX"
    CLOUDINARY_API_SECRET="XX"

    An additional file which reads these configuration(.js):

    const dotenv= require('dotenv').config();

    var cloudinary=require('cloudinary').v2;

    cloudinary.config({

    cloud_name: `${process.env.CLOUDINARY_NAME}`,

    api_key:process.env.CLOUDINARY_API_KEY,

    api_secret:process.env.CLOUDINARY_API_SECRET});

    Thanks,

    Sree