Unknown API Key Cloudinary

62 Views Asked by At

I am creating a mern stack social media app, in which i am taking photos through multer and storing them in cloudinary, but cloudinary is constantly giving this error:

Cloudinary Upload Error: {

message: "Unknown API key '879993354584111',",

name: 'Error',

http_code: 401

}

This is my cloudinary configuration:

import { v2 as cloudinary } from 'cloudinary';

cloudinary.config({
    cloud_name: process.env.CLOUD_NAME,
    api_key: process.env.API_KEY,
    api_secret: process.env.API_SECRET_KEY,
});

export default cloudinary; 

This is my createPost function

export const createPost = async (req, res) => {
    try {
        const { userId, description } = req.body;
        const picturePath = req.file.path;
        console.log("Uploaded File:", req.file);
        console.log("Post request: ", req.body);
        const user = await User.findById(userId);

        if (req.file) {
            // Upload the image to Cloudinary
            try {
                console.log("CLoudinary started...");
                const result = await cloudinary.uploader.upload(picturePath, {
                    api_key: process.env.API_KEY,
                    api_secret: process.env.API_SECRET_KEY,
                    cloud_name: process.env.CLOUD_NAME,
                });
                console.log("Cloudinary Upload Result:", result);
                const finalPicturePath = result.secure_url;
                console.log("Picture Path:", finalPicturePath);
            } catch (error) {
                console.error("Cloudinary Upload Error:", error);
            }
        }

        const newPost = new Post({
            userId,
            firstname: user.firstname,
            lastname: user.lastname,
            location: user.location,
            description,
            userPicturePath: user.picturePath,
            picturePath: picturePath,
            likes: {},
            comments: []
        })

        await newPost.save();
        console.log(newPost)

        // const post = await Post.find();

        res.status(201).json({ message: 'Post added successfully' });

    } catch (err) {
        res.status(409).json({ message: err.message });
    }
}

It keeps giving me the unknown api key error

I want the image that came through multer to be added to cloudinary, but its giving me this error

1

There are 1 best solutions below

2
Aleksandar On

Based on the error message you receive from Cloudinary, 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. There's also a comma at the end of the string in the .env file.

You should be able to resolve it by removing the single quotes from around the values in your .env file as well as the trailing comma on each line and retrying the request again.