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
description: The Strapi Client library simplifies interactions with your Strapi back end, providing a way to fetch, create, update, and delete content.
4
+
toc_max_heading_level: 4
4
5
displayed_sidebar: cmsSidebar
5
6
tags:
6
7
- API
@@ -51,20 +52,22 @@ To use the Strapi Client in your project, install it as a dependency using your
51
52
52
53
To start interacting with your Strapi back end, initialize the Strapi Client and set the base API URL:
53
54
54
-
With Javascript, require the `strapi` function and create a client instance:
55
55
<TabsgroupId="js-ts">
56
56
<TabItemvalue="js"label="JavaScript">
57
57
58
+
With Javascript, require the `strapi` function and create a client instance:
The Strapi Client provides access to the [Media Library](/cms/features/media-library) via the `files` property. This allows you to retrieve and manage file metadata without directly interacting with the REST API.
206
+
207
+
The following methods are available for working with files. Click on the method name in the table to jump to the corresponding section with more details and examples:
208
+
209
+
| Method | Description |
210
+
|--------|-------------|
211
+
|[`find(params?)`](#find)| Retrieves a list of file metadata based on optional query parameters |
212
+
|[`findOne(fileId)`](#findOne)| Retrieves the metadata for a single file by its ID |
213
+
|[`update(fileId, fileInfo)`](#update)| Updates metadata for an existing file |
214
+
|[`upload(type, options)`](#upload)| Uploads a file, specifying:<ul><li>`type` which can be `file`, `fileContentBuffer`, or `fileBlob`</li><li>and an additional `options` object</li></ul> |
215
+
|[`delete(fileId)`](#delete)| Deletes a file by its ID |
216
+
217
+
#### `find`
218
+
219
+
The `strapi.client.files.find()` method retrieves a list of file metadata based on optional query parameters.
220
+
221
+
The method can be used as follows:
222
+
223
+
```js
224
+
// Initialize the client
225
+
constclient=strapi({
226
+
baseURL:'http://localhost:1337/api',
227
+
auth:'your-api-token',
228
+
});
229
+
230
+
// Find all file metadata
231
+
constallFiles=awaitclient.files.find();
232
+
console.log(allFiles);
233
+
234
+
// Find file metadata with filtering and sorting
235
+
constimageFiles=awaitclient.files.find({
236
+
filters: {
237
+
mime: { $contains:'image' }, // Only get image files
238
+
name: { $contains:'avatar' }, // Only get files with 'avatar' in the name
239
+
},
240
+
sort: ['name:asc'], // Sort by name in ascending order
241
+
});
242
+
```
243
+
244
+
#### `findOne`
196
245
197
-
### Media File Upload <NewBadge />
246
+
The `strapi.client.files.findOne()` method retrieves the metadata for a single file by its id.
198
247
199
-
The Strapi Client provides media file upload functionality through the `FilesManager`, accessible as `client.files`.
248
+
The method can be used as follows:
200
249
201
-
The `client.files.upload()` method allows you to upload media files (such as images, videos, or documents) to your Strapi backend. It supports uploading files as `Blob` (in browsers or Node.js) or as `Buffer` (in Node.js). The method also supports attaching metadata to the uploaded file, such as `alternativeText` and `caption`.
250
+
```js
251
+
// Initialize the client
252
+
constclient=strapi({
253
+
baseURL:'http://localhost:1337/api',
254
+
auth:'your-api-token',
255
+
});
202
256
257
+
// Find file metadata by ID
258
+
constfile=awaitclient.files.findOne(1);
259
+
console.log(file.name);
260
+
console.log(file.url);
261
+
console.log(file.mime); // The file MIME type
262
+
```
263
+
264
+
#### `update`
203
265
204
-
### Method Signature
266
+
The `strapi.client.files.findOne()` method updates metadata for an existing filei, accepting 2 parameters, the `fileId`, and an object containing options such as the name, alternative text, and caption for the media.
267
+
268
+
The methods can be used as follows:
269
+
270
+
```js
271
+
// Initialize the client
272
+
constclient=strapi({
273
+
baseURL:'http://localhost:1337/api',
274
+
auth:'your-api-token',
275
+
});
276
+
277
+
// Update file metadata
278
+
constupdatedFile=awaitclient.files.update(1, {
279
+
name:'New file name',
280
+
alternativeText:'Descriptive alt text for accessibility',
281
+
caption:'A caption for the file',
282
+
});
283
+
```
284
+
285
+
286
+
#### `upload` <NewBadge />
287
+
288
+
The Strapi Client provides media file upload functionality through the `FilesManager`, accessible through the `strapi.client.files.upload()` method. The method allows you to upload media files (such as images, videos, or documents) to your Strapi backend.
289
+
290
+
The method supports uploading files as `Blob` (in browsers or Node.js) or as `Buffer` (in Node.js only). The method also supports attaching metadata to the uploaded file, such as `alternativeText` and `caption`.
The response is an array of file objects, each containing details such as `id`, `name`, `url`, `size`, and `mime`[source](https://github.com/strapi/client/blob/60a0117e361346073bed1959d354c7facfb963b3/src/files/types.ts).
215
303
216
-
**Usage examples:**
304
+
<Tabs>
305
+
<TabItemvalue="browser"label="Upload a file with the browser">
217
306
218
-
#### Uploading a File in the Browser
307
+
You can upload a file use through the browser as follows:
<TabItemvalue="node"label="Upload a file with Node.js">
331
+
332
+
With Node.js, you can either upload a blob or a buffer, as in the following examples:
333
+
334
+
<Tabs>
335
+
<TabItemvalue="node-blob"label="Uploading a Blob">
240
336
241
337
```js
242
338
import { readFile } from'fs/promises';
@@ -262,7 +358,9 @@ try {
262
358
}
263
359
```
264
360
265
-
#### Uploading a Buffer in Node.js
361
+
</TabItem>
362
+
363
+
<TabItemvalue="node-buffer"label="Uploading a Buffer">
266
364
267
365
```js
268
366
import { readFile } from'fs/promises';
@@ -288,10 +386,15 @@ try {
288
386
}
289
387
```
290
388
389
+
</TabItem>
390
+
</Tabs>
391
+
392
+
</TabItem>
393
+
</Tabs>
291
394
292
-
### Response Structure
395
+
#####Response Structure
293
396
294
-
The upload method returns an array of file objects, each with fields such as:
397
+
The `strapi.client.files.upload()` method returns an array of file objects, each with fields such as:
295
398
296
399
```json
297
400
{
@@ -307,21 +410,11 @@ The upload method returns an array of file objects, each with fields such as:
307
410
}
308
411
```
309
412
413
+
#### `delete`
310
414
311
-
### Managing files
415
+
The `strapi.client.files.delete()` method deletes a file by its ID.
312
416
313
-
The Strapi Client provides access to the [Media Library](/cms/features/media-library) via the `files` property. This allows you to retrieve and manage file metadata without directly interacting with the REST API.
314
-
315
-
The following methods are available for working with files:
316
-
317
-
| Method | Description |
318
-
|--------|-------------|
319
-
|`find(params?)`| Retrieves a list of file metadata based on optional query parameters |
320
-
|`findOne(fileId)`| Retrieves the metadata for a single file by its ID |
321
-
|`update(fileId, fileInfo)`| Updates metadata for an existing file |
322
-
|`delete(fileId)`| Deletes a file by its ID |
323
-
324
-
**Usage examples:**
417
+
The method can be used as follows:
325
418
326
419
```js
327
420
// Initialize the client
@@ -330,44 +423,24 @@ const client = strapi({
330
423
auth:'your-api-token',
331
424
});
332
425
333
-
// Find all file metadata
334
-
constallFiles=awaitclient.files.find();
335
-
console.log(allFiles);
336
-
337
-
// Find file metadata with filtering and sorting
338
-
constimageFiles=awaitclient.files.find({
339
-
filters: {
340
-
mime: { $contains:'image' }, // Only get image files
341
-
name: { $contains:'avatar' }, // Only get files with 'avatar' in the name
342
-
},
343
-
sort: ['name:asc'], // Sort by name in ascending order
344
-
});
345
-
346
-
// Find file metadata by ID
347
-
constfile=awaitclient.files.findOne(1);
348
-
console.log(file.name); // The file name
349
-
console.log(file.url); // The file URL
350
-
console.log(file.mime); // The file MIME type
351
-
352
-
// Update file metadata
353
-
constupdatedFile=awaitclient.files.update(1, {
354
-
name:'New file name',
355
-
alternativeText:'Descriptive alt text for accessibility',
-**Permission Errors:** If the authenticated user does not have permission to upload or manage files, a `FileForbiddenError` is thrown.
369
-
-**HTTP Errors:** If the server is unreachable, authentication fails, or there are network issues, an `HTTPError` is thrown.
370
-
-**Missing Parameters:** When uploading a `Buffer`, both `filename` and `mimetype` must be provided in the options object. If either is missing, an error is thrown.
437
+
The following errors might occur when sending queries through the Strapi Client:
438
+
439
+
| Error | Description |
440
+
|-------|-------------|
441
+
| Permission Errors | If the authenticated user does not have permission to upload or manage files, a `FileForbiddenError` is thrown. |
442
+
| HTTP Errors|If the server is unreachable, authentication fails, or there are network issues, an `HTTPError` is thrown. |
443
+
| Missing Parameters|When uploading a `Buffer`, both `filename` and `mimetype` must be provided in the options object. If either is missing, an error is thrown. |
0 commit comments