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
@@ -14,6 +15,7 @@ tags:
14
15
The Strapi Client library simplifies interactions with your Strapi back end, providing a way to fetch, create, update, and delete content. This guide walks you through setting up the Strapi Client, configuring authentication, and using its key features effectively.
15
16
16
17
## Getting Started
18
+
17
19
:::prerequisites
18
20
- A Strapi project has been created and is running. If you haven't set one up yet, follow the [Quick Start Guide](/cms/quick-start) to create one.
19
21
- You know the URL of the Content API of your Strapi instance (e.g., `http://localhost:1337/api`).
@@ -51,13 +53,32 @@ To use the Strapi Client in your project, install it as a dependency using your
51
53
52
54
To start interacting with your Strapi back end, initialize the Strapi Client and set the base API URL:
53
55
56
+
<TabsgroupId="js-ts">
57
+
<TabItemvalue="js"label="JavaScript">
58
+
59
+
With Javascript, import the `strapi` function and create a client instance:
Single types in Strapi represent unique content entries that exist only once (e.g., the homepage settings or site-wide configurations). The Strapi Client provides a `single()` method to interact with these resources, with the following methods available:
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.
171
205
172
-
The following methods are available for working with files:
206
+
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:
173
207
174
208
| Method | Description |
175
209
|--------|-------------|
176
-
|`find(params?)`| Retrieves a list of file metadata based on optional query parameters |
177
-
|`findOne(fileId)`| Retrieves the metadata for a single file by its ID |
178
-
|`update(fileId, fileInfo)`| Updates metadata for an existing file |
179
-
|`delete(fileId)`| Deletes a file by its ID |
210
+
|[`find(params?)`](#find)| Retrieves a list of file metadata based on optional query parameters |
211
+
|[`findOne(fileId)`](#findone)| Retrieves the metadata for a single file by its ID |
212
+
|[`update(fileId, fileInfo)`](#update)| Updates metadata for an existing file |
213
+
|[`upload(file, options)`](#upload)| Uploads a file (Blob or Buffer) with an optional `options` object for metadata |
214
+
|[`delete(fileId)`](#delete)| Deletes a file by its ID |
180
215
181
-
**Usage examples:**
216
+
#### `find`
217
+
218
+
The `strapi.client.files.find()` method retrieves a list of file metadata based on optional query parameters.
sort: ['name:asc'], // Sort by name in ascending order
201
240
});
241
+
```
242
+
243
+
#### `findOne` {#findone}
244
+
245
+
The `strapi.client.files.findOne()` method retrieves the metadata for a single file by its id.
246
+
247
+
The method can be used as follows:
248
+
249
+
```js
250
+
// Initialize the client
251
+
constclient=strapi({
252
+
baseURL:'http://localhost:1337/api',
253
+
auth:'your-api-token',
254
+
});
202
255
203
256
// Find file metadata by ID
204
257
constfile=awaitclient.files.findOne(1);
205
-
console.log(file.name);// The file name
206
-
console.log(file.url); // The file URL
258
+
console.log(file.name);
259
+
console.log(file.url);
207
260
console.log(file.mime); // The file MIME type
261
+
```
262
+
263
+
#### `update`
264
+
265
+
The `strapi.client.files.update()` method updates metadata for an existing file, accepting 2 parameters, the `fileId`, and an object containing options such as the name, alternative text, and caption for the media.
266
+
267
+
The methods can be used as follows:
268
+
269
+
```js
270
+
// Initialize the client
271
+
constclient=strapi({
272
+
baseURL:'http://localhost:1337/api',
273
+
auth:'your-api-token',
274
+
});
208
275
209
276
// Update file metadata
210
277
constupdatedFile=awaitclient.files.update(1, {
211
278
name:'New file name',
212
279
alternativeText:'Descriptive alt text for accessibility',
213
280
caption:'A caption for the file',
214
281
});
282
+
```
283
+
284
+
#### `upload` <NewBadge /> {#upload}
285
+
286
+
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.
287
+
288
+
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`.
- For `Blob` uploads, `options` is optional and may include `fileInfo` for metadata.
298
+
- For `Buffer` uploads, `options` must include `filename` and `mimetype`, and may include `fileInfo`.
299
+
300
+
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).
301
+
302
+
<Tabs>
303
+
<TabItemvalue="browser"label="Upload a file with the browser">
304
+
305
+
You can upload a file use through the browser as follows:
The `strapi.client.files.upload()` method returns an array of file objects, each with fields such as:
396
+
397
+
```json
398
+
{
399
+
"id": 1,
400
+
"name": "image.png",
401
+
"alternativeText": "Uploaded from Node.js Buffer",
402
+
"caption": "Example upload",
403
+
"mime": "image/png",
404
+
"url": "/uploads/image.png",
405
+
"size": 12345,
406
+
"createdAt": "2025-07-23T12:34:56.789Z",
407
+
"updatedAt": "2025-07-23T12:34:56.789Z"
408
+
}
409
+
```
410
+
411
+
:::note Additional response fields
412
+
The upload response includes additional fields beyond those shown above. See the complete FileResponse interface in the <ExternalLinkto="https://github.com/strapi/client/blob/main/src/files/types.ts"text="client source code"/> for all available fields.
413
+
:::
414
+
415
+
#### `delete`
416
+
417
+
The `strapi.client.files.delete()` method deletes a file by its ID.
The following errors might occur when sending queries through the Strapi Client:
440
+
441
+
| Error | Description |
442
+
|-------|-------------|
443
+
| Permission Errors | If the authenticated user does not have permission to upload or manage files, a `FileForbiddenError` is thrown. |
444
+
| HTTP Errors|If the server is unreachable, authentication fails, or there are network issues, an `HTTPError` is thrown. |
445
+
| 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. |
446
+
447
+
223
448
:::strapi Additional information
224
-
More details about the Strapi Strapi Client might be found in the <ExternalLinkto="https://github.com/strapi/client/blob/main/README.md"text="package's README"/>.
225
-
:::
449
+
More details about the Strapi Client may be found in the <ExternalLinkto="https://github.com/strapi/client/blob/main/README.md"text="package's README"/>.
0 commit comments