The ImageKit JavaScript SDK provides easy-to-use utility functions for generating optimized image and video URLs, applying a wide range of transformations (resize, crop, overlays, effects, and even AI-powered enhancements), and uploading files. It is designed specifically for browser-based environments. If you are looking to use ImageKit on Node.js, please refer to the ImageKit Node.js SDK.
The SDK is lightweight, does not require any dependencies, and has first-class TypeScript support. You can view the source code on GitHub.
Installation and Setup
Install the SDK via npm or yarn:
npm install @imagekit/javascript # or yarn add @imagekit/javascript
You can also include the SDK directly in your HTML using a script tag. It is recommended to load a specific version to avoid unexpected breaking changes:
<script src="https://unpkg.com/@imagekit/javascript@5.0.0/dist/imagekit.min.js"></script>
For environments with module bundlers (e.g., webpack, Rollup) or ES modules, simply import the functions:
import { buildSrc, buildTransformationString, upload } from '@imagekit/javascript';
The JavaScript SDK exports the following:
Utility functions
buildSrc
- To generate complete URLs for images and videos with transformations.buildTransformationString
- To generate transformation strings so that you can use them in your own URL generation logic.upload
- To upload files to ImageKit.
Error classes for upload error handling
ImageKitInvalidRequestError
- For invalid requests.ImageKitAbortError
- For aborted uploads.ImageKitUploadNetworkError
- For network errors during upload.ImageKitServerError
- For server-side errors.
Type definitions
The SDK provides TypeScript type definitions for all the parameters and options used in the SDK. This includes types for SrcOptions
, UploadOptions
, UploadResponse
, and Transformation
.
URL Generation
The SDK provides two helper functions:
buildSrc
- To generate complete URLs for images and videos with transformations.buildTransformationString
- To generate transformation strings so that you can use them in your own URL generation logic.
buildSrc Parameters
The buildSrc
function accepts an options object with the following parameters:
Parameter | Description and Example |
---|---|
urlEndpoint | The base URL endpoint from your ImageKit dashboard. Example: |
src | A relative or absolute path to the image or video.
Examples: |
transformation | An array of transformation objects. Each object can include properties like Example: See all supported transformation options and how to handle unsupported transformations. |
queryParameters | An object with additional query parameters to append to the URL. Example: { v: 2 } |
transformationPosition | Specifies whether the transformation string is included in the URL path or as a query parameter. Defaults to "query" .Example: "query" or "path" |
buildTransformationString Parameters
The buildTransformationString
accepts an array of transformation objects and returns the transformation string.
import { buildTransformationString } from '@imagekit/javascript'; const transformationString = buildTransformationString([ { width: 300, height: 300 }, { overlay: { type: "text", text: "Hello World" } } ]); console.log(transformationString); // Expected output: "w-300,h-300:l-text,i-Hello%20World,l-end"
Height and Width Transformations
ImageKit allows you to resize images and videos on-the-fly using the width
and height
parameters.
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/photo.jpg", transformation: [{ width: 300, height: 300 }] });
Chained Transformations
You can chain multiple transformations together by passing an array of transformation objects. Each object can specify different properties, such as width, height, cropping, overlays, and effects. See chained transformations for more details.
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/photo.jpg", transformation: [ { width: 400, height: 300 }, { rotation: 90 } ] });
Adding Overlays
You can add overlays to images and videos. The overlay can be a text, image, video, or subtitle.
Besides the overlay type, you can also specify the position, size, and other properties of the overlay. Check the overlay options for more details.
Image Overlay
// Image overlay example: buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/background.jpg", transformation: [ { overlay: { type: "image", input: "logo.png" } } ] });
You can further transform the overlay image independently of the base image by adding additional transformation parameters. For example, to resize the overlay image:
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/background.jpg", transformation: [ { overlay: { type: "image", input: "logo.png", transformation: [ { width: 100, height: 100 } ] } } ] });
Image overlays support a wide range of transformations. Check reference for a complete list of transformations supported on image overlays.
Solid Color Overlay
You can add a solid color overlay to images and videos.
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/background.jpg", transformation: [ { overlay: { type: "solidColor", color: "FF0000" // Required } } ] });
You can also specify the width, height, and other properties of the solid color overlay. For example:
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/background.jpg", transformation: [ { overlay: { type: "solidColor", color: "FF0000", // Required transformation: [ { width: 100, height: 100 } ] } } ] });
For more options related to styling the solid color overlay, check the solid color overlay transformations section.
Text Overlay
You can add text overlays to images and videos.
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/background.jpg", transformation: [ { overlay: { type: "text", text: "Hello, ImageKit!" } } ] });
You can also specify the font size, color, and other properties of the text overlay. For example:
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/background.jpg", transformation: [ { overlay: { type: "text", text: "Hello, ImageKit!", transformation: [ { fontSize: 20, fontColor: "FF0000" } ] } } ] });
Check the text overlay transformations section for more options related to styling the text.
Video Overlay
You can add video overlays on videos only.
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/background.mp4", transformation: [ { overlay: { type: "video", input: "video.mp4" } } ] });
Additionally, you can specify the start and end time for the overlay video. For example:
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/background.mp4", transformation: [ { overlay: { type: "video", input: "video.mp4", timing: { start: 5, duration: 10 } // Overlay appears at 5 seconds and lasts for 10 seconds } } ] });
You can also independently transform the overlay video. For example, to resize the overlay video:
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/background.mp4", transformation: [ { overlay: { type: "video", input: "video.mp4", transformation: [ { width: 100, height: 100 } // Resize overlay video independently ] } } ] });
All supported video transformations can also be applied to overlay videos.
If you're overlaying an image on a base video, refer to this list for all the transformations supported on image overlays.
Subtitle Overlay
You can add subtitle overlays on videos.
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/background.mp4", transformation: [ { overlay: { type: "subtitle", input: "subtitle.srt" } } ] });
The subtitle overlay can be styled with various properties such as font size, color, and outline. See the Subtitle Overlay Transformations section for all styling options.
Background Removal Using AI
You can use the aiRemoveBackground
transformation to remove the background from images. This is particularly useful for e-commerce applications.
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/photo.jpg", transformation: [{ aiRemoveBackground: true }] });
ImageKit supports multiple AI-powered transformations, like upscaling, generative fill, and more. You can use these transformations to enhance your images without needing to manually edit them. Check the AI Transformations section for more details.
More examples:
// Drop shadow using AI buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/photo.jpg", transformation: [{ aiDropShadow: true }] }); // Upscale a small image using AI buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/photo.jpg", transformation: [{ aiUpscale: true }] });
Arithmetic Expressions
You can use arithmetic expressions to dynamically compute transformation values. For example, to set the width to half of the original image width:
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/photo.jpg", transformation: [{ width: "iw_div_2" }] });
Check out the Arithmetic Expressions reference for more examples and details.
Uploading Files
The upload()
function enables you to upload files to ImageKit using upload V1 API. It accepts an options object and returns a promise that resolves with an upload response. The SDK JSON stringifies certain parameters before sending them to the server as expected by the API. If the SDK does not support a parameter, it is added as-is in the request. Check the API documentation for more details.
upload() Parameters
The upload()
function accepts a JSON object with the following parameters:
Option | Description and Example |
---|---|
file (Required) | The file content to be uploaded. Accepts binary data, a base64-encoded string, or a URL. Typically used with a File or Blob in the browser. Example: file: fileInput.files[0] |
fileName (Required) | The name to assign to the uploaded file. Supports alphanumeric characters, dot, underscore, and dash. Any other character is replaced with _ . Example: fileName: "myImage.jpg" |
signature (Required) | The HMAC-SHA1 digest of the concatenation of "token + expire". The signing key is your ImageKit private API key. Must be computed on the server side. Example: signature: "generated_signature" |
token | A unique value to identify and prevent replays. Typically a UUID (e.g., version 4). Example: Check the signature generation section for more details on how to generate this value. |
expire | A Unix timestamp in seconds, less than 1 hour in the future. Example: Check the signature generation section for more details on how to generate this value. |
publicKey | The public API key for your ImageKit account. This is used to identify the account making the upload request. Example: Check the signature generation section for more details on how to generate this value. |
onProgress | A callback function to track the upload progress. It receives an event object with loaded and total properties. Example: onProgress: (event) => console.log(event.loaded, event.total) This is useful for showing upload progress to the user. |
abortSignal | An optional AbortSignal object to abort the upload request. If the signal is already aborted, the upload will fail immediately. You can create an AbortController instance and pass its signal to the upload() function. |
useUniqueFileName | Boolean flag to automatically generate a unique filename if set to true. Defaults to true. If false, the image is uploaded with the provided filename, replacing any existing file with the same name. Example: useUniqueFileName: true |
folder | The folder path where the file will be stored, e.g., "/images/folder/". If the path doesn't exist, it is created on-the-fly. Example: folder: "/images/uploads" |
isPrivateFile | Boolean to mark the file as private, restricting access to the original file URL. A private file requires signed URLs or named transformations for access. Defaults to false. Example: isPrivateFile: false |
tags | Optionally set tags on the uploaded file. Can be a comma-separated string or an array of tags. Example: tags: "summer,holiday" or tags: ["summer","holiday"] |
customCoordinates | A string in "x,y,width,height" format that defines the region of interest in an image (top-left coords and area size). Example: customCoordinates: "10,10,100,100" |
responseFields | A comma-separated or array-based set of fields to return in the upload response. Example: responseFields: "tags,customCoordinates" |
extensions | An array of extension objects to apply to the image, e.g., background removal, auto-tagging, etc. Example: extensions: [{ name: "auto-tagging" }] |
webhookUrl | A webhook URL to receive the final status of any pending extensions once they've completed processing. Example: webhookUrl: "https://example.com/webhook" |
overwriteFile | Defaults to true. If false, and "useUniqueFileName" is also false, the API immediately fails if a file with the same name/folder already exists. Example: overwriteFile: true |
overwriteAITags | Defaults to true. If true, and an existing file is found at the same location, its AITags are removed. Set to false to keep existing AITags. Example: overwriteAITags: true |
overwriteTags | Defaults to true. If no tags are specified in the request, existing tags are removed from overwritten files. Setting to false has no effect if the request includes tags. Example: overwriteTags: true |
overwriteCustomMetadata | Defaults to true. If no customMetadata is specified in the request, existing customMetadata is removed from overwritten files. Setting to false has no effect if the request specifies customMetadata. Example: overwriteCustomMetadata: true |
customMetadata | A stringified JSON or an object containing custom metadata fields to store with the file. Custom metadata fields must be pre-defined in your ImageKit configuration. Example: customMetadata: {author: "John Doe"} |
transformation | Defines pre and post transformations to be applied to the file during upload. The SDK enforces certain validation rules for pre/post transformations. Example: transformation: { pre: "w-200,h-200", post: [...] } |
xhr | An optional XMLHttpRequest instance for the upload. The SDK merges it with its own logic to handle progress events, etc. Example: xhr: new XMLHttpRequest() |
checks | A string specifying the checks to be performed server-side before uploading to the media library, e.g., size or mime type checks. Example: checks: "file.size' < '1MB'" |
Upload Example and Error Handling
The .upload
function requires you to send token
, signature
, expire
and publicKey
parameters. Check the signature generation section for more details on how to generate these values.
import { upload, ImageKitInvalidRequestError, ImageKitAbortError, ImageKitUploadNetworkError, ImageKitServerError } from '@imagekit/javascript'; const fileInput = document.getElementById('fileInput'); const abortController = new AbortController(); // You can abort the upload using abortController.abort(); // Get required token, signature, and expire values from your server. Check the signature generation below for more details. fileInput.addEventListener('change', async (e) => { const file = e.target.files[0]; const uploadOptions = { file, fileName: file.name, token: "your_upload_token", // Check signature generation section for more details signature: "your_generated_signature", expire: Math.floor(Date.now() / 1000) + 3600, publicKey: "your_public_key", onProgress: (event) => { console.log(`Progress: ${event.loaded}/${event.total}`); }, abortSignal: abortController.signal }; try { const response = await upload(uploadOptions); console.log("Upload successful:", response); } catch (error) { if (error instanceof ImageKitAbortError) { // You aborted the upload console.error("Upload aborted:", error.reason); } else if (error instanceof ImageKitInvalidRequestError) { // You made an invalid request console.error("Invalid request:", error.message); } else if(error instanceof ImageKitUploadNetworkError) { // Random network error on client side console.error("Network error:", error.message); } else if (error instanceof ImageKitServerError) { // Error on ImageKit server side (5xx). Usually rare and temporary. console.error("Server error:", error.message); } else { // Any other error. You love JavaScript, right? console.error("Upload error:", error); } } });
Signature Generation
The .upload
function requires you to send token
, signature
, and expire
parameters. These are used to authenticate the upload request. These values should be generated in your secure backend server and passed to the client. The SDK does not generate these values for you. However, all ImageKit SDKs (Java, PHP, Node.js, Python, Ruby, etc.) provide methods to generate these values. You can use any of these SDKs to generate the signature and token.
Check the upload API documentation for more details on how to generate the signature and token.
Never expose your private API key in the client-side code. Always generate the token
, signature
, and expire
values on your server and pass them to the client securely.
Sample code to generate the signature and token using Node.js SDK:
var ImageKit = require("imagekit"); // Node.js SDK var imagekit = new ImageKit({ publicKey : "your_public_api_key", privateKey : "your_private_api_key", urlEndpoint : "https://ik.imagekit.io/your_imagekit_id/" }); var { token, expire, signature } = imagekit.getAuthenticationParameters(); console.log({token, expire, signature});
Supported Transformations
The SDK gives a name to each transformation parameter (e.g. height
maps to h
, width
maps to w
). If the property does not match any of the following supported options, it is added as is in the URL.
If you want to generate transformations without any modifications, use the raw
parameter. For example, the SDK doesn't provide a nice way to write conditional transformations, so you can use the raw
parameter to add them as is.
Check transformation documentation for a complete reference on all transformations supported by ImageKit.
Transformation Name | URL Parameter |
---|---|
width | w |
height | h |
aspectRatio | ar |
quality | q |
aiRemoveBackground | e-bgremove (ImageKit powered) |
aiRemoveBackgroundExternal | e-removedotbg (Using third party) |
aiUpscale | e-upscale |
aiRetouch | e-retouch |
aiVariation | e-genvar |
aiDropShadow | e-dropshadow |
aiChangeBackground | e-changebg |
crop | c |
cropMode | cm |
x | x |
y | y |
xCenter | xc |
yCenter | yc |
focus | fo |
format | f |
radius | r |
background | bg |
border | b |
rotation | rt |
blur | bl |
named | n |
dpr | dpr |
progressive | pr |
lossless | lo |
trim | t |
metadata | md |
colorProfile | cp |
defaultImage | di |
original | orig |
videoCodec | vc |
audioCodec | ac |
grayscale | e-grayscale |
contrastStretch | e-contrast |
shadow | e-shadow |
sharpen | e-sharpen |
unsharpMask | e-usm |
gradient | e-gradient |
flip | fl |
opacity | o |
zoom | z |
page | pg |
startOffset | so |
endOffset | eo |
duration | du |
streamingResolutions | sr |
overlay | Generates the correct layer syntax for image, video, text, subtitle, and solid color overlays. |
raw | The string provided in raw will be added in the URL as is. |
Handling Unsupported Transformations
If you specify a transformation parameter that is not explicitly supported by the SDK, it is added “as-is” in the generated URL. This provides flexibility for using new or custom transformations without waiting for an SDK update.
For example:
buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/photo.jpg", transformation: [ { unsupportedTransformation: "value" } ] }); // Output: https://ik.imagekit.io/your_imagekit_id/photo.jpg?tr=unsupportedTransformation-value
Overlay Reference
This SDK provides overlay
as a transformation parameter. The overlay can be a text, image, video, subtitle, or solid color.
Overlays in ImageKit are applied using layers, allowing you to stack multiple overlays on top of each other. Each overlay can be styled and positioned independently. For more details, refer to the layer documentation.
The SDK automatically generates the correct layer syntax for image, video, text, subtitle, and solid color overlays. You can also specify the overlay position, size, and other properties.
The table below outlines the available overlay configuration options:
Option | Description and Example |
---|---|
type (Required) | Specifies the type of overlay. Supported values: text , image , video , subtitle , solidColor . Example: type: "text" |
text (Required for text overlays) | The text content to display. Example: text: "ImageKit" |
input (Required for image, video, or subtitle overlays) | Relative path to the overlay asset. Example: input: "logo.png" or input: "overlay-video.mp4" |
color (Required for solidColor overlays) | RGB/RGBA hex code or color name for the overlay color. Example: color: "FF0000" |
encoding | Accepted values: auto , plain , base64 . Check this for more details. Example: encoding: "auto" |
transformation | An array of transformation objects to style the overlay independently of the base asset. Each overlay type has its own set of supported transformations.
|
position | Sets the overlay’s position relative to the base asset. Accepts an object with x , y , or focus . Example: position: { x: 10, y: 20 } or position: { focus: "center" } |
timing | (For video base) Specifies when the overlay appears using start , duration , and end (in seconds); if both duration and end are set, duration is ignored. Example: timing: { start: 5, duration: 10 } |
Encoding Options
Overlay encoding options define how the overlay input is converted for URL construction. When set to auto
, the SDK automatically determines whether to use plain text or Base64 encoding based on the input content.
For text overlays:
- If
auto
is used, the SDK checks the text overlay input: if it is URL-safe, it uses the formati-{input}
(plain text); otherwise, it applies Base64 encoding with the formatie-{base64_encoded_input}
. - You can force a specific method by setting encoding to
plain
(always usei-{input}
) orbase64
(always useie-{base64}
). - Note: In all cases, the text is percent-encoded to ensure URL safety.
For image, video, and subtitle overlays:
- The input path is processed by removing any leading/trailing slashes and replacing inner slashes with
@@
whenplain
is used. - Similarly, if
auto
is used, the SDK determines whether to apply plain text or Base64 encoding based on the characters present. - For explicit behavior, use
plain
orbase64
to enforce the desired encoding.
Use auto
for most cases to let the SDK optimize encoding, and use plain
or base64
when a specific encoding method is required.
Solid Color Overlay Transformations
Option | Description | Example |
---|---|---|
width | Specifies the width of the solid color overlay block (in pixels or as an arithmetic expression). | width: 100 |
height | Specifies the height of the solid color overlay block (in pixels or as an arithmetic expression). | height: 50 |
radius | Specifies the corner radius of the solid color overlay block or shape. Can be a number or "max" for circular/oval shapes. | radius: "max" |
alpha | Specifies the transparency level of the solid color overlay. Supports integers from 1 (most transparent) to 9 (least transparent). | alpha: 5 |
Text Overlay Transformations
Option | Description | Example |
---|---|---|
width | Specifies the maximum width (in pixels) of the overlaid text. The text wraps automatically, and arithmetic expressions are supported (e.g., bw_mul_0.2 or bh_div_2 ). | width: 400 |
fontSize | Specifies the font size of the overlaid text. Accepts a numeric value or an arithmetic expression. | fontSize: 50 |
fontFamily | Specifies the font family of the overlaid text. Choose from the supported fonts or provide a custom font. | fontFamily: "Arial" |
fontColor | Specifies the font color of the overlaid text. Accepts an RGB hex code, an RGBA code, or a standard color name. | fontColor: "FF0000" |
innerAlignment | Specifies the inner alignment of the text when it doesn’t occupy the full width. Supported values: left , right , center . | innerAlignment: "center" |
padding | Specifies the padding around the text overlay. Can be a single integer or multiple values separated by underscores; arithmetic expressions are accepted. | padding: 10 |
alpha | Specifies the transparency level of the text overlay. Accepts an integer between 1 and 9 . | alpha: 5 |
typography | Specifies the typography style of the text. Supported values: b for bold, i for italics, and b_i for bold with italics. | typography: "b" |
background | Specifies the background color of the text overlay. Accepts an RGB hex code, an RGBA code, or a color name. | background: "red" |
radius | Specifies the corner radius of the text overlay. Accepts a numeric value or max for circular/oval shape. | radius: "max" |
rotation | Specifies the rotation angle of the text overlay. Accepts a numeric value for clockwise rotation or a string prefixed with N for counterclockwise rotation. | rotation: 90 |
flip | Specifies the flip option for the text overlay. Supported values: h , v , h_v , v_h . | flip: "h" |
lineHeight | Specifies the line height for multi-line text. Accepts a numeric value or an arithmetic expression. | lineHeight: 20 |
Subtitle Overlay Transformations
Option | Description | Example |
---|---|---|
background | Specifies the subtitle background color using a standard color name, RGB color code, or RGBA color code. | background: "blue" |
fontSize | Sets the font size of subtitle text. | fontSize: 16 |
fontFamily | Sets the font family of subtitle text. | fontFamily: "Arial" |
color | Specifies the font color of subtitle text using standard color name, RGB, or RGBA color code. | color: "FF0000" |
typography | Sets the typography style of subtitle text. Supported values: b , i , b_i . | typography: "b" |
fontOutline | Specifies the font outline for subtitles. Requires an outline width and color separated by an underscore. | fontOutline: "2_blue" |
fontShadow | Specifies the font shadow for subtitles. Requires shadow color and indent separated by an underscore. | fontShadow: "blue_2" |