Skip to content

Commit 41ff4a9

Browse files
committed
🤖 Update LLMs files [skip ci]
1 parent dd07583 commit 41ff4a9

File tree

1 file changed

+106
-45
lines changed

1 file changed

+106
-45
lines changed

‎docusaurus/static/llms-full.txt

Lines changed: 106 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,7 @@ Source: https://docs.strapi.io/cms/api/client
17231723
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.
17241724

17251725
## Getting Started
1726+
17261727
:::prerequisites
17271728
- 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.
17281729
- 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
17381739

17391740
To start interacting with your Strapi back end, initialize the Strapi Client and set the base API URL:
17401741

1741-
```js
1742-
1743-
const client = strapi({ baseURL: 'http://localhost:1337/api' });
1744-
```
1745-
1746-
If you're using the Strapi Client in a browser environment, you can include it using a `<script>` tag:
1742+
</Tabs>
17471743

1748-
```js title="./src/api/[apiName]/routes/[routerName].ts (e.g './src/api/restaurant/routes/restaurant.ts')"
1749-
<script src="https://cdn.jsdelivr.net/npm/@strapi/client"></script>
1750-
1751-
<script>
1752-
const client = strapi.strapi({ baseURL: 'http://localhost:1337/api' });
1753-
</script>
1754-
```
1744+
The `baseURL` must include the protocol (`http` or `https`). An invalid URL will throw an error `StrapiInitializationError`.
17551745

17561746
### Authentication
17571747

17581748
The Strapi Client supports different authentication strategies to access protected resources in your Strapi back end.
17591749

1760-
If your Strapi instance uses API tokens, configure the Strapi Client as follows:
1750+
If your Strapi instance uses [API tokens](/cms/features/api-tokens), configure the Strapi Client as follows:
17611751

17621752
```js
17631753
const client = strapi({
@@ -1767,6 +1757,7 @@ const client = strapi({
17671757
```
17681758

17691759
This allows your requests to include the necessary authentication credentials automatically.
1760+
If the token is invalid or missing, the client will throw an error during initialization `StrapiValidationError`.
17701761

17711762
## API Reference
17721763

@@ -1778,6 +1769,7 @@ The Strapi Client provides the following key properties and methods for interact
17781769
| `fetch()` | A utility method for making generic API requests similar to the native fetch API. |
17791770
| `collection()` | Manages collection-type resources (e.g., blog posts, products). |
17801771
| `single()` | Manages single-type resources (e.g., homepage settings, global configurations). |
1772+
| `files()` | Enables upload, retrieve and management of files directly to/from the Strapi Media Library. |
17811773

17821774
### General purpose fetch
17831775

@@ -1800,27 +1792,8 @@ Collection types in Strapi are entities with multiple entries (e.g., a blog with
18001792
| `delete(documentID, queryParams?)` | Update an existing document. |
18011793

18021794
**Usage examples:**
1803-
```js
1804-
const articles = client.collection('articles');
18051795

1806-
// Fetch all english articles sorted by title
1807-
const allArticles = await articles.find({
1808-
locale: 'en',
1809-
sort: 'title',
1810-
});
1811-
1812-
// Fetch a single article
1813-
const singleArticle = await articles.findOne('article-document-id');
1814-
1815-
// Create a new article
1816-
const newArticle = await articles.create({ title: 'New Article', content: '...' });
1817-
1818-
// Update an existing article
1819-
const updatedArticle = await articles.update('article-document-id', { title: 'Updated Title' });
1820-
1821-
// Delete an article
1822-
await articles.delete('article-id');
1823-
```
1796+
</Tabs>
18241797

18251798
### Working with single types
18261799

@@ -1829,7 +1802,7 @@ Single types in Strapi represent unique content entries that exist only once (e.
18291802
| ----------| -------------------------------------------------------------------------------------------- |
18301803
| `find(queryParams?)` | Fetch the document. |
18311804
| `update(documentID, data, queryParams?)` | Update the document. |
1832-
| `delete(queryParams?) ` | Remove the document. |
1805+
| `delete(queryParams?)` | Remove the document. |
18331806

18341807
**Usage examples:**
18351808
```js
@@ -1851,20 +1824,25 @@ const updatedHomepage = await homepage.update(
18511824
await homepage.delete();
18521825
```
18531826

1854-
### Working with files
1827+
### Working with files
18551828

18561829
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.
18571830

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:
18591832

18601833
| Method | Description |
18611834
|--------|-------------|
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 |
18661840

1867-
**Usage examples:**
1841+
#### `find`
1842+
1843+
The `strapi.client.files.find()` method retrieves a list of file metadata based on optional query parameters.
1844+
1845+
The method can be used as follows:
18681846

18691847
```js
18701848
// Initialize the client
@@ -1885,19 +1863,90 @@ const imageFiles = await client.files.find({
18851863
},
18861864
sort: ['name:asc'], // Sort by name in ascending order
18871865
});
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+
});
18881880

18891881
// Find file metadata by ID
18901882
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);
18931885
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.
1891+
1892+
The methods can be used as follows:
1893+
1894+
```js
1895+
// Initialize the client
1896+
const client = strapi({
1897+
baseURL: 'http://localhost:1337/api',
1898+
auth: 'your-api-token',
1899+
});
18941900

18951901
// Update file metadata
18961902
const updatedFile = await client.files.update(1, {
18971903
name: 'New file name',
18981904
alternativeText: 'Descriptive alt text for accessibility',
18991905
caption: 'A caption for the file',
19001906
});
1907+
```
1908+
1909+
#### `upload`
1910+
1911+
</Tabs>
1912+
1913+
</TabItem>
1914+
</Tabs>
1915+
1916+
##### Response Structure
1917+
1918+
The `strapi.client.files.upload()` method returns an array of file objects, each with fields such as:
1919+
1920+
```json
1921+
{
1922+
"id": 1,
1923+
"name": "image.png",
1924+
"alternativeText": "Uploaded from Node.js Buffer",
1925+
"caption": "Example upload",
1926+
"mime": "image/png",
1927+
"url": "/uploads/image.png",
1928+
"size": 12345,
1929+
"createdAt": "2025-07-23T12:34:56.789Z",
1930+
"updatedAt": "2025-07-23T12:34:56.789Z"
1931+
}
1932+
```
1933+
1934+
:::note Additional response fields
1935+
The upload response includes additional fields beyond those shown above. See the complete FileResponse interface in the for all available fields.
1936+
:::
1937+
1938+
#### `delete`
1939+
1940+
The `strapi.client.files.delete()` method deletes a file by its ID.
1941+
1942+
The method can be used as follows:
1943+
1944+
```js
1945+
// Initialize the client
1946+
const client = strapi({
1947+
baseURL: 'http://localhost:1337/api',
1948+
auth: 'your-api-token',
1949+
});
19011950

19021951
// Delete a file by ID
19031952
const deletedFile = await client.files.delete(1);
@@ -1906,8 +1955,20 @@ console.log('Deleted file ID:', deletedFile.id);
19061955
console.log('Deleted file name:', deletedFile.name);
19071956
```
19081957

1958+
<br/>
1959+
1960+
## Handling Common Errors
1961+
1962+
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+
19091970
:::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 .
19111972
:::
19121973

19131974

0 commit comments

Comments
 (0)