You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can use this NodeJS SDK for 3 different kinds of functions - URL generation, file uploads and file management. The usage of the SDK has been explained below
25
+
26
+
### URL Generation
27
+
There are two ways of creating a URL with the SDK.
28
+
29
+
The first method allows you to create a URL using the path where the image exists and the URL endpoint you want to use to access the image. You can refer to the documentation [here](https://docs.imagekit.io/imagekit-docs/url-endpoints) to read more about URL endpoints in ImageKit and the section about [image origins](https://docs.imagekit.io/imagekit-docs/configure-origin) to understand about paths with different kinds of origins.
30
+
31
+
The second method allows you to add transformation parameters to an existing, complete URL that is already mapped to ImageKit. This method should be used if you have the complete URL mapped to ImageKit stored in your database.
| urlEndpoint | Optional. The base URL to be appended before the path of the image. If not specified, the URL Endpoint specified at the time of SDK initialization is used. For example, https://ik.imagekit.io/your_imagekit_id/endpoint/|
77
+
| path | Conditional. This is the path at which the image exists. For example, `/path/to/image.jpg`. Either the `path` or `src` parameter need to be specified for URL generation. |
78
+
| src | Conditional. This is the complete URL of an image already mapped to ImageKit. For example, `https://ik.imagekit.io/your_imagekit_id/endpoint/path/to/image.jpg`. Either the `path` or `src` parameter need to be specified for URL generation. |
79
+
| transformation | Optional. An array of objects specifying the transformation to be applied in the URL. The transformation name and the value should be specified as a key-value pair in the object. Different steps of a [chained transformation](https://docs.imagekit.io/imagekit-docs/chained-transformations) can be specified as different objects of the array. The complete list of supported transformations in the SDK and some examples of using them are given later. If you use a transformation name that is not specified in the SDK, it gets applied as it is in the URL. |
80
+
| transformationPostion | Optional. Default value is `path` that places the transformation string as a path parameter in the URL. Can also be specified as `query` which adds the transformation string as the query parameter `tr` in the URL. If you use `src` parameter to create the URL, then the transformation string is always added as a query parameter. |
81
+
| queryParameters | Optional. These are the other query parameters that you want to add to the final URL. These can be any query parameters and not necessarily related to ImageKit. Especially useful, if you want to add some versioning parameter to your URLs. |
82
+
| signed | Optional. Boolean. Default is `false`. If set to `true`, the SDK generates a signed image URL adding the image signature to the image URL. |
83
+
| expireSeconds | Optional. Integer. Meant to be used along with the `signed` parameter to specify the time in seconds from now when the URL should expire. If specified, the URL contains the expiry timestamp in the URL and the image signature is modified accordingly. |
84
+
85
+
#### Examples of generating URLs
86
+
87
+
**1. Chained Transformations as a query parameter**
**2. Sharpening and contrast transforms and a progressive JPG image**
106
+
107
+
There are some transforms like [Sharpening](https://docs.imagekit.io/imagekit-docs/image-enhancement---color-manipulation) that can be added to the URL with or without any other value. To use such transforms without specifying a value, specify the value as "-" in the transformation object, otherwise, specify the value that you want to be added to this transformation.
The complete list of transformations supported and their usage in ImageKit can be found [here](https://docs.imagekit.io/imagekit-docs/image-transformations). The SDK gives a name to each transformation parameter, making the code simpler and readable. If a transformation is supported in ImageKit, but a name for it cannot be found in the table below, then use the transformation code from ImageKit docs as the name when using in the `url` function.
147
+
148
+
| Supported Transformation Name | Translates to parameter |
The SDK provides a simple interface using the `.upload()` method to upload files to the ImageKit Media Library. It accepts all the parameters supported by the [ImageKit Upload API](https://docs.imagekit.io/imagekit-docs/server-side-file-upload).
198
+
199
+
The `upload()` method requires at least the `file` and the `fileName` parameter to upload a file and returns a callback with the `error` and `result` as arguments. You can pass other parameters supported by the ImageKit upload API using the same parameter name as specified in the upload API documentation. For example, to specify tags for a file at the time of upload use the `tags` parameter as specified in the [documentation here](https://docs.imagekit.io/imagekit-docs/server-side-file-upload).
200
+
201
+
Sample usage
202
+
```
203
+
imagekit.upload({
204
+
file : <url|base_64|binary>, //required
205
+
fileName : "my_file_name.jpg", //required
206
+
}, function(error, result) {
207
+
208
+
});
209
+
```
210
+
211
+
If the upload succeed, `error` will be `null` and the `result` will be the same as what is received from ImageKit's servers.
212
+
If the upload fails, `error` will be the same as what is received from ImageKit's servers and the `result` will be null.
213
+
214
+
215
+
21
216
### File Management
22
217
218
+
The SDK provides a simple interface for all the [media APIs mentioned here](https://docs.imagekit.io/imagekit-docs/media-api) to manage your files. You can use a callback function with all API interfaces. The first argument of the callback function is the error and the second is the result of the API call. Error will be `null` if the API succeeds.
219
+
220
+
**1. List & Search Files**
221
+
222
+
Accepts an object specifying the parameters to be used to list and search files. All parameters specified in the [documentation here](https://docs.imagekit.io/imagekit-docs/list-and-search-files-api) can be passed as is with the correct values to get the results.
223
+
224
+
```
225
+
imagekit.listFiles({
226
+
skip : 10,
227
+
limit : 10
228
+
}, function(err, result) {
229
+
230
+
});
231
+
```
232
+
233
+
**2. Get File Details**
234
+
235
+
Accepts the file ID and fetches the details as per the [API documentation here](https://docs.imagekit.io/imagekit-docs/get-file-details-api).
Update parameters associated with the file as per the [API documentation here](https://docs.imagekit.io/imagekit-docs/update-file-details-api). The first argument to the `updateFileDetails` method is the file ID and the second argument is an object with the parameters to be updated.
256
+
257
+
```
258
+
imagekit.updateFileDetails("file_id", {
259
+
tags : ['image_tag'],
260
+
customCoordinates : "10,10,100,100"
261
+
}, function(err, result) {
262
+
263
+
});
264
+
```
265
+
266
+
**5. Delete File**
267
+
268
+
Delete a file as per the [API documentation here](https://docs.imagekit.io/imagekit-docs/delete-file-api). The method accepts the file ID of the file that has to be deleted.
Programmatically issue a cache clear request as per the [API documentation here](https://docs.imagekit.io/imagekit-docs/purge-cache-api). Accepts the full URL of the file for which the cache has to be cleared.
Get the purge cache request status using the request ID returned when a purge cache request gets submitted as per the [API documentation here](https://docs.imagekit.io/imagekit-docs/purge-cache-status-api)
In case you are looking to implement client-side file upload, you are going to need a token, timestamp and a valid signature for that upload. The SDK provides a simple method that you can use in your code to generate these authentication parameters for you.
299
+
300
+
*Note: The Private API Key should never be exposed in any client-side code. You must always generate these authentication parameters on the server-side*
301
+
302
+
```
303
+
var authenticationParameters = imagekit.getAuthenticationParameters(token, timestamp);
304
+
```
305
+
306
+
Returns
307
+
```
308
+
{
309
+
token : "unique_token",
310
+
timestamp : "valid_timestamp",
311
+
signature : "generated_signature"
312
+
}
313
+
```
314
+
315
+
Both the `token` and `timestamp` parameters are optional. If not specified the SDK uses the [uuid](https://www.npmjs.com/package/uuid) package to generate a random token and also generates a valid timestamp internally. The value of the `token` and `timestamp` used to generate the signature are always returned in the response, no matter if they are provided as an input to this method or not.
0 commit comments