REST API
The REST API allows accessing the content-types through API endpoints. Strapi automatically creates API endpoints when a content-type is created. API parameters can be used when querying API endpoints to refine the results.
All content types are private by default and need to be either made public or queries need to be authenticated with the proper permissions. See the Quick Start Guide, the user guide for the Users & Permissions plugin, and API tokens configuration documentation for more details.
By default, the REST API responses only include top-level fields and does not populate any relations, media fields, components, or dynamic zones. Use the populate
parameter to populate specific fields. Ensure that the find permission is given to the field(s) for the relation(s) you populate.
The Upload plugin (which handles media found in the Media Library) has a specific API described in the Upload plugin documentation.
Endpoints
For each Content-Type, the following endpoints are automatically generated:
- Collection type
- Single type
Method | URL | Description |
---|---|---|
GET | /api/:pluralApiId | Get a list of entries |
POST | /api/:pluralApiId | Create an entry |
GET | /api/:pluralApiId/:documentId | Get an entry |
PUT | /api/:pluralApiId/:documentId | Update an entry |
DELETE | /api/:pluralApiId/:documentId | Delete an entry |
Method | URL | Description |
---|---|---|
GET | /api/:singularApiId | Get an entry |
PUT | /api/:singularApiId | Update/Create an entry |
DELETE | /api/:singularApiId | Delete an entry |
Examples:
- Collection type
- Single type
Restaurant
Content type
Method | URL | Description |
---|---|---|
GET | /api/restaurants | Get a list of restaurants |
POST | /api/restaurants | Create a restaurant |
GET | /api/restaurants/:id | Get a specific restaurant |
DELETE | /api/restaurants/:id | Delete a restaurant |
PUT | /api/restaurants/:id | Update a restaurant |
Homepage
Content type
Method | URL | Description |
---|---|---|
GET | /api/homepage | Get the homepage content |
PUT | /api/homepage | Update/create the homepage content |
DELETE | /api/homepage | Delete the homepage content |
Components don't have API endpoints.
API endpoints are prefixed with /api
by default. This can be changed by setting a different value for the rest.prefix
configuration parameter (see API calls configuration).
Requests
Requests return a response as an object which usually includes the following keys:
data
: the response data itself, which could be:- a single entry, as an object with the following keys:
id
(number)attributes
(object)meta
(object)
- a list of entries, as an array of objects
- a custom response
- a single entry, as an object with the following keys:
meta
(object): information about pagination, publication state, available locales, etc.error
(object, optional): information about any error thrown by the request
Some plugins (including Users & Permissions and Upload) may not follow this response format.
Get entries
Returns entries matching the query filters (see API parameters documentation).
GET http://localhost:1337/api/restaurants
{
"data": [
{
"id": 1,
"attributes": {
"title": "Restaurant A",
"description": "Restaurant A's description"
},
"meta": {
"availableLocales": []
}
},
{
"id": 2,
"attributes": {
"title": "Restaurant B",
"description": "Restaurant B's description"
},
"meta": {
"availableLocales": []
}
},
],
"meta": {}
}
Get an entry
Returns an entry by id
.
GET http://localhost:1337/api/restaurants/1
{
"data": {
"id": 1,
"attributes": {
"title": "Restaurant A",
"description": "Restaurant A's description"
},
"meta": {
"availableLocales": []
}
},
"meta": {}
}
Create an entry
Creates an entry and returns its value.
If the Internationalization (i18n) plugin is installed, it's possible to use POST requests to the REST API to create localized entries.
While creating an entry, you can define its relations and their order (see Managing relations through the REST API for more details).
POST http://localhost:1337/api/restaurants
{
"data": {
"title": "Hello",
"relation_field_a": 2,
"relation_field_b": [2, 4],
"link": {
"id": 1,
"type": "abc"
}
}
}
{
"data": {
"id": 1,
"attributes": { … },
"meta": {}
},
"meta": {}
}
Update an entry
Partially updates an entry by id
and returns its value.
Fields that aren't sent in the query are not changed in the database. Send a null
value to clear fields.
- Even with the Internationalization (i18n) plugin installed, it's currently not possible to update the locale of an entry.
- While updating an entry, you can define its relations and their order (see Managing relations through the REST API for more details).
PUT http://localhost:1337/api/restaurants/1
{
"data": {
"title": "Hello",
"relation_field_a": 2,
"relation_field_b": [2, 4],
}
}
{
"data": {
"id": 1,
"attributes": {},
"meta": {}
},
"meta": {}
}
Delete an entry
Deletes an entry by id
and returns its value.
DELETE http://localhost:1337/api/restaurants/1
{
"data": {
"id": 1,
"attributes": {},
"meta": {}
},
"meta": {}
}