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
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.
1724
1724
1725
1725
## Getting Started
1726
+
1726
1727
:::prerequisites
1727
1728
- 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.
1728
1729
- You know the URL of the Content API of your Strapi instance (e.g., `http://localhost:1337/api`).
@@ -1738,26 +1739,15 @@ To use the Strapi Client in your project, install it as a dependency using your
1738
1739
1739
1740
To start interacting with your Strapi back end, initialize the Strapi Client and set the base API URL:
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.
1857
1830
1858
-
The following methods are available for working with files:
1831
+
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:
1859
1832
1860
1833
| Method | Description |
1861
1834
|--------|-------------|
1862
-
| `find(params?)` | Retrieves a list of file metadata based on optional query parameters |
1863
-
| `findOne(fileId)` | Retrieves the metadata for a single file by its ID |
1864
-
| `update(fileId, fileInfo)` | Updates metadata for an existing file |
1865
-
| `delete(fileId)` | Deletes a file by its ID |
1835
+
| [`find(params?)`](#find) | Retrieves a list of file metadata based on optional query parameters |
1836
+
| [`findOne(fileId)`](#findone) | Retrieves the metadata for a single file by its ID |
1837
+
| [`update(fileId, fileInfo)`](#update) | Updates metadata for an existing file |
1838
+
| [`upload(file, options)`](#upload) | Uploads a file (Blob or Buffer) with an optional `options` object for metadata |
1839
+
| [`delete(fileId)`](#delete) | Deletes a file by its ID |
1866
1840
1867
-
**Usage examples:**
1841
+
#### `find`
1842
+
1843
+
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
1887
1865
});
1866
+
```
1867
+
1868
+
#### `findOne` {#findone}
1869
+
1870
+
The `strapi.client.files.findOne()` method retrieves the metadata for a single file by its id.
1871
+
1872
+
The method can be used as follows:
1873
+
1874
+
```js
1875
+
// Initialize the client
1876
+
const client = strapi({
1877
+
baseURL: 'http://localhost:1337/api',
1878
+
auth: 'your-api-token',
1879
+
});
1888
1880
1889
1881
// Find file metadata by ID
1890
1882
const file = await client.files.findOne(1);
1891
-
console.log(file.name); // The file name
1892
-
console.log(file.url); // The file URL
1883
+
console.log(file.name);
1884
+
console.log(file.url);
1893
1885
console.log(file.mime); // The file MIME type
1886
+
```
1887
+
1888
+
#### `update`
1889
+
1890
+
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.
The following errors might occur when sending queries through the Strapi Client:
1963
+
1964
+
| Error | Description |
1965
+
|-------|-------------|
1966
+
| Permission Errors | If the authenticated user does not have permission to upload or manage files, a `FileForbiddenError` is thrown. |
1967
+
| HTTP Errors|If the server is unreachable, authentication fails, or there are network issues, an `HTTPError` is thrown. |
1968
+
| 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. |
1969
+
1909
1970
:::strapi Additional information
1910
-
More details about the Strapi Strapi Client might be found in the .
1971
+
More details about the Strapi Client may be found in the .
0 commit comments