REST API
The REST API is a fully functional HTTP client that allows you to interact with your Documents in a RESTful manner. It supports all CRUD operations and is equipped with automatic pagination, depth, and sorting.
All Payload API routes are mounted and prefixed to your config's routes.api
URL segment (default: /api
).
REST query parameters:
- depth - automatically populates relationships and uploads
- locale - retrieves document(s) in a specific locale
- fallback-locale - specifies a fallback locale if no locale value exists
- select - specifies which fields to include to the result
- populate - specifies which fields to include to the result from populated documents
Collections
Each collection is mounted using its slug
value. For example, if a collection's slug is users
, all corresponding routes will be mounted on /api/users
.
Note: Collection slugs must be formatted in kebab-case
All CRUD operations are exposed as follows:
Operation | Method | Path | View |
---|---|---|---|
Find | GET | /api/{collection-slug} | |
Find By ID | GET | /api/{collection-slug}/{id} | |
Count | GET | /api/{collection-slug}/count | |
Create | POST | /api/{collection-slug} | |
Update | PATCH | /api/{collection-slug} | |
Update By ID | PATCH | /api/{collection-slug}/{id} | |
Delete | DELETE | /api/{collection-slug} | |
Delete by ID | DELETE | /api/{collection-slug}/{id} |
Auth Operations
Auth enabled collections are also given the following endpoints:
Operation | Method | Path | View |
---|---|---|---|
Login | POST | /api/{user-collection}/login | |
Logout | POST | /api/{user-collection}/logout | |
Unlock | POST | /api/{user-collection}/unlock | |
Refresh | POST | /api/{user-collection}/refresh-token | |
Verify User | POST | /api/{user-collection}/verify/{token} | |
Current User | GET | /api/{user-collection}/me | |
Forgot Password | POST | /api/{user-collection}/forgot-password | |
Reset Password | POST | /api/{user-collection}/reset-password |
Globals
Globals cannot be created or deleted, so there are only two REST endpoints opened:
Operation | Method | Path | View |
---|---|---|---|
Get Global | GET | /api/globals/{global-slug} | |
Update Global | POST | /api/globals/{global-slug} |
Preferences
In addition to the dynamically generated endpoints above Payload also has REST endpoints to manage the admin user preferences for data specific to the authenticated user.
Operation | Method | Path | View |
---|---|---|---|
Get Preference | GET | /api/payload-preferences/{key} | |
Create Preference | POST | /api/payload-preferences/{key} | |
Delete Preference | DELETE | /api/payload-preferences/{key} |
Custom Endpoints
Additional REST API endpoints can be added to your application by providing an array of endpoints
in various places within a Payload Config. Custom endpoints are useful for adding additional middleware on existing routes or for building custom functionality into Payload apps and plugins. Endpoints can be added at the top of the Payload Config, collections
, and globals
and accessed respective of the api and slugs you have configured.
Each endpoint object needs to have:
Property | Description |
---|---|
path | A string for the endpoint route after the collection or globals slug |
method | The lowercase HTTP verb to use: 'get', 'head', 'post', 'put', 'delete', 'connect' or 'options' |
handler | A function or array of functions to be called with req, res and next arguments. Next.js |
root | When true , defines the endpoint on the root Next.js app, bypassing Payload handlers and the routes.api subpath. Note: this only applies to top-level endpoints of your Payload Config, endpoints defined on collections or globals cannot be root. |
custom | Extension point for adding custom data (e.g. for plugins) |
Example:
Helpful tips
req.data
Data is not automatically appended to the request. You can read the body data by calling await req.json()
.
Or you could use our helper function that mutates the request and appends data and file if found.
req.locale
& req.fallbackLocale
The locale and the fallback locale are not automatically appended to custom endpoint requests. If you would like to add them you can use this helper function.
Method Override for GET Requests
Payload supports a method override feature that allows you to send GET requests using the HTTP POST method. This can be particularly useful in scenarios when the query string in a regular GET request is too long.
How to Use
To use this feature, include the X-HTTP-Method-Override
header set to GET
in your POST request. The parameters should be sent in the body of the request with the Content-Type
set to application/x-www-form-urlencoded
.
Example
Here is an example of how to use the method override to perform a GET request: