Document Service API
The Document Service API is built on top of the Query Engine API and is used to perform CRUD (create, retrieve, update, and delete) operations on documents .
The Document Service API also supports counting documents and, if Draft & Publish is enabled on the content-type, performing Strapi-specific operations such as publishing, unpublishing, and discarding drafts.
In Strapi 5, documents are uniquely identified by their documentId at the API level.
The Document Service API replaces the Entity Service API used in Strapi v4 (see Strapi v4 documentation).
Additional information on how to migrate from the Entity Service API to the Document Service API can be found in the migration reference.
Relations can also be connected, disconnected, and set through the Document Service API, just like with the REST API (see the REST API relations documentation for examples).
findOne()
Find a document matching the passed documentId and parameters.
Syntax: findOne(parameters: Params) => Document
Parameters
| Parameter | Description | Default | Type |
|---|---|---|---|
documentId | Document id | ID | |
locale | Locale of the document to find. | Default locale | String or undefined |
status | If Draft & Publish is enabled for the content-type: Publication status, can be:
| 'draft' | 'published' or 'draft' |
fields | Select fields to return | All fields (except those not populated by default) | Object |
populate | Populate results with additional fields. | null | Object |
Example
If only a documentId is passed without any other parameters, findOne() returns the draft version of a document in the default locale:
await strapi.documents('api::restaurant.restaurant').findOne({
documentId: 'a1b2c3d4e5f6g7h8i9j0klm'
})
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
publishedAt: null, // draft version (default)
locale: "en", // default locale
// …
}
The findOne() method returns the matching document if found, otherwise returns null.
findFirst()
Find the first document matching the parameters.
Syntax: findFirst(parameters: Params) => Document
Parameters
| Parameter | Description | Default | Type |
|---|---|---|---|
locale | Locale of the documents to find. | Default locale | String or undefined |
status | If Draft & Publish is enabled for the content-type: Publication status, can be:
| 'draft' | 'published' or 'draft' |
filters | Filters to use | null | Object |
fields | Select fields to return | All fields (except those not populate by default) | Object |
populate | Populate results with additional fields. | null | Object |
Examples
Generic example
By default, findFirst() returns the draft version, in the default locale, of the first document for the passed unique identifier (collection type id or single type id):
await strapi.documents('api::restaurant.restaurant').findFirst()
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Restaurant Biscotte",
publishedAt: null,
locale: "en"
// …
}
Find the first document matching parameters
Pass some parameters to findFirst() to return the first document matching them.
If no locale or status parameters are passed, results return the draft version for the default locale:
await strapi.documents('api::restaurant.restaurant').findFirst(
{
filters: {
name: {
$startsWith: "Pizzeria"
}
}
}
)
{
documentId: "j9k8l7m6n5o4p3q2r1s0tuv",
name: "Pizzeria Arrivederci",
publishedAt: null,
locale: "en"
// …
}
findMany()
Find documents matching the parameters.
Syntax: findMany(parameters: Params) => Document[]
Parameters
| Parameter | Description | Default | Type |
|---|---|---|---|
locale | Locale of the documents to find. | Default locale | String or undefined |
status | If Draft & Publish is enabled for the content-type: Publication status, can be:
| 'draft' | 'published' or 'draft' |
filters | Filters to use | null | Object |
fields | Select fields to return | All fields (except those not populate by default) | Object |
populate | Populate results with additional fields. | null | Object |
pagination | Paginate results | ||
sort | Sort results |
Examples
Generic example
When no parameter is passed, findMany() returns the draft version in the default locale for each document:
await strapi.documents('api::restaurant.restaurant').findMany()
[
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
publishedAt: null, // draft version (default)
locale: "en" // default locale
// …
},
{
documentId: "j9k8l7m6n5o4p3q2r1s0tuv",
name: "Pizzeria Arrivederci",
publishedAt: null,
locale: "en"
// …
},
]