Upload an image to Cloudinary using the Node SDK

In a similar vein to doing it with PHP, uploading an image to Cloudinary with Javascript can be done in a few lines.

I used this on Ale House Rock in a Netlify function.

Install the library using npm or yarn

npm install cloudinary --save

Next, we need to include and initialise the library - for this code we are going to be using v2 of the Node SDK.

In the example below, I've used the dotenv package to avoid having my credentials committed to the repository.

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

cloudinary.config({
	cloud_name: process.env.cloudinary_cloud_name,
	api_key: process.env.cloudinary_api_key,
	api_secret: process.env.cloudinary_api_secret,
});

To make this more easily portable, I created a module with my Cloudinary initialisation in. This allows me to include the following for a ready-to-go Cloudinary API

const cloudinary = require('./cloudinary');

With the library loaded, you can upload your image with the path to the file. This can be a remote or local path. The public_id is the path where you want the image to be uploaded to.

const cloudinary = require('cloudinary').v2

cloud_image = cloudinary.uploader.upload(
	'path/to/image.jpg',
	{
		secure: true,
		public_id: 'custom_path/and/folder.jpg'
	},
	function() {
	}
);

View this post on Github

You might also enjoy…

Mike Street

Written by Mike Street

Mike is a CTO and Lead Developer from Brighton, UK. He spends his time writing, cycling and coding. You can find Mike on Mastodon.