I have a program that uploads images to Cloudinary. When I run it on my program (localhost) + Heroku, it worked fine(in the past). But now it’s not working.
the error is :
Unknown API key "12345678910"
The most confusing part to me is that when the error message prints out "Unknown API Key: 12345678910" the unknown key is the same as my Cloudinary API Key: 12345678910.
Please help me . Thank you in advance.
My .env file
CLOUDINARY_NAME = 'dupz*****'
CLOUDINARY_API_KEY = '532746917******'
CLOUDINARY_API_SECRET = 'WpTLLxyJZlMwjUITP**********'
Here is my server.js file
const app = require("./app");
const { connectDatabase } = require("./config/database");
const cloudinary = require("cloudinary");
connectDatabase();
cloudinary.config({
cloud_name: process.env.CLOUDINARY_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
Here is my Post.js file
const Post = require("../models/Post");
const User = require("../models/User");
const cloudinary = require("cloudinary");
exports.createPost = async (req, res) => {
try {
const myCloud = await cloudinary.v2.uploader.upload(req.body.image, {
folder: "posts",
});
const newPostData = {
caption: req.body.caption,
image: {
public_id: myCloud.public_id,
url: myCloud.secure_url,
},
owner: req.user._id,
};
const post = await Post.create(newPostData);
const user = await User.findById(req.user._id);
user.posts.unshift(post._id);
await user.save();
res.status(201).json({
success: true,
message: "Post created",
});
} catch (error) {
res.status(500).json({
success: false,
message: error.message,
});
}
};
2
Answers
it seems like your code is correct, if it used to run fine, then the code is likely not the problem but Cloudinary itself.
Likely problems
Based on the error message you receive from Cloudinary in your screenshot, it shows the unknown API key that was sent in your upload request – notice the API Key is within single quotes in the error message. The single quotes should not be part of the value sent in your request.
You should be able to resolve it by removing the single quotes from around the values in your
.env
file and retrying the request again.