JavaScript SDK

Overview

Cloudinary's JavaScript SDK provides simple, yet comprehensive image and video upload, transformation, optimization, and delivery capabilities that you can implement using code that integrates seamlessly with your existing JavaScript application.

This guide relates to the latest released version of the Cloudinary JavaScript library.

For details on all new features and fixes from previous versions, see the CHANGELOG.

Note
The following instructions describe Cloudinary's JavaScript client-side library. For the server side NodeJS library, see the NodeJS documentation.

Quick example

Take a look at the following transformation code and the image it delivers:

JS:
Copy to clipboard
cloudinary.imageTag('front_face.png', {secure: true, transformation: [
  {width: 150, height: 150, gravity: "face", crop: "thumb"},
  {radius: 20},
  {effect: "sepia"},
  {overlay: new cloudinary.Layer().publicId("cloudinary_icon"), gravity: "south_east", x: 5, y: 5, width: 50, opacity: 60, effect: "brightness:200"},
  {angle: 10}
  ]}).toHtml();
sample transformation

This relatively simple code performs all of the following on the original front_face.jpg image before delivering it:

  • Crop to a 150x150 thumbnail using face-detection gravity to automatically determine the location for the crop
  • Round the corners with a 20 pixel radius
  • Apply a sepia effect
  • Overlay the Cloudinary logo on the southeast corner of the image (with a slight offset). The logo is scaled down to a 50 pixel width, with increased brightness and partial transparency (opacity = 60%)
  • Rotate the resulting image (including the overlay) by 10 degrees
  • Convert and deliver the image in PNG format (the originally uploaded image was a JPG)

And here's the URL that would be included in the image tag that's automatically generated from the above code:

JavaScript SDK features

  • Build URLs for image and video transformation & manipulation
  • JavaScript view helper tags for embedding and transforming images, and more

Getting started

This section describes the information you need to know for installing and setting up JavaScript.

Installation

Install the files using the NPM package manager:

Copy to clipboard
npm install cloudinary-core --save

The optional --save parameter saves the dependency in your package.json file.

Setup

1. Instantiate the cloudinary-core library

If you installed the files with NPM, instantiate a new variable with the cloudinary-core library within your code.

Copy to clipboard
var cloudinary = require("cloudinary-core"); // If your code is for ES5
import {Cloudinary} from cloudinary-core;    // If your code is for ES6 or higher

Otherwise, you can include the files in your in your HTML page. For Example:

Copy to clipboard
<script src="/lodash/lodash.js" type="text/javascript"></script>
<script src="/cloudinary-core/cloudinary-core.js" type="text/javascript"></script>

2. Set Cloudinary configuration parameters

To use the Cloudinary JavaScript library you have to configure at least your cloud_name. You can additionally define a number of optional configuration parameters if relevant. You can find your account-specific configuration credentials in the dashboard of our Management Console.

You set configuration parameters while instantiating a new Cloudinary class, for example:

Copy to clipboard
var cl = new cloudinary.Cloudinary({cloud_name: "demo", secure: true});

JavaScript capitalization and data type guidelines

When using the Node.js SDK, keep these guidelines in mind:

  • Parameter names: snake_case. For example: public_id
  • Classes: PascalCase. For example: ImageTag
  • Methods: camelCase. For example: toHTML
  • Pass parameter data as: Object